Exemple #1
0
    def connect(self, username=None, passcode=None, wait=False, headers={}, **keyword_headers):
        """
        Send a STOMP CONNECT frame. Differs from 1.0 and 1.1 versions in that the HOST header is enforced.
        \param username
            optionally specify the login user
        \param passcode
            optionally specify the user password
        \param wait
            wait for the connection to complete before returning
        """
        cmd = CMD_STOMP
        headers = utils.merge_headers([headers, keyword_headers])
        headers[HDR_ACCEPT_VERSION] = self.version
        headers[HDR_HOST] = self.transport.current_host_and_port[0]
        
        if self.transport.vhost:
            headers[HDR_HOST] = self.transport.vhost

        if username is not None:
            headers[HDR_LOGIN] = username

        if passcode is not None:
            headers[HDR_PASSCODE] = passcode

        self.__send_frame(cmd, headers)
        
        if wait:
            self.transport.wait_for_connection()
            if self.connection_error:
                raise ConnectFailedException()
Exemple #2
0
    def connect(self, headers={}, **keyword_headers):
        """
        Send a CONNECT frame to start a connection
        """
        wait = False
        if 'wait' in keyword_headers and keyword_headers['wait']:
            wait = True
            del keyword_headers['wait']

        if self.version >= 1.1:
            if self.__strict:
                cmd = 'STOMP'
            else:
                cmd = 'CONNECT'
            headers['accept-version'] = self.version
            headers['heart-beat'] = '%s,%s' % self.heartbeats
        else:
            cmd = 'CONNECT'
        self.__send_frame_helper(
            cmd, '',
            utils.merge_headers(
                [self.__connect_headers, headers, keyword_headers]), [])
        if wait:
            self.__connect_wait_condition.acquire()
            while not self.is_connected():
                self.__connect_wait_condition.wait()
            self.__connect_wait_condition.release()
Exemple #3
0
 def ack(self, headers={}, **keyword_headers):
     """
     Send an ACK frame, to acknowledge receipt of a message
     """
     self.__send_frame_helper(
         'ACK', '', utils.merge_headers([headers, keyword_headers]),
         ['message-id'])
Exemple #4
0
 def connect(self, headers={}, **keyword_headers):
     """
     Send a CONNECT frame to start a connection
     """
     wait = False
     if 'wait' in keyword_headers and keyword_headers['wait']:
         wait = True
         del keyword_headers['wait']
         
     if self.version >= 1.1:
         if self.__strict:
             cmd = 'STOMP'
         else:
             cmd = 'CONNECT'
         if self.vhost is not None:
             headers['host'] = self.vhost
         headers['accept-version'] = self.version
         headers['heart-beat'] = '%s,%s' % self.heartbeats
     else:
         cmd = 'CONNECT'
     self.__send_frame_helper(cmd, '', utils.merge_headers([self.__connect_headers, headers, keyword_headers]), [ ])
     if wait:
         self.__connect_wait_condition.acquire()
         while not self.is_connected(): 
             self.__connect_wait_condition.wait()
         self.__connect_wait_condition.release()
Exemple #5
0
 def disconnect(self, headers={}, **keyword_headers):
     """
     Send a DISCONNECT frame to finish a connection
     """
     self.__send_frame_helper('DISCONNECT', '', utils.merge_headers([self.__connect_headers, headers, keyword_headers]), [ ])
     self.__running = False
     if self.__socket is not None:
         if self.__ssl:
             #
             # Even though we don't want to use the socket, unwrap is the only API method which does a proper SSL shutdown
             #
             try:
                 self.__socket = self.__socket.unwrap()
             except Exception:
                 #
                 # unwrap seems flaky on Win with the backported ssl mod, so catch any exception and log it
                 #
                 _, e, _ = sys.exc_info()
                 log.warn(e)
         elif hasattr(socket, 'SHUT_RDWR'):
             self.__socket.shutdown(socket.SHUT_RDWR)
     #
     # split this into a separate check, because sometimes the socket is nulled between shutdown and this call
     #
     if self.__socket is not None:
         self.__socket.close()
     self.__current_host_and_port = None
