Пример #1
0
    def transmit(self, frame):
        """
        Convert a frame object to a frame string and transmit to the server.

        :param Frame frame: the Frame object to transmit
        """
        with self.__listeners_change_condition:
            listeners = list(self.listeners.values())

        for listener in listeners:
            if not listener:
                continue
            try:
                listener.on_send(frame)
            except AttributeError:
                continue

        lines = utils.convert_frame_to_lines(frame)

        packed_frame = pack(lines)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Sending frame: %s", lines)
        else:
            log.info("Sending frame: %r, headers=%r", frame.cmd or "heartbeat",
                     utils.clean_headers(frame.headers))

        self.send(encode(packed_frame))
Пример #2
0
    def send(self,
             destination,
             body,
             content_type=None,
             headers=None,
             **keyword_headers):
        """
        Send a message to a destination in the messaging system (as per https://stomp.github.io/stomp-specification-1.2.html#SEND)

        :param str destination: the destination (such as a message queue - for example '/queue/test' - or a message topic)
        :param body: the content of the message
        :param str content_type: the MIME type of message
        :param dict headers: additional headers to send in the message frame
        :param keyword_headers: any additional headers the broker requires
        """
        assert destination is not None, "'destination' is required"
        assert body is not None, "'body' is required"
        headers = utils.merge_headers([headers, keyword_headers])
        headers[HDR_DESTINATION] = destination
        if content_type:
            headers[HDR_CONTENT_TYPE] = content_type
        body = encode(body)
        if self.auto_content_length and body and HDR_CONTENT_LENGTH not in headers:
            headers[HDR_CONTENT_LENGTH] = len(body)
        self.send_frame(CMD_SEND, headers, body)
Пример #3
0
    def transmit(self, frame):
        """
        Convert a frame object to a frame string and transmit to the server.

        :param frame: the Frame object to transmit
        """
        for listener in self.listeners.values():
            if not listener:
                continue
            try:
                listener.on_send(frame)
            except AttributeError:
                continue

        lines = utils.convert_frame_to_lines(frame)

        packed_frame = pack(lines)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Sending frame %s", lines)
        else:
            log.info("Sending frame cmd=%r headers=%r", frame.cmd,
                     frame.headers)

        self.send(encode(packed_frame))
Пример #4
0
    def send(self,
             destination,
             body,
             content_type=None,
             headers=None,
             **keyword_headers):
        """
        Send a message to a destination.

        :param str destination: the destination of the message (e.g. queue or topic name)
        :param body: the content of the message
        :param str content_type: the content type of the message
        :param dict headers: a map of any additional headers the broker requires
        :param keyword_headers: any additional headers the broker requires
        """
        assert destination is not None, "'destination' is required"
        assert body is not None, "'body' is required"
        headers = utils.merge_headers([headers, keyword_headers])
        headers[HDR_DESTINATION] = destination
        if content_type:
            headers[HDR_CONTENT_TYPE] = content_type
        body = encode(body)
        if self.auto_content_length and body and HDR_CONTENT_LENGTH not in headers:
            headers[HDR_CONTENT_LENGTH] = len(body)
        self.send_frame(CMD_SEND, headers, body)
Пример #5
0
    def transmit(self, frame):
        """
        Convert a frame object to a frame string and transmit to the server.
        """
        for listener in self.listeners.values():
            if not listener: continue
            if not hasattr(listener, 'on_send'):
                continue
            listener.on_send(frame)

        lines = utils.convert_frame_to_lines(frame)

        packed_frame = pack(lines)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Sending frame %s", lines)
        else:
            log.info("Sending frame cmd=%r headers=%r", frame.cmd, frame.headers)

        if self.socket is not None:
            try:
                self.__socket_semaphore.acquire()
                try:
                    self.send_over_socket(encode(packed_frame))
                finally:
                    self.__socket_semaphore.release()
            except Exception:
                _, e, _ = sys.exc_info()
                log.error("Error sending frame", exc_info=1)
                raise e
        else:
            raise exception.NotConnectedException()
Пример #6
0
 def send(self, destination, body, content_type=None, headers={}, **keyword_headers):
     assert destination is not None, "'destination' is required"
     assert body is not None, "'body' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_DESTINATION] = destination
     if content_type:
         headers[HDR_CONTENT_TYPE] = content_type
     body = encode(body)
     #if HDR_CONTENT_LENGTH not in headers:
     #    headers[HDR_CONTENT_LENGTH] = len(body)
     self.send_frame(CMD_SEND, headers, body)
