def cmd_exec_thread_process(self): while self._is_active: if len(self._cmd_buffer) > 0: # Get command info cmd_msg = self._cmd_buffer.pop(0) cmd_text = cmd_msg.get_field('Message') # type:str cmd_text = cmd_text.split(' ', 2) cmd_ref = cmd_text[0] cmd_args = [] if len(cmd_text) > 1: cmd_args = cmd_text[1].split(' ') # Looking for command cmds = self.get_cmd_list() if cmd_ref not in cmds.keys(): # if command not found msg = Message({'Sender': self._uid, 'Receivers': [cmd_msg.get_field('Sender')], 'Type': MESSAGE_MSG_TYPE, 'Message': 'Unable to execute command: Command "%s" not found.' % cmd_ref}) self._send_buffer.append(msg) else: cmd = cmds.get(cmd_ref) # Verify the permission to execute command if self.verify_execution(cmd_msg, cmd): # Execute the command and get the result result = self.execute_cmd(cmd, cmd_args) if not result is None: msg = Message( {'Sender': self._uid, 'Receivers': [cmd_msg.get_field('Sender')], 'Type': MESSAGE_MSG_TYPE, 'Message': result}) self._send_buffer.append(msg)
def test_admin_command(self): fail_msg = 'Permissions test failed' chat = DummyChat() message = Message('steve', 'bar') result = test_cmd(chat, message) admin_message = Message(config.owner, 'foo') admin_result = test_cmd(chat, admin_message) self.assertTrue(admin_result, fail_msg) self.assertFalse(result, fail_msg)
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 distribute_message(self, msg: Message): msg_type = msg.get_field('Type') if msg_type == MESSAGE_MSG_TYPE: self._recv_msg_buffer.append(msg) self.disp_msg(msg) elif msg_type == COMMAND_MSG_TYPE: self._cmd_buffer.append(msg) else: # Unknown type message self._handle_unknow_type_msg(msg)
def test_constructor(self): fail_msg = 'Message constructor failed' author = 'bob' content = 'hi there' m_time = time.time() message = Message(author, content, m_time) self.assertEqual(message.author.name, author, fail_msg) self.assertEqual(message.content, content, fail_msg) self.assertEqual(message.timestamp, m_time, fail_msg)
def _conn_handle_thread_process(self): while self._is_active: # Accept new connection connection, address = self._sock.accept() print('Received connection request from %s' % str(address)) uid = self.assign_uid() if uid is None: print('Connection refused, server is currently full.') msg = repr(Message( {'Sender': SERVER_UID, 'Receivers': [GUEST_UID], 'Type': MESSAGE_MSG_TYPE, 'Message': 'Server is currently full.'})) connection.sendall(msg.encode('utf-8')) connection.close() continue print('Connection approved, assigned uid: [%d]' % uid) # Send welcome information msg_str = repr(Message( {'Sender': SERVER_UID, 'Receivers': [uid], 'Type': MESSAGE_MSG_TYPE, 'Message': 'Welcome, you are now connected.'})) connection.sendall(msg_str.encode('utf-8')) self._add_user(uid, connection)
def _parse_message(raw_message): ''' Parses raw message from server and returns a Message object. :param raw_message: UTF-8 encoded message from server. ''' result = re.search('^:(\\w+)!\\w+@[\\w.]+ [A-Z]+ #\\w+ :(.+)\\r\\n', raw_message) if not result: return author, content = result.groups() author = User(author) return Message(author, content)
def disp_msg(self, msg: Message) -> None: sender = self._users.get(msg.get_field('Sender')) nick = '' if sender is not None and self._users.get(sender) is not None: nick = self._users.get(sender) print('[%d]%s: %s' % (sender, nick, msg.get_field('Message'))) # Expected
def _handle_unknow_type_msg(self, msg: Message): if not msg.get_field('Type') in self._unknow_type_msg_storage.keys(): self._unknow_type_msg_storage[msg.get_field('Type')] = [msg] else: self._unknow_type_msg_storage.get(msg.get_field('Type')).append(msg)
def test_serialize(self): message = Message('frank', 'howdy') ser = message.serialize() other = Message.deserialize(ser) self.assertEqual(message, other, 'Message serialization failed')
def add_message(self, title, text, author): new_message = Message(title, text, author) self.messages.append(new_message)