Exemple #6
0
 def disconnect(self, headers={}, **keyword_headers):
     """
     Send a DISCONNECT frame to finish a connection
     """
     self.__send_frame_helper(
         'DISCONNECT', '',
         utils.merge_headers(
             [self.__connect_headers, headers, keyword_headers]), [])
     self.__running = False
     if self.__socket is not None:
         if self.__ssl:
             #
             # Even though we don't want to use the socket, unwrap is the only API method which does a proper SSL shutdown
             #
             try:
                 self.__socket = self.__socket.unwrap()
             except Exception:
                 #
                 # unwrap seems flaky on Win with the backported ssl mod, so catch any exception and log it
                 #
                 _, e, _ = sys.exc_info()
                 log.warn(e)
         elif hasattr(socket, 'SHUT_RDWR'):
             self.__socket.shutdown(socket.SHUT_RDWR)
     #
     # split this into a separate check, because sometimes the socket is nulled between shutdown and this call
     #
     if self.__socket is not None:
         self.__socket.close()
     self.__current_host_and_port = None
Exemple #7
0
    def send(self, message='', headers={}, **keyword_headers):
        """
        Send a message (SEND) frame
        """
        if '\x00' in message:
            content_length_headers = {'content-length': len(message)}
        else:
            content_length_headers = {}

        merged_headers = utils.merge_headers(
            [headers, keyword_headers, content_length_headers])

        if self.__wait_on_receipt and 'receipt' in merged_headers.keys():
            self.__send_wait_condition.acquire()

        self.__send_frame_helper('SEND', message, merged_headers,
                                 ['destination'])
        self.__notify('send', headers, message)

        # if we need to wait-on-receipt, then block until the receipt frame arrives
        if self.__wait_on_receipt and 'receipt' in merged_headers.keys():
            receipt = merged_headers['receipt']
            while receipt not in self.__receipts:
                self.__send_wait_condition.wait()
            del self.__receipts[receipt]
            self.__send_wait_condition.release()
Exemple #8
0
 def subscribe(self, headers={}, **keyword_headers):
     """
     Send a SUBSCRIBE frame to subscribe to a queue
     """
     self.__send_frame_helper(
         'SUBSCRIBE', '', utils.merge_headers([headers, keyword_headers]),
         ['destination'])
Exemple #9
0
 def unsubscribe(self, headers={}, **keyword_headers):
     """
     Send an UNSUBSCRIBE frame to unsubscribe from a queue
     """
     self.__send_frame_helper(
         'UNSUBSCRIBE', '', utils.merge_headers([headers, keyword_headers]),
         [('destination', 'id')])
Exemple #10
0
 def begin(self, transaction=None, headers={}, **keyword_headers):
     headers = utils.merge_headers([headers, keyword_headers])
     if not transaction:
         transaction = str(uuid.uuid4())
     headers[HDR_TRANSACTION] = transaction
     self.__send_frame(CMD_BEGIN, headers)
     return transaction
Exemple #11
0
 def nack(self, headers={}, **keyword_headers):
     """
     Send an NACK frame, to acknowledge a message was not successfully processed
     """
     if self.version < 1.1:
         raise RuntimeError('NACK is not supported with 1.0 connections')
     self.__send_frame_helper('NACK', '', utils.merge_headers([headers, keyword_headers]), [ 'message-id' ])
Exemple #12
0
 def nack(self, headers={}, **keyword_headers):
     """
     Send an NACK frame, to acknowledge a message was not successfully processed
     """
     if self.version < 1.1:
         raise RuntimeError('NACK is not supported with 1.0 connections')
     self.__send_frame_helper('NACK', '', utils.merge_headers([headers, keyword_headers]), [ 'message-id' ])
Exemple #13
0
 def commit(self, headers={}, **keyword_headers):
     """
     Send a COMMIT frame to commit a transaction (send pending messages)
     """
     self.__send_frame_helper(
         'COMMIT', '', utils.merge_headers([headers, keyword_headers]),
         ['transaction'])
Exemple #14
0
 def abort(self, headers={}, **keyword_headers):
     """
     Send an ABORT frame to rollback a transaction
     """
     self.__send_frame_helper(
         'ABORT', '', utils.merge_headers([headers, keyword_headers]),
         ['transaction'])
Exemple #15
0
 def unsubscribe(self, destination=None, id=None, headers={}, **keyword_headers):
     assert id is not None or destination is not None, "'id' or 'destination' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     if id:
         headers[HDR_ID] = id
     if destination:
         headers[HDR_DESTINATION] = destination
     self.__send_frame(CMD_UNSUBSCRIBE, headers)
