コード例 #1
1
class StompCLI(ConnectionListener):
    def __init__(self, host='localhost', port=61613, user='', passcode=''):
        self.c = Connection([(host, port)], user, passcode)
        self.c.set_listener('', self)
        self.c.start()

    def __print_async(self, frame_type, headers, body):
        print "\r  \r",
        print frame_type
        for header_key in headers.keys():
            print '%s: %s' % (header_key, headers[header_key])
        print
        print body
        print '> ',
        sys.stdout.flush()

    def on_connecting(self, host_and_port):
        self.c.connect(wait=True)

    def on_disconnected(self, headers, body)
コード例 #2
1
ファイル: send_job.py プロジェクト: melexis/mapmerge
from stomp import Connection

import sys

hostname = sys.argv[1]

class MessageListener:

  def on_message(self, headers, message):
    print "Got message %s" % message


c = Connection([(hostname, 61613)])
c.set_listener('', MessageListener())
c.start()
c.connect()
c.subscribe(destination='/topic/postprocessing.mapmerge.out', ack='auto')
f = open('example.xml', 'rb').read()
c.send(f, destination='/queue/postprocessing.mapmerge.erfurt.in')
print 'sent'

import time
time.sleep(1)
コード例 #3
0
 def write_to_active_mq(self, message):
     rospy.loginfo(str(datetime.datetime.now()) + ': Posting to ActiveMQ ' + str(message))
     conn = Connection(host_and_ports=[(AntennaConstants.ACTIVE_MQ_HOST, AntennaConstants.ACTIVE_MQ_PORT)])
     conn.start()
     conn.connect(AntennaConstants.ACTIVE_MQ_USER, AntennaConstants.ACTIVE_MQ_PASS, wait=True)
     conn.send(body=json.dumps(message), destination=AntennaConstants.ACTIVE_MQ_TOPIC_OUT)
     conn.disconnect()
コード例 #4
0
ファイル: test_activemq.py プロジェクト: q742765643/beat
    def verify_destination_metrics_collection(self, destination_type):
        from stomp import Connection

        self.render_config_template(modules=[self.get_activemq_module_config(destination_type)])
        proc = self.start_beat(home=self.beat_path)

        destination_name = ''.join(random.choice(string.ascii_lowercase) for i in range(10))

        conn = Connection([self.get_stomp_host_port()])
        conn.start()
        conn.connect(wait=True)
        conn.send('/{}/{}'.format(destination_type, destination_name), 'first message')
        conn.send('/{}/{}'.format(destination_type, destination_name), 'second message')

        self.wait_until(lambda: self.destination_metrics_collected(destination_type, destination_name))
        proc.check_kill_and_wait()
        self.assert_no_logged_warnings()

        output = self.read_output_json()

        passed = False
        for evt in output:
            if self.all_messages_enqueued(evt, destination_type, destination_name):
                assert 0 < evt['activemq'][destination_type]['messages']['size']['avg']
                self.assert_fields_are_documented(evt)
                passed = True

        conn.disconnect()
        assert passed
コード例 #5
0
ファイル: volare.py プロジェクト: cyberelfo/casanova
class Volare(object):

    # Construtor simplificado, similar ao usado em Java
    # Note que broker deve ser uma url do tipo "stomp://host:port[/]",
    # que será decodificada para isolar o host e a porta usados pelo stomppy
    def __init__(self, user, password, broker):

        self.user = user
        self.password = password

        if not broker:
            raise ValueError(u"A url do broker é obrigatória.")

        if not broker.startswith("stomp://"):
            raise ValueError(u"A url do broker deve usar 'stomp://'.")
         
        # como broker é da forma 'stomp://hostname:porta[/]', pode remover
        # o prefixo, desprezar a '/' opcional no final e separar no ':'

        self.host, dummy, strport = broker[8:].rstrip('/').partition(':')

        if len(self.host) == 0 or not strport.isdigit():
            raise ValueError(u"O host e a porta do broker são obrigatórios.")

        self.port = int(strport)


    def connect(self):
        try:
            self._connection = Connection([(self.host, self.port)], self.user, self.password)
            self._connection.start()
            self._connection.connect(wait=True)
        except ConnectFailedException, err:
            raise OfflineBrokerError("Failed to connect to the broker at stomp://%s:%s/" % (self.host, self.port) )
