def idle(self): """ Require connected """ if not self.connected: raise NotConnectedException t = thread.start_new_thread(self.send, ()) try: while True: data = self.sock.recv(1024) if len(data) == 0: #TODO CHANGE THIS raise NotConnectedException type = Packet.get_type(data) if type == PKT_MESSAGE: user, msg = MessagePacket(data=data, key=self.__key).unpack() self.ui.new_msg("[{0}] {1}".format(user, msg)) elif type == PKT_CONNECTED: user = ConnectedPacket(data=data).unpack() self.ui.new_msg("{0} just appeared".format(user)) elif type == PKT_DISCONNECTED: user = DisconnectedPacket(data=data).unpack() self.ui.new_msg("{0} ran away".format(user)) else: raise NotImplemented except NotConnectedException: self.disconnect()
def recved(self, cid, user, data): """ Handler for the reception of a packet from user """ type = Packet.get_type(data) if type == PKT_QUIT: self.cmptr[cid].disconnect(user) return True elif type == PKT_MESSAGE: msg = MessagePacket(data=data) try: # Follow packet to relevant comptoir self.cmptr[cid].new_msg(msg, user) except InvalidHashException: # TODO this should not be sent by chat object ... # TODO this should be a packed message user.sock.send("Message rejected: invalid hash.\n") return False else: # TODO handle properly this scenario raise NotImplemented