def save_data(n_clicks, old_output): if n_clicks == 0 or n_clicks == 1: raise PreventUpdate data_presenter = Data_Presenter.get_instance() packet_table = data_presenter.get_packet_table() dataframe = pd.DataFrame(columns=['sent_at', 'received_at']) try: for x in [['a', 'b']]: # prep_data(packet_table): dataframe.append({'sent_at': x[0], 'received_at': x[1]}, ignore_index=True) csv_string = dataframe.to_csv(index=False, encoding='utf-8') csv_string = "data:text/csv;charset=utf-8," + urllib.parse.quote(csv_string) file_name = f'loss_data_{datetime.now().strftime("%Y-%m-%d_%H:%M:%S")}.csv' download_link = html.A( file_name, id='download-link', download=file_name + ".csv", href=csv_string, target="_blank" ) except RuntimeError as e: log(e) download_link = html.P('The test is not finished yet. Wait for the test to finish') list_item = html.Li(download_link) return old_output + [list_item]
def ready_socket(self): try: # Assign IP address and port number to socket self.server_socket.bind(self.listening_address) except socket.error as error: log("socket could not connect to host") self.shutdown() raise error
def run(self): self.ready_event_loop() self.ready_socket() try: address = self.wait_for_probe() self.run_loop(address) except Exception as e: log("Server timeout. Client didn't connect to server") log(e) raise e
def log(self): one_second_in_the_past = datetime.now() - timedelta(seconds=2) packet_loss = self.calculate_packet_loss_in_pct(one_second_in_the_past) sent = self.data_presenter.get_time_table( )[one_second_in_the_past].sent received = self.data_presenter.get_time_table( )[one_second_in_the_past].received log(f"{sent} packets sent last second |" + f" {received} packets received last second ", " | package loss: {:.2f}".format(packet_loss * 100), end='\r')
def wait_for_probe(self): self.server_socket.settimeout(self.socket_timeout) log("Server ready at: %-15s %s" % self.listening_address) log("Server ready at: %-15s %s" % (self.get_local_ip(), self.listening_address[1])) log("Waiting for a probe to ping") request_and_address = self.server_socket.recvfrom(1024) address: Tuple[str, int] = request_and_address[1] log(f"Request from {address[0]}{address[1]}") return address
def run_loop(self, address): # loop part loop = asyncio.get_event_loop() listen_task = loop.create_datagram_endpoint( lambda: EchoServerProtocol(self), sock=self.server_socket) transport, protocol = loop.run_until_complete(listen_task) # loop.create_task(self.log_forever()) loop.create_task(self.serve_forever(address)) # Running part log('loop is running') try: loop.run_until_complete(asyncio.sleep(self.time_of_sample_size)) log("Test is complete") except KeyboardInterrupt as error: log("test got interrupted") raise error finally: transport.close() self.shutdown()