def handle(self): # socketType refers to whether the socket is a speaker # or listener from the _client's_ point of view. socketType = n.receive(self.request) if socketType == "speak": server.clientIDLock.acquire() clientID = server.clientIDCounter server.clientIDCounter += 1 server.clientIDLock.release() # Tell the client its ID. n.send(self.request, serialize(clientID)) print( "[" + self.client_address[0] + " connected as client " + str(clientID) + ".]") # Create a queue corresponding to this client. server.messages.append(Queue.Queue()) while True: serializedMessage = n.receive(self.request) if serializedMessage: message = unserialize(serializedMessage) print ( "[" + message.__class__.__name__ + ": " + str(message) + "]") assignID(message) server.distributionQueue.put(message) newMessageTree = trees.MessageTree(message) server.baseMessageTree.append(newMessageTree) else: break print "[Client " + str(clientID) + " quit.]" else: assert socketType[0:6] == "listen" clientID = int(unserialize(socketType[6:])) n.send(self.request, serialize(server.baseMessageTree)) # While the client is still listening... while n.receive(self.request) == "still here": # ...send a message from the queue # corresponding to this client. n.send(self.request, serialize( server.messages[clientID].get() ))
def handle(self): # socketType refers to whether the socket is a speaker # or listener from the _client's_ point of view. socketType = n.receive(self.request) if socketType == "speak": server.clientIDLock.acquire() clientID = server.clientIDCounter server.clientIDCounter += 1 server.clientIDLock.release() # Tell the client its ID. n.send(self.request, serialize(clientID)) print("[" + self.client_address[0] + " connected as client " + str(clientID) + ".]") # Create a queue corresponding to this client. server.messages.append(Queue.Queue()) while True: serializedMessage = n.receive(self.request) if serializedMessage: message = unserialize(serializedMessage) print("[" + message.__class__.__name__ + ": " + str(message) + "]") assignID(message) server.distributionQueue.put(message) newMessageTree = trees.MessageTree(message) server.baseMessageTree.append(newMessageTree) else: break print "[Client " + str(clientID) + " quit.]" else: assert socketType[0:6] == "listen" clientID = int(unserialize(socketType[6:])) n.send(self.request, serialize(server.baseMessageTree)) # While the client is still listening... while n.receive(self.request) == "still here": # ...send a message from the queue # corresponding to this client. n.send(self.request, serialize(server.messages[clientID].get()))
def writeMessageTreeToFile(filename): file = open(filename, "w") file.write(serialize(server.baseMessageTree)) file.close()
def speak(sock, client): while True: newMessage = client.messageIn.get() n.send(sock, serialize(newMessage))