コード例 #6
0
class RemoteBroker:
    def __init__(self, hostname, port, request_queue_name, response_queue_name,
                 request_timeout_millis):
        hosts = [(hostname, port)]
        connect_timeout = 10
        self.conn = Connection(host_and_ports=hosts, timeout=connect_timeout)
        self.conn.start()
        self.conn.connect(wait=True)
        self.request_queue_name = request_queue_name
        self.response_queue_name = response_queue_name
        self.request_timeout_millis = request_timeout_millis
        self._timer = None

    def acknowledge(self, headers):
        self.conn.ack(headers['message-id'], headers['subscription'])

    def publish(self, response):
        self.conn.send(body=json.dumps(response, separators=(',', ':')),
                       destination=self.response_queue_name)

    def subscribe(self, handling_strategy, audit):
        listener = Listener(self, handling_strategy, self.start_timer,
                            self.stop_timer, audit)
        self.conn.set_listener('listener', listener)
        self.conn.subscribe(destination=self.request_queue_name,
                            id=1,
                            ack='client-individual')
        self.start_timer()

    def respond_to(self, headers, response):
        self.acknowledge(headers)
        self.publish(
            OrderedDict([('result', response.result), ('error', None),
                         ('id', response.id)]))

    def stop(self):
        self.conn.unsubscribe(1)
        self.conn.remove_listener('listener')

    def close(self):
        self.conn.disconnect()

    def is_connected(self):
        return self.conn.is_connected()

    def stop_timer(self):
        if self._timer is not None:
            self._timer.cancel()

    def start_timer(self):
        self._timer = Timer(self.request_timeout_millis / 1000.00, self.close)
        self._timer.start()
class Volare(object):

    # Construtor simplificado, similar ao usado em Java
    # Note que broker deve ser uma url do tipo "stomp://host:port[/]",
    # que será decodificada para isolar o host e a porta usados pelo stomppy
    def __init__(self, user, password, broker):

        self.user = user
        self.password = password

        if not broker:
            raise ValueError(u"A url do broker é obrigatória.")

        if not broker.startswith("stomp://"):
            raise ValueError(u"A url do broker deve usar 'stomp://'.")

        # como broker é da forma 'stomp://hostname:porta[/]', pode remover
        # o prefixo, desprezar a '/' opcional no final e separar no ':'

        self.host, dummy, strport = broker[8:].rstrip('/').partition(':')

        if len(self.host) == 0 or not strport.isdigit():
            raise ValueError(u"O host e a porta do broker são obrigatórios.")

        self.port = int(strport)

    def connect(self):
        try:
            self._connection = Connection([(self.host, self.port)], self.user,
                                          self.password)
            self._connection.start()
            self._connection.connect(wait=True)
        except ConnectFailedException, err:
            raise OfflineBrokerError(
                "Failed to connect to the broker at stomp://%s:%s/" %
                (self.host, self.port))
