Ejemplo n.º 1
0
    def work_with_chat(self, client, mess):
        room = mess.get('room')
        server_logger.debug(f'check is chat {room} exist')

        if not room:
            server_logger.error('in message with action join has not room name')
            self.send_mess(client, server_responce.wrong_request())
            return False
        else:
            server_logger.debug('check is room in chart list')
            if room not in self._chats_list:
                server_logger.debug(f'room with name {room} dose not exist in chats list, now that room been created')
                self._chats_list[room] = [client, ]
                server_logger.debug(f'now chats list is {self._chats_list}')
                server_logger.debug(f'sending accept required message to client {client}')
                self.send_mess(client, server_responce.accept_required(room))
                return True
            else:
                server_logger.debug(f'check is client in room {room}')
                if client not in self._chats_list[room]:
                    server_logger.debug('client not in room, now added')
                    self._chats_list[room].append(client)
                    self.send_mess(client, server_responce.accept_required(room))
                    return True
                else:
                    server_logger.error('client was in chat room')
                    self.send_mess(client, server_responce.client_alredy_connected())
                    return True
Ejemplo n.º 2
0
 def check_client_mess(self, my_client, client_address, mess):
     """check is client message. If client message is empty, disconect client"""
     if not mess:
         server_logger.debug('client message is empty, run disconnecting from client')
         server_logger.info(f'connecting to {client_address} is failed')
         self.disconnect_client(my_client, client_address, server_responce.wrong_request())
         return False
     else:
         server_logger.info(f'connecting to {client_address} is successful')
         return True
Ejemplo n.º 3
0
 def send_message_to_group(self,client, message, group_name):
     server_logger.debug(f'find chat with name: {group_name}')
     if group_name in self._chats_list:
         server_logger.debug(f'group name: {group_name} in chat list, start message sending loop')
         for chat_client in self._chats_list[group_name]:
             server_logger.debug(f'sending message for client of chat: {chat_client}')
             self.send_mess(chat_client, json.dumps(message))
         return True
     else:
         server_logger.debug(f'chat with name {group_name} not registered, send to client wrong request response')
         self.send_mess(client, server_responce.wrong_request(f' chat with name {group_name} not exist'))
         return False
Ejemplo n.º 4
0
    def client_send_message(self, message, client):
        server_logger.debug('get addressees of message')
        to_user = message.get('to')
        server_logger.debug('check addressees is exist')
        if not to_user:
            server_logger.debug('addressees is not exist, send to client wrong request response')
            self.send_mess(client, server_responce.wrong_request(' addressees in not exist'))
            return False

        server_logger.debug('addressees is group chat or user')
        if to_user[0] == '#':
            server_logger.debug('addressees is group chat')
            result = self.send_message_to_group(client, message, to_user[1:])
            return result
        else:
            server_logger.debug('addressees is user')
            return True
Ejemplo n.º 5
0
    def work_whith_client_mess(self, mess, my_client, client_address):
        """gets client action and starts match function"""
        server_logger.debug('try get parameter action')
        try:
            action = mess["action"]
        except KeyError:
            server_logger.warning(f'client message {mess} has not parameter action')
            server_logger.debug('run disconnect client')
            self.disconnect_client(my_client, None, server_responce.wrong_request())
            return False

        else:
            server_logger.debug('client message has parameter action, comparison it with "presence" and "quit"')
            if action == "presence":
                server_logger.debug('value of action parameter is "presence", run processing of presence message')

            elif action == "quit":
                server_logger.info('client request disconnect from server')
                server_logger.debug('value of action parameter is "quit", run disconnecting')
                self.disconnect_client(my_client, client_address)
                return "quit"

            elif action == "join":
                server_logger.info('client request join to chat room')
                result = self.work_with_chat(my_client, mess)
                if result:
                    return "join"
                else:
                    return None

            elif action == "msg":
                server_logger.info('client send message')
                result = self.client_send_message(mess, my_client)
                if not result:
                    return None
                else:
                    return "msg"