def tcp_thd_conn(port, host='localhost', name=None): moler_conn = ObservableConnection(decoder=lambda data: data.decode("utf-8")) conn_logger_name = 'threaded.tcp-connection({}:{})'.format(host, port) conn_logger = logging.getLogger(conn_logger_name) io_conn = tcp.ThreadedTcp(moler_connection=moler_conn, port=port, host=host, logger=conn_logger) return io_conn
def main(connections2observe4ip): # Starting the servers servers = [] for address, ping_ip in connections2observe4ip: # simulate pinging given IP server_thread, server_done = start_ping_sim_server(address, ping_ip) servers.append((server_thread, server_done)) # Starting the clients connections = [] for address, ping_ip in connections2observe4ip: host, port = address # 1. create Moler's connection that knows encoding decoder = lambda data: data.decode("utf-8") moler_conn = ObservableConnection(decoder=decoder) # 2. create external-IO connection gluing to Moler's connection conn_logger_name = 'threaded.tcp-connection({}:{})'.format(*address) conn_logger = logging.getLogger(conn_logger_name) tcp_connection = tcp.ThreadedTcp(moler_connection=moler_conn, port=port, host=host, logger=conn_logger) client_thread = threading.Thread(target=ping_observing_task, args=(tcp_connection, ping_ip)) client_thread.start() connections.append(client_thread) # await observers job to be done for client_thread in connections: client_thread.join() # stop servers for server_thread, server_done in servers: server_done.set() server_thread.join()