コード例 #8
0
class StompCLI(ConnectionListener):
    def __init__(self, host='localhost', port=61613, user='', passcode=''):
        self.c = Connection([(host, port)], user, passcode)
        self.c.set_listener('', self)
        self.c.start()

    def __print_async(self, frame_type, headers, body):
        print "\r  \r",
        print frame_type
        for header_key in headers.keys():
            print '%s: %s' % (header_key, headers[header_key])
        print
        print body
        print '> ',
        sys.stdout.flush()

    def on_connecting(self, host_and_port):
        self.c.connect(wait=True)

    def on_disconnected(self):
        print "lost connection"

    def on_message(self, headers, body):
        self.__print_async("MESSAGE", headers, body)

    def on_error(self, headers, body):
        self.__print_async("ERROR", headers, body)

    def on_receipt(self, headers, body):
        self.__print_async("RECEIPT", headers, body)

    def on_connected(self, headers, body):
        print 'connected'
        self.__print_async("CONNECTED", headers, body)

    def ack(self, args):
        '''
        Usage:
            ack <message-id> [transaction-id]

        Required Parameters:
            message-id - the id of the message being acknowledged

        Optional Parameters:
            transaction-id - the acknowledgement should be a part of the named transaction

        Description:
            The command 'ack' is used to acknowledge consumption of a message from a subscription using client
            acknowledgment. When a client has issued a 'subscribe' with the ack flag set to client, any messages
            received from that destination will not be considered to have been consumed (by the server) until
            the message has been acknowledged.
        '''
        if len(args) < 3:
            self.c.ack(message_id=args[1])
        else:
            self.c.ack(message_id=args[1], transaction=args[2])

    def abort(self, args):
        '''
        Usage:
            abort <transaction-id>

        Required Parameters:
            transaction-id - the transaction to abort

        Description:
            Roll back a transaction in progress.
        '''
        self.c.abort(transaction=args[1])

    def begin(self, args):
        '''
        Usage:
            begin

        Description:
            Start a transaction. Transactions in this case apply to sending and acknowledging -
            any messages sent or acknowledged during a transaction will be handled atomically based on the
            transaction.
        '''
        print 'transaction id: %s' % self.c.begin()

    def commit(self, args):
        '''
        Usage:
            commit <transaction-id>

        Required Parameters:
            transaction-id - the transaction to commit

        Description:
            Commit a transaction in progress.
        '''
        if len(args) < 2:
            print 'expecting: commit <transid>'
        else:
            print 'committing %s' % args[1]
            self.c.commit(transaction=args[1])

    def disconnect(self, args):
        '''
        Usage:
            disconnect

        Description:
            Gracefully disconnect from the server.
        '''
        try:
            self.c.disconnect()
        except NotConnectedException:
            pass  # ignore if no longer connected

    def send(self, args):
        '''
        Usage:
            send <destination> <message>

        Required Parameters:
            destination - where to send the message
            message - the content to send

        Description:
            Sends a message to a destination in the messaging system.
        '''
        if len(args) < 3:
            print 'expecting: send <destination> <message>'
        else:
            self.c.send(destination=args[1], message=' '.join(args[2:]))

    def sendtrans(self, args):
        '''
        Usage:
            sendtrans <destination> <transaction-id> <message>

        Required Parameters:
            destination - where to send the message
            transaction-id - the id of the transaction in which to enlist this message
            message - the content to send

        Description:
            Sends a message to a destination in the message system, using a specified transaction.
        '''
        if len(args) < 3:
            print 'expecting: sendtrans <destination> <transaction-id> <message>'
        else:
            self.c.send(destination=args[1],
                        message="%s\n" % ' '.join(args[3:]),
                        transaction=args[2])

    def subscribe(self, args):
        '''
        Usage:
            subscribe <destination> [ack]

        Required Parameters:
            destination - the name to subscribe to

        Optional Parameters:
            ack - how to handle acknowledgements for a message; either automatically (auto) or manually (client)

        Description:
            Register to listen to a given destination. Like send, the subscribe command requires a destination
            header indicating which destination to subscribe to. The ack parameter is optional, and defaults to
            auto.
        '''
        if len(args) < 2:
            print 'expecting: subscribe <destination> [ack]'
        elif len(args) > 2:
            print 'subscribing to "%s" with acknowledge set to "%s"' % (
                args[1], args[2])
            self.c.subscribe(destination=args[1], ack=args[2])
        else:
            print 'subscribing to "%s" with auto acknowledge' % args[1]
            self.c.subscribe(destination=args[1], ack='auto')

    def unsubscribe(self, args):
        '''
        Usage:
            unsubscribe <destination>

        Required Parameters:
            destination - the name to unsubscribe from

        Description:
            Remove an existing subscription - so that the client no longer receive messages from that destination.
        '''
        if len(args) < 2:
            print 'expecting: unsubscribe <destination>'
        else:
            print 'unsubscribing from "%s"' % args[1]
            self.c.unsubscribe(destination=args[1])

    def stats(self, args):
        '''
        Usage:
            stats [on|off]
            
        Description:
            Record statistics on messages sent, received, errors, etc. If no argument (on|off) is specified,
            dump the current statistics.
        '''
        if len(args) < 2:
            stats = self.c.get_listener('stats')
            if stats:
                print stats
            else:
                print 'No stats available'
        elif args[1] == 'on':
            self.c.set_listener('stats', StatsListener())
        elif args[1] == 'off':
            self.c.remove_listener('stats')
        else:
            print 'expecting: stats [on|off]'

    def help(self, args):
        '''
        Usage:
            help [command]

        Description:
            Display info on a specified command, or a list of available commands
        '''
        if len(args) == 1:
            print 'Usage: help <command>, where command is one of the following:'
            print '    '
            for f in dir(self):
                if f.startswith('_') or f.startswith('on_') or f == 'c':
                    continue
                else:
                    print '%s ' % f,
            print ''
            return
        elif not hasattr(self, args[1]):
            print 'There is no command "%s"' % args[1]
            return

        func = getattr(self, args[1])
        if hasattr(func, '__doc__') and getattr(func, '__doc__') is not None:
            print func.__doc__
        else:
            print 'There is no help for command "%s"' % args[1]
