import logging from connection import ServerConnection from connection import ClientConnection class Server: """The parent class of network servers. Subclass this class to implement servers of different kinds. You will probably only need to override the implementation of connected().""" def __init__(self, name): self.name = name def __str__(self): return self.name.__str__() def bind(self, netaddr, closer): """Callback invoked when the server is successfully bound to a NetAddr. The parameters are the netaddr to which the server is bound and closer (as in a thing that causes something to close) is a callback function that should be used when the server wishes to stop listening for new connections.""" self.closer = closer def close(self): """Stop listening for new connections.""" self.closer() def connected(self, client): """Callback invoked when a client connects to this server. This callback must create the Connection object that will be returned to the client.""" logging.debug("server %s connected to client %s", self, client) serverconnection = ServerConnection() clientconnection = ClientConnection(serverconnection) serverconnection.peer = clientconnection return clientconnection