Exemple #1
0
    def start_queue_device():

        factory = ZmqFactory()

        frontend_endpoint = ZmqEndpoint(ZmqEndpointType.bind, options.frontend_endpoint)
        frontend = ZmqRepConnection(frontend_endpoint)
        frontend = yield frontend.listen(factory)
        print "broker's frontend router connection is ready"
        print frontend

        backend_endpoint = ZmqEndpoint(ZmqEndpointType.bind, options.backend_endpoint)
        backend = ZmqReqConnection(backend_endpoint)
        backend = yield backend.listen(factory)
        print "broker's backend dealer connection is ready"
        print backend

        broker = ZmqQueue(frontend, backend)
        print "broker is ready"
        print broker

        defer.returnValue(None)
Exemple #2
0
if __name__ == '__main__':
    
    (options, args) = parser.parse_args()
 
    def handleResponse(reply):
        # this example only uses single-part messages
        reply = reply[0]
        print "received reply: %s" % reply
        reactor.callLater(1, reactor.stop)
    
    def doRequest(requester):
        data = 'hello'
        print "client sending request containing \'%s\'" % (data)
        d = requester.request(data)
        d.addCallback(handleResponse)

    def onConnect(requester):
        print "client connected to broker"
        reactor.callLater(2, doRequest, requester)
        

    endpoint = ZmqEndpoint(ZmqEndpointType.connect, options.endpoint)
    print endpoint
    client = ZmqReqConnection(endpoint)
    print client
    deferred = client.connect(ZmqFactory())
    deferred.addCallback(onConnect)
    reactor.run()

Exemple #3
0
 def setUp(self):
     self.factory = ZmqFactory()
     ZmqReqConnection.identity = 'client'
     self.r = ZmqTestRepConnection(ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
     self.s = ZmqReqConnection(ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
     self.count = 0
Exemple #4
0
parser = OptionParser("")
parser.add_option("-e", "--endpoint", dest="endpoint", default="ipc:///tmp/sock", help="0MQ Endpoint")


if __name__ == '__main__':
    
    (options, args) = parser.parse_args()
  
    @defer.inlineCallbacks 
    def doRequest(requester):
        data = str(time.time())
        print "sending request containing %s ..." % (data)
        reply = yield requester.request(data)
        # this example only uses single-part messages
        reply = reply[0]
        print "received reply: %s" % reply
        reactor.callLater(1, reactor.stop)

    def onConnect(requester):
        print "Requester connected"
        requester.setSocketOptions({constants.LINGER:0})
        reactor.callLater(1, doRequest, requester)
        

    endpoint = ZmqEndpoint(ZmqEndpointType.connect, options.endpoint)
    requester = ZmqReqConnection(endpoint)
    deferred = requester.connect(ZmqFactory())
    deferred.addCallback(onConnect)
    reactor.run()

Exemple #5
0
class ZmqReqRepConnectionTestCase(unittest.TestCase):
    """
    Test case for L{tx0mq.reqrep}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        ZmqReqConnection.identity = 'client'
        self.r = ZmqTestRepConnection(ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.s = ZmqReqConnection(ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
        self.count = 0


    def tearDown(self):
        ZmqReqConnection.identity = None
        self.factory.shutdown()


    def test_getNextId(self):
        self.failUnlessEqual([], self.s._uuids)
        id1 = self.s._getNextId()
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE - 1, len(self.s._uuids))
        self.failUnlessIsInstance(id1, str)

        id2 = self.s._getNextId()
        self.failUnlessIsInstance(id2, str)

        self.failIfEqual(id1, id2)

        ids = [self.s._getNextId() for _ in range(1000)]
        self.failUnlessEqual(len(ids), len(set(ids)))

    def test_releaseId(self):
        self.s._releaseId(self.s._getNextId())
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))



    def test_send_recv(self):

        def get_next_id():
            self.count += 1
            return 'msg_id_%d' % (self.count,)
        self.s._getNextId = get_next_id

        def onListen(r, d):
            ds = self.s.connect(self.factory)
            ds.addCallback(onConnect, d)

        def onConnect(s, d):
            d1 = self.s.request(['aaa', 'aab'])
            d2 = self.s.request('bbb')
            reactor.callLater(0.2, check, d)
        
        def check(d):
            result = getattr(self.r, 'messages', [])
            expected = [['msg_id_1', ['aaa', 'aab']], ['msg_id_2', ['bbb']]]
            self.failUnlessEqual(
                result, expected, "Message should have been received")
            d.callback(True)

        d = defer.Deferred()
        dr = self.r.listen(self.factory)
        dr.addCallback(onListen, d)
        return d

    def test_send_recv_reply(self):
        
        def onListen(r, d):
            ds = self.s.connect(self.factory)
            ds.addCallback(onConnect, d)

        def onConnect(s, d):
            d1 = self.s.request('aaa')
            d1.addCallback(check_response, d)

        def check_response(response, d):
            self.assertEqual(response, ['aaa'])
            d.callback(True)

        d = defer.Deferred()
        dr = self.r.listen(self.factory)
        dr.addCallback(onListen, d)
        return d

    def test_lot_send_recv_reply(self):

        def onListen(r, d):
            ds = self.s.connect(self.factory)
            ds.addCallback(onConnect, d)

        def onConnect(s, d):
            reactor.callLater(0.2, perform_test, d)

        def perform_test(d):
            
            def check_response(response, identifier):
                expected = ['aaa']
                self.assertEqual(response, expected)
                return response == expected
       
            deferreds = []
            for i in range(1, 11):
                identifier = "msg_id_%d" % (i,)
                ds = self.s.request(['aaa'])
                deferreds.append(ds)
                ds.addCallback(check_response, identifier)
            dl = defer.DeferredList(deferreds)
            dl.addCallback(d.callback)
        
        d = defer.Deferred()
        dr = self.r.listen(self.factory)
        dr.addCallback(onListen, d)
        return d

    def test_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        
        def onListen(r, d):
            ds = self.s.connect(self.factory)
            ds.addCallback(onConnect, d)

        def onConnect(s, d):
            d1 = self.s.request('aaa')
            d1.addCallback(check, d)

        def check(ignore, d):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))
            d.callback(True)

        d = defer.Deferred()
        dr = self.r.listen(self.factory)
        dr.addCallback(onListen, d)
        return d