Exemple #1
0
class ActionBase(metaclass=ABCMeta):
    errors = {}

    def __init__(self, message):
        self.result = Message()
        self.error = False
        self.msg = message
        self.user = None

        self.set_basic_params()
        self.db_connect = DatabaseConnection()

    def set_basic_params(self):
        self.result.add_header_param(MESSAGE_ACTION, self.get_action_number())

    @abstractmethod
    def get_action_number(self):
        pass

    @abstractmethod
    def execute(self, *args, **kwargs):
        pass

    def set_error_with_status(self, status):
        self.error = True
        self.result.add_body_param(MESSAGE_STATUS, status)

    def set_status_ok(self):
        self.result.add_body_param(MESSAGE_STATUS, MESSAGE_STATUS_OK)

    def get_action_result(self):
        return self.result

    def get_error(self):
        return self.error
Exemple #2
0
    def send_message(self, client_list, current_username):
        from xcomm.message import Message
        message = Message(body=self.msg.body)
        message.add_header_param(MESSAGE_ACTION,
                                 MESSAGE_ACTION_RECVMESSAGE_CODE)

        # Adding info about user and current date&time
        message.add_body_param(MESSAGE_ACTION_RECVMESSAGE_USER,
                               current_username)
        time_now = datetime.datetime.now()
        message.add_body_param(MESSAGE_ACTION_RECVMESSAGE_TIMESTAMP,
                               time_now.timestamp())

        logger.debug("Directing message to all users in room: " +
                     message.get_complete_message().replace('\0', '\n'))

        message = message.convert_message_to_bytes()

        for client in client_list:
            client.client_socket.sendall(message)