Exemple #16
0
 def subscribe(self, destination, id=None, ack="auto", headers={}, **keyword_headers):
     assert destination is not None, "'destination' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_DESTINATION] = destination
     if id:
         headers[HDR_ID] = id
     headers[HDR_ACK] = ack
     self.__send_frame(CMD_SUBSCRIBE, headers)
Exemple #17
0
 def connect(self, headers={}, **keyword_headers):
     """
     Send a CONNECT frame to start a connection
     """
     if 'wait' in keyword_headers and keyword_headers['wait']:
         while not self.is_connected(): time.sleep(0.1)
         del keyword_headers['wait']
     self.__send_frame_helper('CONNECT', '', utils.merge_headers([self.__connect_headers, headers, keyword_headers]), [ ])
Exemple #18
0
 def begin(self, headers={}, **keyword_headers):
     """
     Send a BEGIN frame to start a transaction
     """
     use_headers = utils.merge_headers([headers, keyword_headers])
     if not 'transaction' in use_headers.keys(): 
         use_headers['transaction'] = str(uuid.uuid4())
     self.__send_frame_helper('BEGIN', '', use_headers, [ 'transaction' ])
     return use_headers['transaction']
Exemple #19
0
 def subscribe(self, headers={}, **keyword_headers):
     """
     Send a SUBSCRIBE frame to subscribe to a queue
     """
     merged_headers = utils.merge_headers([headers, keyword_headers])
     required_headers = [ 'destination' ]
     if self.version >= 1.1:
         required_headers.append('id')
     self.__send_frame_helper('SUBSCRIBE', '', merged_headers, required_headers)
Exemple #20
0
 def begin(self, headers={}, **keyword_headers):
     """
     Send a BEGIN frame to start a transaction
     """
     use_headers = utils.merge_headers([headers, keyword_headers])
     if not 'transaction' in use_headers.keys():
         use_headers['transaction'] = str(uuid.uuid4())
     self.__send_frame_helper('BEGIN', '', use_headers, ['transaction'])
     return use_headers['transaction']
Exemple #21
0
 def subscribe(self, headers={}, **keyword_headers):
     """
     Send a SUBSCRIBE frame to subscribe to a queue
     """
     merged_headers = utils.merge_headers([headers, keyword_headers])
     required_headers = [ 'destination' ]
     if self.version >= 1.1:
         required_headers.append('id')
     self.__send_frame_helper('SUBSCRIBE', '', merged_headers, required_headers)
Exemple #22
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)
Exemple #23
0
 def send(self, message='', headers={}, **keyword_headers):
     """
     Send a message (SEND) frame
     """
     if '\x00' in message:
         content_length_headers = {'content-length': len(message)}
     else:
         content_length_headers = {}
     self.__send_frame_helper('SEND', message, utils.merge_headers([headers, 
                                                                     keyword_headers,
                                                                     content_length_headers]), [ 'destination' ])
     self.__notify('send', headers, message)
Exemple #24
0
 def connect(self, headers={}, **keyword_headers):
     """
     Send a CONNECT frame to start a connection
     """
     if 'wait' in keyword_headers and keyword_headers['wait']:
         while not self.is_connected():
             time.sleep(0.1)
         del keyword_headers['wait']
     self.__send_frame_helper(
         'CONNECT', '',
         utils.merge_headers(
             [self.__connect_headers, headers, keyword_headers]), [])
Exemple #25
0
 def send(self, message='', headers={}, **keyword_headers):
     """
     Send a message (SEND) frame
     """
     if '\x00' in message:
         content_length_headers = {'content-length': len(message)}
     else:
         content_length_headers = {}
     self.__send_frame_helper(
         'SEND', message,
         utils.merge_headers(
             [headers, keyword_headers, content_length_headers]),
         ['destination'])
     self.__notify('send', headers, message)
Exemple #26
0
 def disconnect(self, send_disconnect=True, headers={}, **keyword_headers):
     """
     Send a DISCONNECT frame to finish a connection
     """
     try:
         self.__send_frame_helper('DISCONNECT', '', utils.merge_headers([self.__connect_headers, headers, keyword_headers]), [ ])
     except exception.NotConnectedException:
         _, e, _ = sys.exc_info()
         self.disconnect_socket()
         raise e
     if self.version >= 1.1 and 'receipt' in headers:
         self.__disconnect_receipt = headers['receipt']
     else:
         self.disconnect_socket()
