Example #1
0
 def test_1_msg_connectpub(self):
     raise nose.exc.SkipTest("I have no idea why this test doesn't pass. It really should. It does not. Sadness.")
     url, sub = cchooser(zmqsub.BindSub)
     debugp('using url %s' % url)
     debugp("bound for sub")
     pub = zmqsub.ConnectPub(url, context=sub)
     debugp("connected, selecting for write")
     r, w, x = zmqsub.select([], [pub], [], 1.0)
     debugp("selected for write, checking")
     self.assertEquals(1, len(w))
     debugp("write ready, writing")
     pub.send({u'您好': u'World'})
     time.sleep(2.0)
     debugp("selecting for read..")
     r, w, x = zmqsub.select([sub], [], [], 1.0)
     debugp("selected for read")
     self.assertEquals(1, len(r))
     debugp("read ready, reading")
     self.assertEquals({u'您好': u'World'}, sub.recv(timeout=3.0))
     debugp("the value made it through fine.")
Example #2
0
 def test_1_msg_connectpub(self):
     raise nose.exc.SkipTest(
         "I have no idea why this test doesn't pass. It really should. It does not. Sadness."
     )
     url, sub = cchooser(zmqsub.BindSub)
     debugp('using url %s' % url)
     debugp("bound for sub")
     pub = zmqsub.ConnectPub(url, context=sub)
     debugp("connected, selecting for write")
     r, w, x = zmqsub.select([], [pub], [], 1.0)
     debugp("selected for write, checking")
     self.assertEquals(1, len(w))
     debugp("write ready, writing")
     pub.send({u'您好': u'World'})
     time.sleep(2.0)
     debugp("selecting for read..")
     r, w, x = zmqsub.select([sub], [], [], 1.0)
     debugp("selected for read")
     self.assertEquals(1, len(r))
     debugp("read ready, reading")
     self.assertEquals({u'您好': u'World'}, sub.recv(timeout=3.0))
     debugp("the value made it through fine.")
Example #3
0
    def run(self):
        # ZMQ+tcp+json input - SUB
        self.adaptors_in = zmqsub.BindSub(self.adaptor_url)
        # UDP+msgpack input
        self.udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.udpsock.bind(('0.0.0.0', INPUT_PORT))
        # TODO on end, unbind that..

        self.directory_out = zmqsub.BindPub(self.directory_url)
        self.sent_directory = time.time() - DIRECTORY_INTERVAL
        while self.ok:
            r, _w, _x = zmqsub.select([self.udpsock, self.adaptors_in], [], [], BLOCK_TIME)

            msgs = []

            # TODO handle the situation where fairness between the 2 methods of input can be too much,
            # resulting in one of the inputs being starved for consumption
            for sock in r:
                if sock is self.adaptors_in:
                    msgs.append(self.adaptors_in.recv(timeout=BLOCK_TIME))
                    if self.verbose:
                        print('recvd zmq adaptor data.')
                elif sock is self.udpsock:
                    data, addr = self.udpsock.recvfrom(4096)
                    if self.verbose:
                        print('recvd udp adaptor data')
                    
                    if data is None:
                        continue

                    message = self.decode_mpack(data, verbose=self.verbose)
                    if message is not None:
                        msgs.append(message)
            for msg in msgs:
                try:
                    output_event = self.directory.handle_message(msg, accept_listmsg=True)
                    if output_event:
                        self.directory_out.send(output_event)
                        if self.verbose:
                            print('sent event update for %s.' % output_event['ns'])
                except thing.BadMessageException as bme:
                    if self.verbose:
                        print('recvd bad message, skipped reason: %s' % str(bme))

            now = time.time()
            if now > self.sent_directory + DIRECTORY_INTERVAL:
                # time to send out a snapshot, for fun!
                # maybe we should send snapshots on another socket, later.
                snapshot_msg = {
                    'type': 'thing_snapshot',
                    'ts': now,
                    'data': dict([
                        (thing_obj.ns, thing_obj.emit_snapshot())
                        for
                        thing_obj
                        in
                        self.directory.all_things
                        if not thing_obj.expired
                    ])
                }
                self.directory_out.send(snapshot_msg)
                if self.verbose:
                    print('sent snapshot of %d things.' % len(snapshot_msg['data']))
                self.sent_directory = now