def configure(self): self.cListener = QueuedConnectionListener(self, 0) self.cReader = QueuedConnectionReader(self, 0) self.cWriter = ConnectionWriter(self, 0) self.database_mgr = DatabaseManager(self.database_filepath) self.active_connections = [] self.open_connection()
class NetworkManager(QueuedConnectionManager): def __init__(self, server_address, server_port, database_filepath): QueuedConnectionManager.__init__(self) self.server_address = server_address self.server_port = server_port self.database_filepath = database_filepath def configure(self): self.cListener = QueuedConnectionListener(self, 0) self.cReader = QueuedConnectionReader(self, 0) self.cWriter = ConnectionWriter(self, 0) self.database_mgr = DatabaseManager(self.database_filepath) self.active_connections = [] self.open_connection() def unconfigure(self): if self.tcp_socket: self.cListener.removeConnection(self.tcp_socket) for connection in self.active_connections: if connection: self.cReader.removeConnection(connection) self.active_connections.remove(connection) self.closeConnection(self.tcp_socket) self.cListener = self.cReader = cWriter = None def open_connection(self, backlog=1000): self.tcp_socket = self.openTCPServerRendezvous(self.server_port, backlog) if self.tcp_socket: print ("Database: Opening socket on port %d" % self.server_port) self.cListener.addConnection(self.tcp_socket) taskMgr.add(self.listener_process, "Poll the connection listener") taskMgr.add(self.reader_process, "Poll the connection reader") def listener_process(self, taskname): if self.cListener.newConnectionAvailable(): rendezvous = PointerToConnection() netAddress = NetAddress() newConnection = PointerToConnection() if self.cListener.getNewConnection(rendezvous, netAddress, newConnection): newConnection = newConnection.p() self.active_connections.append(newConnection) self.cReader.addConnection(newConnection) print ("Database: Recieved a new connection, %s" % newConnection) return Task.cont def reader_process(self, taskname): if self.cReader.dataAvailable(): datagram = NetDatagram() if self.cReader.getData(datagram): self.handle_datagram(datagram) return Task.cont def boot_invalid_connection(self, connection): if connection in self.active_connections: self.cReader.removeConnection(connection) self.active_connections.remove(connection) print ("Database: Recieved an invalid connection from %s, booted client." % str(connection)) return def handle_datagram(self, datagram): self.connection = datagram.getConnection() if not self.connection: self.boot_invalid_connection() di = PyDatagramIterator(datagram) msg_type = di.getUint16() if msg_type == REQUEST_FOR_LOGIN: self.database_mgr.handle_login_request(self, self.connection, account_name=di.getString(), account_password=di.getString()) elif msg_type == REQUEST_FOR_REGISTER: self.database_mgr.handle_create_account_request(self, self.connection, di.getString(), di.getString()) elif msg_type == REQUEST_FOR_PASSWORD_CHANGE: return NotImplemented elif msg_type == REQUEST_FOR_ACCOUNT_DELETE: return NotImplemented else: print ("Database: Recieved an unknown protocol request: %d from %s" % (msg_type, str(self.connection))) return def send_response(self, connection, datagram): if connection in self.active_connections: self.cWriter.send(datagram, connection) else: print ("Database: Tried to send data to a non-existant connection.") return