def __init__(self, tunnel): self.tunnel = tunnel self.connections = {} # Start a socket server on same interface as calvincontrol self.host = get_calvincontrol().host for x in range(0, 10): try: self.port = randint(5100, 5200) self.server = server_connection.ServerProtocolFactory( self.handle_request, "http") self.server.start(self.host, self.port) _log.info("Control proxy for %s listening on: %s:%s" % (tunnel.peer_node_id, self.host, self.port)) break except: pass # Tell peer node that we a listening and on what uri msg = { "cmd": "started", "controluri": "http://" + get_calvincontrol().external_host + ":" + str(self.port) } self.tunnel.send(msg)
def start(self, node, uri, tunnel=False, external_uri=None): """ If not tunnel, start listening on uri and handle http requests. If tunnel, setup a tunnel to uri and handle requests. """ self.node = node self.security = Security(self.node) schema, _ = uri.split(':', 1) if tunnel: # Connect to tunnel server self.tunnel_client = CalvinControlTunnelClient(uri, self) else: url = urlparse(uri) self.port = int(url.port) self.host = url.hostname if external_uri is not None: self.external_host = urlparse(external_uri).hostname else: self.external_host = self.host _log.info("Control API listening on: %s:%s" % (self.host, self.port)) self.server = server_connection.ServerProtocolFactory( self.handle_request, "http", node_name=node.node_name) self.server.start(self.host, self.port) # Create tunnel server self.tunnel_server = CalvinControlTunnelServer(self.node)
def test_many_clients(self): print_header("TEST_MANY_CLIENTS") print_header("Setup") scheduler = Scheduler_stub() self.factory = server_connection.ServerProtocolFactory( scheduler.trigger_loop, mode='raw', max_length=10) self.factory.start('localhost', 8123) self.conn = None self.client_socket = None print_header("Test_Connection") ################################################################## clients = [] for i in range(100): clients.append(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) for c in clients: yield threads.defer_to_thread(c.connect, ('localhost', 8123)) yield threads.defer_to_thread(hundred_connection_made, self.factory) assert len(self.factory.pending_connections) == 100 for i in range(100): _, self.conn = self.factory.accept() assert not self.factory.pending_connections
def test_args_in_line_mode(self): print_header("TEST_ARGS_IN_LINE_MODE") print_header("Setup") scheduler = Scheduler_stub() self.factory = server_connection.ServerProtocolFactory( scheduler.trigger_loop, delimiter='end', max_length=3) self.factory.start('localhost', 8123) self.conn = None self.client_socket = None self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) yield threads.defer_to_thread(self.client_socket.connect, ('localhost', 8123)) yield threads.defer_to_thread(connection_made, self.factory) _, self.conn = self.factory.accept() print_header("Test_Short_Line_Received") #################################################################### yield threads.defer_to_thread(self.client_socket.send, "123end") yield threads.defer_to_thread(data_available, self.conn) assert self.conn.data_get() == "123" print_header("Test_Long_Line_Received") #################################################################### yield threads.defer_to_thread(self.client_socket.send, "1234end") yield threads.defer_to_thread(data_available, self.conn) assert self.conn.data_get() == "1234" print_header("Teardown") self.factory.stop() yield threads.defer_to_thread(no_more_connections, self.factory)
def init(self, host, port, mode, delimiter, max_length): self._server = server_connection.ServerProtocolFactory( trigger=self.calvinsys._node.sched.schedule_calvinsys, mode=mode, delimiter=delimiter, max_length=max_length, actor_id=self.actor.id) self._server.start(host, port) self._connections = {}
def test_default_line_mode(self): print_header("TEST_DEFAULT_LINE_MODE") print_header("Setup") scheduler = Scheduler_stub() self.factory = server_connection.ServerProtocolFactory( scheduler.trigger_loop) self.factory.start('localhost', 8123) self.conn = None self.client_socket = None print_header("Test_Connection") ################################################################## self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) assert not self.factory.connections assert not self.factory.pending_connections yield threads.defer_to_thread(self.client_socket.connect, ('localhost', 8123)) yield threads.defer_to_thread(connection_made, self.factory) assert self.factory.pending_connections _, self.conn = self.factory.accept() #################################################################### #################################################################### print_header("Test_Line_Received") #################################################################### assert self.conn.data_available is False yield threads.defer_to_thread(self.client_socket.send, "sending string \r\n") yield threads.defer_to_thread(data_available, self.conn) assert self.conn.data_get() == "sending string " print_header("Teardown") self.factory.stop() yield threads.defer_to_thread(no_more_connections, self.factory)
def test_raw_mode(self): print_header("TEST_RAW_MODE") print_header("Setup") scheduler = Scheduler_stub() self.factory = server_connection.ServerProtocolFactory( scheduler.trigger_loop, mode='raw', max_length=10) self.factory.start('localhost', 8123) self.conn = None self.client_socket = None print_header("Test_Connection") ################################################################## self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) yield threads.defer_to_thread(self.client_socket.connect, ('localhost', 8123)) yield threads.defer_to_thread(connection_made, self.factory) assert self.factory.pending_connections _, self.conn = self.factory.accept() assert not self.factory.pending_connections print_header("Test_Data_Received") #################################################################### assert self.conn.data_available is False yield threads.defer_to_thread(self.client_socket.send, "abcdefghijklmnopqrstuvxyz123456789") yield threads.defer_to_thread(data_available, self.conn) assert self.conn.data_get() == "abcdefghij" assert self.conn.data_get() == "klmnopqrst" assert self.conn.data_get() == "uvxyz12345" assert self.conn.data_get() == "6789" print_header("Teardown") self.factory.stop() yield threads.defer_to_thread(no_more_connections, self.factory)