コード例 #9
0
ファイル: stomp_test.py プロジェクト: kuochunchang/scada
from stomp import Connection, PrintingListener
import time

c = Connection([('127.0.0.1', 61613)])
c.set_listener('', PrintingListener())
c.start()

c.connect('emmet', 'masterbuilder', wait=True)
c.subscribe('/queue/test01', 123)
# c.send('queue/test01', 'a test message')
time.sleep(60)

コード例 #10
0
class TestMq(unittest.TestCase):
    def setUp(self):
        self.topic = 'testEventListening'
        self.host_and_ports = mq.host_and_ports

        self.listener = EventListener(self.host_and_ports, self.topic)
        reactor = Mock()
        self.listener.start_listening(reactor)

        self.sender_client = Connection(self.host_and_ports)
        self.sender_client.start()
        self.sender_client.connect(wait=True)

    def tearDown(self):
        self.sender_client.disconnect()
        self.listener.stop_listening()

    def _send(self, topic, msg):
        self.sender_client.send(body=msg, destination=topic)

    def test_msg_receiving(self):
        listener = self.listener
        with patch.object(listener, 'on_message') as on_message:
            on_message.return_value = None
            self.assertTrue(listener.on_message.call_count == 0)
            self._send(self.topic, 'this is a test msg')
            time.sleep(1)
            self.assertTrue(listener.on_message.call_count == 1)
            self._send(self.topic, 'this is a test msg')
            time.sleep(1)
            self.assertTrue(listener.on_message.call_count == 2)

    def test_reconnect(self):
        # ensure it's connecting to AMQ
        self.test_msg_receiving()
        se = ConnectFailedException,
        with patch.object(self.listener,
                          '_connect_and_subscribe',
                          side_effect=se) as m:
            self.assertTrue(m.call_count == 0)
            self.listener.conn.disconnect()
            time.sleep(1)
            self.assertTrue(m.call_count == 1)
            time.sleep(6)
            self.assertTrue(m.call_count == 2)
        time.sleep(1)

        # ensure reconnecting is successful
        self.test_msg_receiving()

    def test_qos(self):
        topic = 'topic_for_ack'
        cnt_msg = 5
        listener = EventListener(self.host_and_ports, topic)
        listener.start_listening('client')
        for i in xrange(cnt_msg):
            self._send(topic, 'msg for testing ack')
        listener.stop_listening()

        def se(headers, msg):
            msg_id = headers['message-id']
            listener.conn.ack(msg_id, listener.subscription_id)

        with patch.object(listener, 'on_message', side_effect=se) as m:
            listener.start_listening('client')
            time.sleep(1)
            self.assertTrue(m.call_count == cnt_msg)

        listener.stop_listening()
        time.sleep(1)
        with patch.object(listener, 'on_message', side_effect=se) as m:
            listener.start_listening('client')
            time.sleep(1)
            self.assertTrue(m.call_count == 0)

        listener.stop_listening()