Exemple #27
0
 def disconnect(self, send_disconnect=True, headers={}, **keyword_headers):
     """
     Send a DISCONNECT frame to finish a connection
     """
     if self.version >= 1.1 and 'receipt' not in headers:
         headers['receipt'] = str(uuid.uuid4())
     try:
         self.__send_frame_helper('DISCONNECT', '', utils.merge_headers([self.__connect_headers, headers, keyword_headers]), [ ])
     except exception.NotConnectedException:
         _, e, _ = sys.exc_info()
         self.disconnect_socket()
         raise e
     if 'receipt' in headers:
         self.__disconnect_receipt = headers['receipt']
     else:
         self.disconnect_socket()
Exemple #28
0
    def connect(self, username=None, passcode=None, wait=False, headers={}, **keyword_headers):
        cmd = CMD_CONNECT
        headers = utils.merge_headers([headers, keyword_headers])
        headers[HDR_ACCEPT_VERSION] = self.version
        
        if username is not None:
            headers[HDR_LOGIN] = username

        if passcode is not None:
            headers[HDR_PASSCODE] = passcode

        self.__send_frame(cmd, headers)
        
        if wait:
            self.transport.wait_for_connection()
            if self.connection_error:
                raise ConnectFailedException()
Exemple #29
0
 def send(self, message='', headers={}, **keyword_headers):
     """
     Send a message (SEND) frame
     """
     merged_headers = utils.merge_headers([headers, keyword_headers])
     
     if self.__wait_on_receipt and 'receipt' in merged_headers.keys():
         self.__send_wait_condition.acquire()
      
     self.__send_frame_helper('SEND', message, merged_headers, [ 'destination' ])
     self.__notify('send', headers, message)
     
     # if we need to wait-on-receipt, then block until the receipt frame arrives 
     if self.__wait_on_receipt and 'receipt' in merged_headers.keys():
         receipt = merged_headers['receipt']
         while receipt not in self.__receipts:
             self.__send_wait_condition.wait()
         self.__send_wait_condition.release()
         del self.__receipts[receipt]
Exemple #30
0
 def abort(self, transaction, headers={}, **keyword_headers):
     assert transaction is not None, "'transaction' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_TRANSACTION] = transaction
     self.__send_frame(CMD_ABORT, headers)
Exemple #31
0
 def commit(self, headers={}, **keyword_headers):
     """
     Send a COMMIT frame to commit a transaction (send pending messages)
     """
     self.__send_frame_helper('COMMIT', '', utils.merge_headers([headers, keyword_headers]), [ 'transaction' ])
Exemple #32
0
 def abort(self, headers={}, **keyword_headers):
     """
     Send an ABORT frame to rollback a transaction
     """
     self.__send_frame_helper('ABORT', '', utils.merge_headers([headers, keyword_headers]), [ 'transaction' ])
Exemple #33
0
 def ack(self, headers={}, **keyword_headers):
     """
     Send an ACK frame, to acknowledge receipt of a message
     """
     self.__send_frame_helper('ACK', '', utils.merge_headers([headers, keyword_headers]), [ 'message-id' ])
Exemple #34
0
 def unsubscribe(self, headers={}, **keyword_headers):
     """
     Send an UNSUBSCRIBE frame to unsubscribe from a queue
     """
     merged_headers = utils.merge_headers([headers, keyword_headers])
     self.__send_frame_helper('UNSUBSCRIBE', '', merged_headers, [ ('destination', 'id') ])
Exemple #35
0
 def unsubscribe(self, id, headers={}, **keyword_headers):
     assert id is not None, "'id' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_ID] = id
     self.__send_frame(CMD_UNSUBSCRIBE, headers)
Exemple #36
0
 def commit(self, transaction=None, headers={}, **keyword_headers):
     assert transaction is not None, "'transaction' is required"
     headers = utils.merge_headers([headers, keyword_headers])
     headers[HDR_TRANSACTION] = transaction
     self.__send_frame("COMMIT", headers)
Exemple #37
0
 def disconnect(self, receipt=str(uuid.uuid4()), headers={}, **keyword_headers):
     headers = utils.merge_headers([headers, keyword_headers])
     if receipt:
         headers[HDR_RECEIPT] = receipt
     self.__send_frame(CMD_DISCONNECT, headers)
Exemple #38
0
 def subscribe(self, headers={}, **keyword_headers):
     """
     Send a SUBSCRIBE frame to subscribe to a queue
     """
     self.__send_frame_helper('SUBSCRIBE', '', utils.merge_headers([headers, keyword_headers]), [ 'destination' ])