def listen(self): self._bind() self.base_sock.listen(1) client_socket, addr = self.base_sock.accept() LOG.debug("Got a connection from %s" % (str(addr))) self.sock = client_socket self.connected = True
def _handle_ehlo(self, message): """ _handle_ehlo is a message handler for "ehlo" message types. :param message: Message object that we have built from the socket. :type message: Message """ LOG.debug("Presence From %s@%s" % (message.user, message.hostname)) self.userConnected.emit(message.user, message.hostname)
def _handle_start_chat_server(self, message): """ _handle_start_chat_server is a message handler for "start_chat_server" message types. :param message: Message object that we have built from the socket. :type message: Message """ LOG.debug("%s wants to chat. Chat Server requested" % message.user) self.newChatServerRequested.emit(message.user, message.hostname)
def _handle_start_chat_client(self, message): """ _handle_start_chat_client is a message handler for "start_chat_client" message types. :param message: Message object that we have built from the socket. :type message: Message """ intended_user, port = message.message.split(Message.SEP) LOG.debug("%s has setup a server for %s to connect to at port %s" % ( message.user, intended_user, port )) if intended_user == self.user: self.newChatClientRequested.emit(message.user, message.hostname, port)
def handle_message(self, message): """ handle_message is the where we check to see if the message object we received has a handler that can accept it. :param message: Message object that we have built from the socket. :type message: Message """ message_type = MessageType.TO_NAMES.get(message.message_type, None) if message_type: if hasattr(self, self.handler_template.format(mtype=message_type)): method = getattr(self, self.handler_template.format(mtype=message_type)) LOG.debug("Calling %s" % method) return method(message) LOG.debug( "Received unknown message from \"{u}\": \"{host}\"\n" "{mtype}: \"{msg}\"".format( u=message.user, host=message.hostname, msg=message.message, mtype=message.message_type ) )
def run_forever(self): while not self.stop: data, (host, port) = self.sock.recvfrom(1024) try: message = Message.from_str(data) LOG.debug("Recieved message from %s" % message.user) if message.user == self.user: LOG.debug("Message from self. Ignoring") continue self.handle_message(message) except ChatAppMessageError as err: LOG.error("ERROR: \"{err}\"".format(err=err)) LOG.debug("Listener exitting")
def sendMessage(self, message): if self.sock is None: self.setup_socket() try: LOG.debug("Sending message to %s:%s" % (self.addr, self.port)) self.sock.sendto(message.to_str(), (self.addr, self.port)) except Exception as err: LOG.debug(traceback.format_exc()) LOG.warning("Could not send message to %s:%s - \"%s\"" % (self.addr, self.port, message.to_str()))
def run(self): if self.mode == SocketChat.SERVER and (self.base_sock is self.sock or not self.connected): self.listen() elif self.mode == SocketChat.CLIENT and not self.connected: self.connect(self.host, self.port) while not self.stop: data, (host, port) = self.sock.recvfrom(1024) try: message = Message.from_str(data) self.recieved_message.emit(message) except ChatAppMessageError as err: LOG.debug("ERROR: \"{err}\"".format(err=err)) while len(self.send_message_queue): message = self.send_message_queue.pop() LOG.debug("Sending message \"%s\"" % message) self.sock.send(message) self.sent_message.emit(message) LOG.debug("Server exiting")