コード例 #11
0
ファイル: cli.py プロジェクト: Alerion/homeberman
class StompCLI(ConnectionListener):
    def __init__(self, host='localhost', port=61613, user='', passcode=''):
        self.c = Connection([(host, port)], user, passcode)
        self.c.set_listener('', self)
        self.c.start()

    def __print_async(self, frame_type, headers, body):
        print "\r  \r",
        print frame_type
        for header_key in headers.keys():
            print '%s: %s' % (header_key, headers[header_key])
        print
        print body
        print '> ',
        sys.stdout.flush()

    def on_connecting(self, host_and_port):
        self.c.connect(wait=True)

    def on_disconnected(self):
        print "lost connection"

    def on_message(self, headers, body):
        self.__print_async("MESSAGE", headers, body)

    def on_error(self, headers, body):
        self.__print_async("ERROR", headers, body)

    def on_receipt(self, headers, body):
        self.__print_async("RECEIPT", headers, body)

    def on_connected(self, headers, body):
        print 'connected'
        self.__print_async("CONNECTED", headers, body)

    def ack(self, args):
        '''
        Usage:
            ack <message-id> [transaction-id]

        Required Parameters:
            message-id - the id of the message being acknowledged

        Optional Parameters:
            transaction-id - the acknowledgement should be a part of the named transaction

        Description:
            The command 'ack' is used to acknowledge consumption of a message from a subscription using client
            acknowledgment. When a client has issued a 'subscribe' with the ack flag set to client, any messages
            received from that destination will not be considered to have been consumed (by the server) until
            the message has been acknowledged.
        '''
        if len(args) < 3:
            self.c.ack(message_id=args[1])
        else:
            self.c.ack(message_id=args[1], transaction=args[2])

    def abort(self, args):
        '''
        Usage:
            abort <transaction-id>

        Required Parameters:
            transaction-id - the transaction to abort

        Description:
            Roll back a transaction in progress.
        '''
        self.c.abort(transaction=args[1])

    def begin(self, args):
        '''
        Usage:
            begin

        Description:
            Start a transaction. Transactions in this case apply to sending and acknowledging -
            any messages sent or acknowledged during a transaction will be handled atomically based on the
            transaction.
        '''
        print 'transaction id: %s' % self.c.begin()

    def commit(self, args):
        '''
        Usage:
            commit <transaction-id>

        Required Parameters:
            transaction-id - the transaction to commit

        Description:
            Commit a transaction in progress.
        '''
        if len(args) < 2:
            print 'expecting: commit <transid>'
        else:
            print 'committing %s' % args[1]
            self.c.commit(transaction=args[1])

    def disconnect(self, args):
        '''
        Usage:
            disconnect

        Description:
            Gracefully disconnect from the server.
        '''
        try:
            self.c.disconnect()
        except NotConnectedException:
            pass # ignore if no longer connected

    def send(self, args):
        '''
        Usage:
            send <destination> <message>

        Required Parameters:
            destination - where to send the message
            message - the content to send

        Description:
            Sends a message to a destination in the messaging system.
        '''
        if len(args) < 3:
            print 'expecting: send <destination> <message>'
        else:
            self.c.send(destination=args[1], message=' '.join(args[2:]))
            
    def sendreply(self, args):
        '''
        Usage:
            sendreply <destination> <correlation-id> <message>

        Required Parameters:
            destination - where to send the message
            correlation-id - the correlating identifier to send with the response
            message - the content to send

        Description:
            Sends a reply message to a destination in the messaging system.
        '''
        if len(args) < 4:
            print 'expecting: sendreply <destination> <correlation-id> <message>'
        else:
            self.c.send(destination=args[1], message="%s\n" % ' '.join(args[3:]), headers={'correlation-id': args[2]})
    
    def sendtrans(self, args):
        '''
        Usage:
            sendtrans <destination> <transaction-id> <message>

        Required Parameters:
            destination - where to send the message
            transaction-id - the id of the transaction in which to enlist this message
            message - the content to send

        Description:
            Sends a message to a destination in the message system, using a specified transaction.
        '''
        if len(args) < 3:
            print 'expecting: sendtrans <destination> <transaction-id> <message>'
        else:
            self.c.send(destination=args[1], message="%s\n" % ' '.join(args[3:]), transaction=args[2])

    def subscribe(self, args):
        '''
        Usage:
            subscribe <destination> [ack]

        Required Parameters:
            destination - the name to subscribe to

        Optional Parameters:
            ack - how to handle acknowledgements for a message; either automatically (auto) or manually (client)

        Description:
            Register to listen to a given destination. Like send, the subscribe command requires a destination
            header indicating which destination to subscribe to. The ack parameter is optional, and defaults to
            auto.
        '''
        if len(args) < 2:
            print 'expecting: subscribe <destination> [ack]'
        elif len(args) > 2:
            print 'subscribing to "%s" with acknowledge set to "%s"' % (args[1], args[2])
            self.c.subscribe(destination=args[1], ack=args[2])
        else:
            print 'subscribing to "%s" with auto acknowledge' % args[1]
            self.c.subscribe(destination=args[1], ack='auto')

    def unsubscribe(self, args):
        '''
        Usage:
            unsubscribe <destination>

        Required Parameters:
            destination - the name to unsubscribe from

        Description:
            Remove an existing subscription - so that the client no longer receive messages from that destination.
        '''
        if len(args) < 2:
            print 'expecting: unsubscribe <destination>'
        else:
            print 'unsubscribing from "%s"' % args[1]
            self.c.unsubscribe(destination=args[1])

    def stats(self, args):
        '''
        Usage:
            stats [on|off]
            
        Description:
            Record statistics on messages sent, received, errors, etc. If no argument (on|off) is specified,
            dump the current statistics.
        '''
        if len(args) < 2:
            stats = self.c.get_listener('stats')
            if stats:
                print stats
            else:
                print 'No stats available'
        elif args[1] == 'on':
            self.c.set_listener('stats', StatsListener())
        elif args[1] == 'off':
            self.c.remove_listener('stats')
        else:
            print 'expecting: stats [on|off]'

    def help(self, args):
        '''
        Usage:
            help [command]

        Description:
            Display info on a specified command, or a list of available commands
        '''
        if len(args) == 1:
            print 'Usage: help <command>, where command is one of the following:'
            print '    '
            for f in dir(self):
                if f.startswith('_') or f.startswith('on_') or f == 'c':
                    continue
                else:
                    print '%s ' % f,
            print ''
            return
        elif not hasattr(self, args[1]):
            print 'There is no command "%s"' % args[1]
            return

        func = getattr(self, args[1])
        if hasattr(func, '__doc__') and getattr(func, '__doc__') is not None:
            print func.__doc__
        else:
            print 'There is no help for command "%s"' % args[1]
