def recv(self) -> Message or None: data = self.recv_raw() try: return Message.parse_str(data) except SyntaxError as e: print('Error occurred while parsing message from server.') print('Raw data received: \n%s' % data) return None
def process_input(self, s: str): # type: (str) -> None # if input is a command, execute it if s.startswith(COMMAND_PREFIX): # Generate a new command message and insert to local buffer self._cmd_buffer.insert(0, Message( {'Sender': self._uid, 'Receivers': [self._uid], 'Type': COMMAND_MSG_TYPE, 'Message': s[1:]})) else: # if input can be parsed to a Message object, parse and send it try: self._send_buffer.append(Message.parse_str(s)) except SyntaxError as e: # if cannot be parsed, send it as normal message msg = Message( {'Sender': self._uid, 'Receivers': [], 'Type': MESSAGE_MSG_TYPE, 'Message': s}) self._send_buffer.append(msg)
def recv(self): # type: ()-> Message or None data = self.recv_raw() try: msg = Message.parse_str(data) except SyntaxError as e: if self.is_connected(): # Ignore the error if user is not connected print('Error occurred while parsing received data from user %s[%d]' % ( self.get_nick(), self.get_uid())) print('Raw data received:\n%s' % str(data)) return None if not msg.get_field('Sender') == self.get_uid(): print( 'Error occurred while verifying message from user %s[%d]: Illegal Sender' % ( self.get_nick(), self.get_uid())) # Send notice to user self.send( Message({'Sender': self._server.get_uid(), 'Receivers': [self.get_uid()], 'Type': MESSAGE_MSG_TYPE, 'Message': 'Error occurred while verifying your message: Sender is not you'})) return None else: return msg