class Server: def __init__(self): self.session = ServerSession() self.clientSessions = dict() self.running = True def acceptConnectionLoop(self): while self.running: data = self.session.getUnreadDataQueue() newConnectionSocket, newConnectionUid = data.get() # Blocking call logging.info('Connection from new client %s', newConnectionUid) # Replace any existing session with this uid self.clientSessions[newConnectionUid] = Session(newConnectionSocket) logging.info("Stopping server listening") # TODO this would be better implemented as a coroutine def manageClientsLoop(self): while self.running: for uid, clientSession in self.clientSessions: # Not sure if python supports non-blocking queue iteration, so do this as a while unreadData = clientSession.unreadData.get(block=False) while (unreadData is not None): logging.info("Received data %s", unreadData) for otherUid in self.clientSessions.keys().remove(uid): logging.info("Sending to %s", otherUid) self.clientSessions[otherUid].addDataToSend(unreadData) clientSession.unreadData.remove(unreadData) unreadData = clientSession.unreadData.get(block=False) time.sleep(5) logging.info("Removing client management thread")
def __init__(self): self.session = ServerSession() self.clientSessions = dict() self.running = True
def createConnection(self, sock): return ServerSession(self.engine, sock)