Ejemplo n.º 1
0
    def response(
            self,
            request_msg: HorseMessage,
            status: ResultCode,
            response_content: str = None,
            additional_headers: List[MessageHeader] = None) -> HorseResult:
        """
        Sends a response message to a request
        :param request_msg: Request message
        :param status: Response message status
        :param response_content: Response message content
        :param additional_headers: Additional message headers
        :return: Returns ok, if sent successfully
        """

        msg = HorseMessage()
        msg.type = MessageType.Response
        msg.content_type = status.value
        msg.message_id = request_msg.message_id
        msg.high_priority = request_msg.high_priority
        msg.first_acquirer = request_msg.first_acquirer

        if request_msg.type == MessageType.QueueMessage.value:
            msg.target = request_msg.target
        else:
            msg.target = request_msg.source

        if not response_content is None:
            msg.set_content(response_content)

        return self.send(msg, additional_headers)
Ejemplo n.º 2
0
    async def publish_router(
            self,
            router: str,
            content_type: int,
            message: str,
            wait_ack: bool,
            additional_headers: List[MessageHeader] = None) -> HorseResult:
        """
        Publishes a message to a router
        :param router: Router name
        :param content_type: Message content type. It's used in router. Real queue content type may be different (if overwritten in server)
        :param message: String message content
        :param wait_ack: If true, waits for acknowledge
        :param additional_headers: Additional message headers
        :return: If operation successful, returns Ok
        """

        msg = HorseMessage()
        msg.type = MessageType.Router
        msg.content_type = router
        msg.target = content_type

        msg.set_content(message)
        if wait_ack:
            msg.pending_acknowledge = True
            return await self.send_get_ack(msg, additional_headers)
        else:
            msg.pending_acknowledge = False
            return self.send(msg, additional_headers)
Ejemplo n.º 3
0
    async def send_direct(
            self,
            target: str,
            content_type: int,
            message: str,
            wait_ack: bool,
            additional_headers: List[MessageHeader] = None) -> HorseResult:
        """
        Sends a direct message to a receiver
        :param target: Unique Id of the client or @name:name_of_client or @type:type_of_client
        :param content_type: Message content type
        :param message: String message content
        :param wait_ack: If true, message will wait for acknowledge
        :param additional_headers: Additional message headers
        :return:
        """

        msg = HorseMessage()
        msg.type = MessageType.DirectMessage
        msg.content_type = content_type
        msg.target = target

        msg.set_content(message)
        if wait_ack:
            msg.pending_acknowledge = True
            return await self.send_get_ack(msg, additional_headers)
        else:
            msg.pending_acknowledge = False
            return self.send(msg, additional_headers)
Ejemplo n.º 4
0
    async def push_queue(
            self,
            channel: str,
            queue: int,
            message: str,
            wait_ack: bool,
            additional_headers: List[MessageHeader] = None) -> HorseResult:
        """
        Pushes a message into a queue
        :param channel: Channel name of the queue
        :param queue: Queue Id
        :param message: String message content
        :param wait_ack: If true, waits for acknowledge
        :param additional_headers: Additional message headers
        :return: If operation successful, returns Ok
        """

        msg = HorseMessage()
        msg.type = MessageType.QueueMessage
        msg.content_type = queue
        msg.target = channel

        msg.set_content(message)
        if wait_ack:
            msg.pending_acknowledge = True
            return await self.send_get_ack(msg, additional_headers)
        else:
            msg.pending_acknowledge = False
            return self.send(msg, additional_headers)
Ejemplo n.º 5
0
    def __handshake(self) -> bool:
        """
        Sends HMQP handshake message and reads handshake response.
        :returns true if handshake is successful
        """

        self.__socket.sendall("HMQP/2.1".encode('UTF-8'))

        # create handshake message properties
        content = 'CONNECT /\r\n'
        if self.id:
            content += HorseHeaders.create_line(HorseHeaders.CLIENT_ID,
                                                self.id)
        if self.name:
            content += HorseHeaders.create_line(HorseHeaders.CLIENT_NAME,
                                                self.name)
        if self.type:
            content += HorseHeaders.create_line(HorseHeaders.CLIENT_TYPE,
                                                self.type)
        if self.token:
            content += HorseHeaders.create_line(HorseHeaders.CLIENT_TOKEN,
                                                self.token)

        if self.headers:
            for h in self.headers:
                content += HorseHeaders.create_line(h.key, h.value)

        msg = HorseMessage()
        msg.type = MessageType.Server
        msg.content_type = KnownContentTypes.HELLO.value
        msg.set_content(content)
        sent = self.send(msg)
        if not sent:
            return False

        hr_result = self.__read_certain(8)
        if hr_result is None:
            return False

        hs_response = hr_result.decode('UTF-8')
        return hs_response == "HMQP/2.1"