Esempio n. 1
0
    def deserialize(self, raw_message):
        """Deserializes a message

        Args:
            raw_message (str): serialized message

        Returns:
            Message: new instance of the message
        """
        logger.debug("Deserializing message...")
        try:
            topic, uuid, timestamp, data_type, data = raw_message.split(
                self.delimiter
            )

            if data_type != self.serializer.data_type:
                raise InvalidMessageError(
                    u"Invalid data_type '{}', "
                    "expected: '{}'".format(
                        data_type, self.data_type
                    )
                )

            data = self.serializer.deserialize(data)
        except Exception as e:
            raise InvalidMessageError(u"Invalid message: {}".format(e))
        else:
            logger.debug("Message '{}' deserialized".format(uuid))
            return self.__class__(
                topic=topic,
                uuid=uuid,
                timestamp=timestamp,
                data_type=data_type,
                data=data,
            )
Esempio n. 2
0
    def receive(self):
        """Returns a single message from the publishers

        Returns:
            Message: deserialized message
        """
        logger.debug("Waiting for requests...")
        request = self.socket.recv()
        return messages.parse(raw_message=request)
Esempio n. 3
0
    def respond(self, response):
        """Respond to a request

        Args:
            request (Message): Request to respond to
        """
        logger.debug("Sending response '{}'".format(response.uuid))
        self.socket.send(response.serialize())
        logger.debug("Response '{}' sent".format(response.uuid))
Esempio n. 4
0
    def save(self, file_name=None):
        self.set_file_path(file_name)

        logger.debug("Saving message '{}' data to '{}'".format(
            self.uuid, self.file_path
        ))

        with open(self.file_path, 'wb') as destination:
            destination.write(self.data)

        logger.debug("Message '{}' data saved".format(self.uuid))
Esempio n. 5
0
    def serialize(self):
        """Returns serialized message

        Returns:
            str: serialized message
        """
        logger.debug("Serializing message '{}'".format(self.uuid))

        serialized_data = self.serializer.serialize(self.data)
        self.validate_serialized_data(serialized_data)

        return "{topic}{d}{uuid}{d}{timestamp}{d}{data_type}{d}{data}".format(
            d=self.delimiter,
            topic=self.topic,
            uuid=self.uuid,
            timestamp=self.timestamp,
            data_type=self.serializer.data_type,
            data=serialized_data
        )
Esempio n. 6
0
def parse(raw_message):
    """Parses a raw message and returns a Message instance of correct type

    Args:
        raw_message (str): serialized message

    Returns:
        Message: deserialized message
    """
    logger.debug("Deserializing message...")
    try:
        topic, uuid, timestamp, data_type, data = raw_message.split(
            Message.delimiter
        )
    except Exception as e:
        raise InvalidMessageError(u"Invalid message: {}".format(e))
    else:
        if data_type not in MESSAGE_FOR_DATA_TYPE:
            raise InvalidMessageError(
                u"Invalid message data_type '{}', "
                "allowed data types: '{}'".format(
                    data_type, ", ".join(MESSAGE_FOR_DATA_TYPE.keys())
                )
            )

        message = MESSAGE_FOR_DATA_TYPE[data_type]
        data = message.serializer.deserialize(data)

        logger.debug("Message '{}' deserialized".format(uuid))

        return message(
            topic=topic,
            uuid=uuid,
            timestamp=timestamp,
            data_type=data_type,
            data=data,
        )
Esempio n. 7
0
    def request(self, message):
        """Returns a single message from the servers

        Returns:
            Message: deserialized message
        """
        logger.debug("Sending request...")
        self.socket.send(message.serialize())
        logger.debug("Getting response...")
        response = self.socket.recv()
        logger.debug("Processing response...")
        return self.process_response(response)
Esempio n. 8
0
 def process_response(self, response):
     logger.debug("Processing a response...")
     return messages.parse(raw_message=response)
Esempio n. 9
0
 def validate(self):
     logger.debug("Validating message '{}'".format(self.uuid))
     try:
         self.serialize()
     except Exception as e:
         raise InvalidMessageError(u"Invalid message: {}".format(e))