コード例 #12
0
class NotificationReceiver(object):
    """
    A class for receiving HMC notifications that are published to particular
    HMC notification topics.

    **Experimental:** This class is considered experimental at this point, and
    its API may change incompatibly as long as it is experimental.

    Creating an object of this class establishes a JMS session with the
    HMC and subscribes for the specified HMC notification topic(s).

    Notification topic strings are created by the HMC in context of a
    particular client session (i.e. :class:`~zhmcclient.Session` object).
    However, these topic strings can be used by any JMS message listener that
    knows the topic string and that authenticates under some valid HMC userid.
    The HMC userid used by the JMS listener does not need to be the one that
    was used for the client session in which the notification topic was
    originally created.
    """
    def __init__(self,
                 topic_names,
                 host,
                 userid,
                 password,
                 port=DEFAULT_STOMP_PORT):
        """
        Parameters:

          topic_names (:term:`string` or list/tuple thereof): Name(s) of the
            HMC notification topic(s).
            Must not be `None`.

          host (:term:`string`):
            HMC host. For valid formats, see the
            :attr:`~zhmcclient.Session.host` property.
            Must not be `None`.

          userid (:term:`string`):
            Userid of the HMC user to be used.
            Must not be `None`.

          password (:term:`string`):
            Password of the HMC user to be used.
            Must not be `None`.

          port (:term:`integer`):
            STOMP TCP port. Defaults to
            :attr:`~zhmcclient._constants.DEFAULT_STOMP_PORT`.
        """
        if not isinstance(topic_names, (list, tuple)):
            topic_names = [topic_names]
        self._topic_names = topic_names
        self._host = host
        self._port = port
        self._userid = userid
        self._password = password

        # Wait timeout to honor keyboard interrupts after this time:
        self._wait_timeout = 10.0  # seconds

        # Subscription ID. We use some value that allows to identify on the
        # HMC that this is the zhmcclient, but otherwise we are not using
        # this value ourselves.
        self._sub_id = 'zhmcclient.%s' % id(self)

        # Sync variables for thread-safe handover between listener thread and
        # receiver thread:
        self._handover_dict = {}
        self._handover_cond = threading.Condition()

        # Lazy importing for stomp, because it is so slow (ca. 5 sec)
        if 'Stomp_Connection' not in globals():
            from stomp import Connection as Stomp_Connection

        self._conn = Stomp_Connection([(self._host, self._port)],
                                      use_ssl="SSL")
        listener = _NotificationListener(self._handover_dict,
                                         self._handover_cond)
        self._conn.set_listener('', listener)
        self._conn.start()
        self._conn.connect(self._userid, self._password, wait=True)

        for topic_name in self._topic_names:
            dest = "/topic/" + topic_name
            self._conn.subscribe(destination=dest, id=self._sub_id, ack='auto')

    @logged_api_call
    def notifications(self):
        """
        Generator method that yields all HMC notifications (= JMS messages)
        received by this notification receiver.

        Example::

            desired_topic_types = ('security-notification',
                                   'audit-notification')
            topics = session.get_notification_topics()
            topic_names = [t['topic-name']
                           for t in topics
                           if t['topic-type'] in desired_topic_types]

            receiver = zhmcclient.NotificationReceiver(
                topic_names, hmc, userid, password)

            for headers, message in receiver.notifications():
                . . . # processing of topic-specific message format

        Yields:

          : A tuple (headers, message) representing one HMC notification, with:

          * headers (dict): The notification header fields.

            Some important header fields (dict items) are:

            * 'notification-type' (string): The HMC notification type (e.g.
              'os-message', 'job-completion', or others).

            * 'session-sequence-nr' (string): The sequence number of this HMC
              notification within the session created by this notification
              receiver object. This number starts at 0 when this receiver
              object is created, and is incremented each time an HMC
              notification is published to this receiver.

            * 'class' (string): The class name of the HMC resource publishing
              the HMC notification (e.g. 'partition').

            * 'object-id' (string) or 'element-id' (string): The ID of the HMC
              resource publishing the HMC notification.

            For a complete list of notification header fields, see section
            "Message format" in chapter 4. "Asynchronous notification" in the
            :term:`HMC API` book.

          * message (:term:`JSON object`): Body of the HMC notification,
            converted into a JSON object.

            The properties of the JSON object vary by notification type.

            For a description of the JSON properties, see the sub-sections
            for each notification type within section "Notification message
            formats" in chapter 4. "Asynchronous notification" in the
            :term:`HMC API` book.
        """

        while True:
            with self._handover_cond:

                # Wait until MessageListener has a new notification
                while len(self._handover_dict) == 0:
                    self._handover_cond.wait(self._wait_timeout)

                if self._handover_dict['headers'] is None:
                    return

                # Process the notification
                yield (self._handover_dict['headers'],
                       self._handover_dict['message'])

                # Indicate to MessageListener that we are ready for next
                # notification
                del self._handover_dict['headers']
                del self._handover_dict['message']
                self._handover_cond.notifyAll()

    @logged_api_call
    def close(self):
        """
        Disconnect and close the JMS session with the HMC.

        This implicitly unsubscribes from the notification topic this receiver
        was created for, and causes the
        :meth:`~zhmcclient.NotificationReceiver.notifications` method to
        stop its iterations.
        """
        self._conn.disconnect()
