class DummyServer(serialization.Serializable): classProvides(IChatServerFactory) implements(IChatServer) def __init__(self, agent, port, client_disconnect_timeout=10): self._agent = IConnectionAgent(agent) self.port = port self._messages = list() ### IChatServer ### @replay.side_effect def start(self): pass @replay.side_effect def stop(self): pass @replay.side_effect def get_list(self): return dict() @replay.side_effect def broadcast(self, body): self._messages.append(body) ### methods usufull for testing ### def publish_message(self, body): self._agent.publish_message(body) def get_messages(self): return self._messages ### python specific ### def __eq__(self, other): if type(self) != type(other): return NotImplemented return self.port == other.port def __ne__(self, other): if type(self) != type(other): return NotImplemented return not self.__eq__(other)
def __init__(self, agent, port, client_disconnect_timeout=10): log.LogProxy.__init__(self, agent) log.Logger.__init__(self, self) self._agent = IConnectionAgent(agent) self._port = port # ChatProtocol instances self._connections = list() self._factory = ChatFactory(self, client_disconnect_timeout) self._listener = None
class ChatServer(serialization.Serializable, log.Logger, log.LogProxy): classProvides(IChatServerFactory) implements(IChatServer, IConnectionAgent) def __init__(self, agent, port, client_disconnect_timeout=10): log.LogProxy.__init__(self, agent) log.Logger.__init__(self, self) self._agent = IConnectionAgent(agent) self._port = port # ChatProtocol instances self._connections = list() self._factory = ChatFactory(self, client_disconnect_timeout) self._listener = None ### IChatServer ### @property def port(self): return self._listener and self._listener.getHost().port @replay.side_effect def start(self): if self._listener: raise RuntimeError("start() already called, call stop first") self._listener = reactor.listenTCP(self._port, self._factory) @replay.side_effect def stop(self): if self._listener: self.debug('Stopping chat server.') self._listener.stopListening() for conn in self._connections: conn.disconnect() self._listener = None @replay.side_effect def get_list(self): resp = dict() for prot in self._connections: if prot.session_id: resp[prot.session_id] = prot.ip return resp @replay.side_effect def broadcast(self, body): for conn in self._connections: conn.send_msg(body) ### delegate IConnectionAgent for ChatFactory to use def validate_session(self, session_id): return self._agent.validate_session(session_id) def publish_message(self, body): self._agent.publish_message(body) self.broadcast(body) ### private interface for the use of ChatProtocol def connection_made(self, prot): self._connections.append(prot) def connection_lost(self, prot): if prot.session_id: self._agent.connection_lost(prot.session_id) self._connections.remove(prot) ### python specific ### def __eq__(self, other): if type(self) != type(other): return NotImplemented return self.port == other.port def __ne__(self, other): if type(self) != type(other): return NotImplemented return not self.__eq__(other)
def __init__(self, agent, port, client_disconnect_timeout=10): self._agent = IConnectionAgent(agent) self.port = port self._messages = list()