Ejemplo n.º 1
0
    def request(self, subject, data=None, blk=None, **opts):
        '''\
        Send a request and have the response delivered to the supplied callback.

        Params:
        =====
        subject: message subject;
        msg: message payload;
        callback: callback if any;

        Returns:
        =====
        sid: Subject Identifier
        '''
        if not self.conn.connected:
            raise NatsClientException("Connection losted")
        if not subject:
            return None
        inbox = Common.create_inbox()

        def process_reply(msg, reply):
            'closure block of request'
            args, _, _, _ = inspect.getargspec(blk)
            args_len = len(args)
            if args_len == 0:
                blk()
            elif args_len == 1:
                blk(msg)
            else:
                blk(msg, reply)

        sid = self.subscribe(inbox, process_reply, **opts)
        self.publish(subject, data, inbox)
        return sid
Ejemplo n.º 2
0
    def request(self, subject, data=None, blk=None, **opts):
        '''\
        Send a request and have the response delivered to the supplied callback.

        Params:
        =====
        subject: message subject;
        msg: message payload;
        callback: callback if any;

        Returns:
        =====
        sid: Subject Identifier
        '''
        if not self.conn.connected:
            raise NatsClientException("Connection losted")
        if not subject:
            return None
        inbox = Common.create_inbox()
        def process_reply(msg, reply):
            'closure block of request'
            args, _, _, _ = inspect.getargspec(blk)
            args_len = len(args)
            if args_len == 0:
                blk()
            elif args_len == 1:
                blk(msg)
            else:
                blk(msg, reply)
        sid = self.subscribe(inbox, process_reply, **opts)
        self.publish(subject, data, inbox)
        return sid
Ejemplo n.º 3
0
 def test_use_default_uri(self):
     'should use default server address if no uri was given'
     user, pswd, host, port = Common.parse_address_info()
     self.assertEqual(user, 'nats')
     self.assertEqual(pswd, 'nats')
     self.assertEqual(host, '127.0.0.1')
     self.assertEqual(port, 4222)
Ejemplo n.º 4
0
 def test_use_default_uri(self):
     'should use default server address if no uri was given'
     user, pswd, host, port = Common.parse_address_info()
     self.assertEqual(user, 'nats')
     self.assertEqual(pswd, 'nats')
     self.assertEqual(host, '127.0.0.1')
     self.assertEqual(port, 4222)
Ejemplo n.º 5
0
 def test_parse_address_info(self):
     'should return the right value if right uri given'
     nats_uri = 'nats://*****:*****@127.0.0.1:4222'
     user, pswd, host, port = Common.parse_address_info(nats_uri)
     self.assertEqual(user, 'nats')
     self.assertEqual(pswd, 'nats')
     self.assertEqual(host, '127.0.0.1')
     self.assertEqual(port, 4222)
Ejemplo n.º 6
0
 def test_parse_address_info(self):
     'should return the right value if right uri given'
     nats_uri = 'nats://*****:*****@127.0.0.1:4222'
     user, pswd, host, port = Common.parse_address_info(nats_uri)
     self.assertEqual(user, 'nats')
     self.assertEqual(pswd, 'nats')
     self.assertEqual(host, '127.0.0.1')
     self.assertEqual(port, 4222)
Ejemplo n.º 7
0
 def __init__(self, **options):
     self.conn_opts = {}
     for (kwd, default) in self._DEFAULT_CONN_OPTS.items():
         if kwd in options:
             value = options[kwd]
         else:
             value = default
         self.conn_opts[kwd] = value
     self.conn_opts['user'], self.conn_opts['pass'], self.host, self.port = \
                  Common.parse_address_info(options.get("uris"))
     self.sock = socket()
     self.connected = False
     self.callback = options['callback']
     self.data_recv = threading.Thread(target=self._waiting_data)
     self.pending = []
Ejemplo n.º 8
0
 def __init__(self, **options):
     self.conn_opts = {}
     for (kwd, default) in self._DEFAULT_CONN_OPTS.items():
         if kwd in options:
             value = options[kwd]
         else:
             value = default
         self.conn_opts[kwd] = value
     self.conn_opts['user'], self.conn_opts['pass'], self.host, self.port = \
                  Common.parse_address_info(options.get("uris"))
     self.sock = socket()
     self.connected = False
     self.callback = options['callback']
     self.data_recv = threading.Thread(target=self._waiting_data)
     self.pending = []
Ejemplo n.º 9
0
    def subscribe(self, subject, callback=None, **opts):
        '''\
        Subscribe to a subject with optional wildcards.
        Messages will be delivered to the supplied callback.
        Callback can take any number of the supplied arguments as defined by the
        list: msg, reply, sub.

        Params:
        =====
        subject: optionally with wilcards.
        opts:  optional options hash, e.g. "queue", "max".
        callback, called when a message is delivered.

        Returns:
        =====
        sid: Subject Identifier
        Returns subscription id which can be passed to #unsubscribe.
        '''
        if not self.conn.connected:
            raise NatsClientException("Connection losted")
        if not subject:
            return None
        sid = Common.get_ssid()
        queue_str = ""
        sub = {"subject" : subject, "received" : 0}
        sub["callback"] = callback
        if "queue" in opts:
            queue_str = sub["queue"] = opts["queue"]
        if "max" in opts:
            sub["max"] = opts["max"]
        self.conn.send_command("SUB {} {} {}{}".format(subject, queue_str,
            sid, Protocol.CR_LF))
        self.subs[sid] = sub
        # Setup server support for auto-unsubscribe
        if "max" in opts:
            self.unsubscribe(sid, opts["max"])
        return sid
Ejemplo n.º 10
0
    def subscribe(self, subject, callback=None, **opts):
        '''\
        Subscribe to a subject with optional wildcards.
        Messages will be delivered to the supplied callback.
        Callback can take any number of the supplied arguments as defined by the
        list: msg, reply, sub.

        Params:
        =====
        subject: optionally with wilcards.
        opts:  optional options hash, e.g. "queue", "max".
        callback, called when a message is delivered.

        Returns:
        =====
        sid: Subject Identifier
        Returns subscription id which can be passed to #unsubscribe.
        '''
        if not self.conn.connected:
            raise NatsClientException("Connection losted")
        if not subject:
            return None
        sid = Common.get_ssid()
        queue_str = ""
        sub = {"subject": subject, "received": 0}
        sub["callback"] = callback
        if "queue" in opts:
            queue_str = sub["queue"] = opts["queue"]
        if "max" in opts:
            sub["max"] = opts["max"]
        self.conn.send_command("SUB {} {} {}{}".format(subject, queue_str, sid,
                                                       Protocol.CR_LF))
        self.subs[sid] = sub
        # Setup server support for auto-unsubscribe
        if "max" in opts:
            self.unsubscribe(sid, opts["max"])
        return sid
Ejemplo n.º 11
0
 def test_create_inbox(self):
     'should return random INBOX name'
     inbox1 = Common.create_inbox()
     inbox2 = Common.create_inbox()
     assert '_INBOX' in inbox1
     assert inbox1 != inbox2
Ejemplo n.º 12
0
 def test_create_inbox(self):
     'should return random INBOX name'
     inbox1 = Common.create_inbox()
     inbox2 = Common.create_inbox()
     assert '_INBOX' in inbox1
     assert inbox1 != inbox2