Пример #7
0
def convert_frame(frame, body_encoding=None):
    """
    Convert a frame to a list of lines separated by newlines.

    :param Frame frame: the Frame object to convert

    :rtype: list(str)
    """
    lines = []

    body = None
    if frame.body:
        if body_encoding:
            body = encode(frame.body, body_encoding)
        else:
            body = encode(frame.body)

        if HDR_CONTENT_LENGTH in frame.headers:
            frame.headers[HDR_CONTENT_LENGTH] = len(body)

    if frame.cmd:
        lines.append(encode(frame.cmd))
        lines.append(ENC_NEWLINE)
    for key, vals in sorted(frame.headers.items()):
        if vals is None:
            continue
        if type(vals) != tuple:
            vals = (vals,)
        for val in vals:
            lines.append(encode("%s:%s\n" % (key, val)))
    lines.append(ENC_NEWLINE)
    if body:
        lines.append(body)

    if frame.cmd:
        lines.append(ENC_NULL)
    return lines
Пример #8
0
 def send(self,
          destination,
          body,
          content_type=None,
          headers={},
          **keyword_headers):
     assert destination is not None, "'destination' is required"
     assert body is not None, "'body' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_DESTINATION] = destination
     if content_type:
         headers[HDR_CONTENT_TYPE] = content_type
     body = encode(body)
     if body and HDR_CONTENT_LENGTH not in headers:
         headers[HDR_CONTENT_LENGTH] = len(body)
     self.send_frame(CMD_SEND, headers, body)
Пример #9
0
 def send(self, destination, body, content_type=None, headers={}, **keyword_headers):
     """
     Send a message to a destination in the messaging system (as per https://stomp.github.io/stomp-specification-1.2.html#SEND)
     
     :param destination: the destination (such as a message queue - for example '/queue/test' - or a message topic)
     :param body: the content of the message
     :param content_type: the MIME type of message 
     :param headers: additional headers to send in the message frame
     """
     assert destination is not None, "'destination' is required"
     assert body is not None, "'body' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_DESTINATION] = destination
     if content_type:
         headers[HDR_CONTENT_TYPE] = content_type
     body = encode(body)
     if body and HDR_CONTENT_LENGTH not in headers:
         headers[HDR_CONTENT_LENGTH] = len(body)
     self.send_frame(CMD_SEND, headers, body)
Пример #10
0
    def send(self, destination, body, content_type=None, headers=None, **keyword_headers):
        """
        Send a message to a destination.

        :param str destination: the destination of the message (e.g. queue or topic name)
        :param body: the content of the message
        :param str content_type: the content type of the message
        :param dict headers: a map of any additional headers the broker requires
        :param keyword_headers: any additional headers the broker requires
        """
        assert destination is not None, "'destination' is required"
        assert body is not None, "'body' is required"
        headers = utils.merge_headers([headers, keyword_headers])
        headers[HDR_DESTINATION] = destination
        if content_type:
            headers[HDR_CONTENT_TYPE] = content_type
        body = encode(body)
        if self.auto_content_length and body and HDR_CONTENT_LENGTH not in headers:
            headers[HDR_CONTENT_LENGTH] = len(body)
        self.send_frame(CMD_SEND, headers, body)
Пример #11
0
    def transmit(self, frame):
        """
        Convert a frame object to a frame string and transmit to the server.
        """
        for listener in self.listeners.values():
            if not listener: continue
            if not hasattr(listener, 'on_send'):
                continue
            listener.on_send(frame)

        lines = utils.convert_frame_to_lines(frame)

        packed_frame = pack(lines)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Sending frame %s", lines)
        else:
            log.info("Sending frame cmd=%r headers=%r", frame.cmd, frame.headers)

        self.send(encode(packed_frame))
Пример #12
0
    def transmit(self, frame):
        """
        Convert a frame object to a frame string and transmit to the server.

        :param Frame frame: the Frame object to transmit
        """
        for listener in self.listeners.values():
            if not listener:
                continue
            try:
                listener.on_send(frame)
            except AttributeError:
                continue

        lines = utils.convert_frame_to_lines(frame)

        packed_frame = pack(lines)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Sending frame: %s", lines)
        else:
            log.info("Sending frame: %r, headers=%r", frame.cmd or "heartbeat", frame.headers)

        self.send(encode(packed_frame))
Пример #13
0
 def send(self, msg):
     if not msg.endswith('\x00'):
         msg = msg + '\x00'
     self.conn.sendall(backward.encode(msg))
Пример #14
0
##
# As of STOMP 1.2, lines can end with either line feed, or carriage return plus line feed.
#
PREAMBLE_END_RE = re.compile(b'\n\n|\r\n\r\n')

##
# As of STOMP 1.2, lines can end with either line feed, or carriage return plus line feed.
#
LINE_END_RE = re.compile('\n|\r\n')

##
# Used to replace the "passcode" to be dumped in the transport log (at debug level)
#
PASSCODE_RE = re.compile(r'\'passcode:[^\']*\'')

ENC_NEWLINE = encode("\n")
ENC_NULL = encode(NULL)

def default_create_thread(callback):
    """
    Default thread creation - used to create threads when the client doesn't want to provide their
    own thread creation.

    :param function callback: the callback function provided to threading.Thread
    """
    thread = threading.Thread(None, callback)
    thread.daemon = True  # Don't let thread prevent termination
    thread.start()
    return thread