def __init__(self, addr): self._connection_handler = ConnectionHandler() try: self._s_socket = socket.socket() self._s_socket.bind(addr) self._s_socket.listen(1) except socket.error as e: logging.error(e[1]) exit(1) self._c_socket = None
def __init__(self, addr): self._connection_handler = ConnectionHandler() try: self._s_socket = socket.socket() self._s_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._s_socket.bind(addr) self._s_socket.listen(1) except socket.error as e: logging.error(e[1]) exit(1) self._c_socket = None
class ServerHandler(object): def __init__(self, addr): self._connection_handler = ConnectionHandler() try: self._s_socket = socket.socket() self._s_socket.bind(addr) self._s_socket.listen(1) except socket.error as e: logging.error(e[1]) exit(1) self._c_socket = None def accept_connection(self): self._c_socket, addr = self._s_socket.accept() self._c_socket = (self._c_socket, addr[0]) logging.info("Recieved connection from %s" % self._c_socket[1]) self._connection_handler.add_connection(self._c_socket[1], self._c_socket[0]) return self._c_socket def get_ctl_sock(self): if self._c_socket != None: return self._c_socket[0] else: return None def get_messages(self): messages = self._connection_handler.check_connections() addr = self._c_socket[1] if self._connection_handler.get_connection(addr) == None: logging.info("Lost controller connection.") self._c_socket = None return messages def send_data_to_ctl(self, data): if self._c_socket != None: return send_data(self._c_socket[0], data) else: return False def add_connection(self, id, connection): self._connection_handler.add_connection(id, connection) def remove_connection(self, key): connection = self._connection_handler.get_connection(key) self._connection_handler.remove_connection(connection) def clear_connections(self): self._connection_handler.clear_connections() def update_connections(self, connections): for key, connection in connections.iteritems(): self.remove_connection(key) self.add_connection(key, connection)
class ServerHandler(object): def __init__(self, addr): self._connection_handler = ConnectionHandler() try: self._s_socket = socket.socket() self._s_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._s_socket.bind(addr) self._s_socket.listen(1) except socket.error as e: logging.error(e[1]) exit(1) self._c_socket = None def accept_connection(self): self._c_socket, addr = self._s_socket.accept() self._c_socket = (self._c_socket, addr[0]) logging.info("Recieved connection from %s" % self._c_socket[1]) self._connection_handler.add_connection(self._c_socket[1], self._c_socket[0]) return self._c_socket def get_ctl_sock(self): if self._c_socket != None: return self._c_socket[0] else: return None def get_messages(self): messages = self._connection_handler.check_connections() #push ctl messages to the end of message queue, this ensures that #update messages are handled first ctl_msgs = [] non_ctl_msgs = [] for msg in messages: if msg[0] == self._c_socket[1]: ctl_msgs.append(msg) else: non_ctl_msgs.append(msg) messages = non_ctl_msgs + ctl_msgs addr = self._c_socket[1] if self._connection_handler.get_connection(addr) == None: logging.info("Lost controller connection.") self._c_socket = None return messages def send_data_to_ctl(self, data): if self._c_socket != None: return send_data(self._c_socket[0], data) else: return False def add_connection(self, id, connection): self._connection_handler.add_connection(id, connection) def remove_connection(self, key): connection = self._connection_handler.get_connection(key) self._connection_handler.remove_connection(connection) def clear_connections(self): self._connection_handler.clear_connections() def update_connections(self, connections): for key, connection in connections.iteritems(): self.remove_connection(key) self.add_connection(key, connection)