コード例 #13
0
class ActiveMQListener:
    def __init__(self):
        self.topic_public_calc = None
        self.topic_public_utf_antenna = None
        self.topic_communication = None
        self.topic_satellite_manager = None
        self.topic_active_mq_writer = None
        self.connection = None

    def __delete__(self, instance):
        if self.connection is not None:
            print('Closing MQ in connection')
            self.connection.disconnect()

    def publish_to_public_calc(self, message):
        try:
            request = Flyover_request()
            request.request_id = message['request_id']
            request.action = message['action']
            request.satellite_ids = message['data']['satellite_ids']
            request.long = message['data']['lon']
            request.lat = message['data']['lat']
            request.elevation = message['data']['elevation']

            rospy.loginfo(
                str(datetime.datetime.now()) +
                ': ActiveMQListener publish to public calc: ' + str(request))
            self.topic_public_calc.publish(request)
        except Exception as ex:
            rospy.logerr(
                str(datetime.datetime.now()) + ': request ID: ' +
                message['request_id'] + ' ' + str(ex))
            self.publish_exception(message['request_id'], str(ex))

    def publish_to_public_utf_antenna(self, message):
        try:
            request = UTF_antenna_command()
            request.request_id = message['request_id']
            request.action = message['action']

            if message['elevation'] is not None:
                request.elevation = message['elevation']

            if message['azimuth'] is not None:
                request.azimuth = message['azimuth']

            rospy.loginfo(
                str(datetime.datetime.now()) +
                ': ActiveMQListener publish to utf antenna: ' + str(request))
            self.topic_public_utf_antenna.publish(request)
        except Exception as ex:
            rospy.logerr(
                str(datetime.datetime.now()) + ': request ID: ' +
                message['request_id'] + ' ' + str(ex))
            self.publish_exception(message['request_id'], str(ex))

    def publish_to_communication(self, message):
        try:
            request = Communication_package()
            request.request_id = message['request_id']
            request.data = message['data']

            rospy.loginfo(
                str(datetime.datetime.now()) +
                ': ActiveMQListener publish to satellite communication: ' +
                str(request))
            self.topic_communication.publish(request)
        except Exception as ex:
            rospy.logerr(
                str(datetime.datetime.now()) + ': request ID: ' +
                message['request_id'] + ' ' + str(ex))
            self.publish_exception(message['request_id'], str(ex))

    def publish_to_satellite_manager(self, message):
        try:
            request = Satellite_manager_command()
            request.request_id = message['request_id']
            request.action = message['action']

            if message['satellite_ids'] is not None:
                request.satellite_ids = message['satellite_ids']

            rospy.loginfo(
                str(datetime.datetime.now()) +
                ': ActiveMQListener publish to satellite manager: ' +
                str(request))
            self.topic_satellite_manager.publish(request)
        except Exception as ex:
            rospy.logerr(
                str(datetime.datetime.now()) + ': request ID: ' +
                message['request_id'] + ' ' + str(ex))
            self.publish_exception(message['request_id'], [str(ex)])

    def publish_exception(self, request_id, message):
        response = Response()
        response.request_id = request_id
        response.ok = False
        response.data = [message]

        rospy.loginfo(
            str(datetime.datetime.now()) +
            ': ActiveMQListener publish exception: ' + str(response))
        self.topic_active_mq_writer.publish(response)

    def connect_to_mq(self):
        rospy.loginfo(
            str(datetime.datetime.now()) + ': Connecting to ActiveMQ')
        self.connection = Connection(
            host_and_ports=[(AntennaConstants.ACTIVE_MQ_HOST,
                             AntennaConstants.ACTIVE_MQ_PORT)])
        self.connection.set_listener('my_listener',
                                     ActiveMQConnectionListener(self))
        self.connection.start()
        self.connection.connect(AntennaConstants.ACTIVE_MQ_USER,
                                AntennaConstants.ACTIVE_MQ_PASS,
                                wait=True)
        self.connection.subscribe(
            destination=AntennaConstants.ACTIVE_MQ_TOPIC_IN, id=1, ack='auto')

    def start(self):
        print('Starting ActiveMQListener')
        self.topic_public_calc = rospy.Publisher(
            TopicConstants.TOPIC_PUBLIC_CALC,
            Flyover_request,
            queue_size=TopicConstants.QUEUE_SIZE)
        self.topic_public_utf_antenna = rospy.Publisher(
            TopicConstants.TOPIC_PUBLIC_UTF_ANTENNA,
            UTF_antenna_command,
            queue_size=TopicConstants.QUEUE_SIZE)
        self.topic_communication = rospy.Publisher(
            TopicConstants.TOPIC_COMMUNICATION,
            Communication_package,
            queue_size=TopicConstants.QUEUE_SIZE)
        self.topic_satellite_manager = rospy.Publisher(
            TopicConstants.TOPIC_SATELLITE_MANAGER,
            Satellite_manager_command,
            queue_size=TopicConstants.QUEUE_SIZE)
        self.topic_active_mq_writer = rospy.Publisher(
            TopicConstants.TOPIC_ACTIVE_MQ_WRITER,
            Response,
            queue_size=TopicConstants.QUEUE_SIZE)
        rospy.init_node(TopicConstants.TOPIC_ACTIVE_MQ_LISTENER,
                        anonymous=False)
        self.connect_to_mq()