def test_getsockopt_events(self): sock1, sock2, _port = self.create_bound_pair(zmq.DEALER, zmq.DEALER) sleep() poll_out = zmq.Poller() poll_out.register(sock1, zmq.POLLOUT) sock_map = poll_out.poll(100) self.assertEqual(len(sock_map), 1) events = sock1.getsockopt(zmq.EVENTS) self.assertEqual(events & zmq.POLLOUT, zmq.POLLOUT) sock1.send(b'') poll_in = zmq.Poller() poll_in.register(sock2, zmq.POLLIN) sock_map = poll_in.poll(100) self.assertEqual(len(sock_map), 1) events = sock2.getsockopt(zmq.EVENTS) self.assertEqual(events & zmq.POLLIN, zmq.POLLIN)
def handle_nodes(): logger.debug("Binding socket...") context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("{}://{}:{}".format("tcp", "*", 5560)) poller = zmq.Poller() poller.register(socket, zmq.POLLIN) pub_socket = context.socket(zmq.PUB) pub_socket.bind("{}://{}:{}".format("tcp", "*", 5561)) logger.debug("socket is binded") while True: try: messages = dict(poller.poll(1000)) if socket in messages: msg = socket.recv() logger.info("Received message on REP socket " + msg.decode()) socket.send(b"READY") msg_header = msg.decode()[:4] msg_dict = json.loads(msg.decode()[5:]) # aquí iría un "switch" para cada posible header # este es solo para el header PEER if msg_header == "PEER" and msg_dict not in peer_list: with open(PEER_LIST_FILE, "w+") as f: peer_list.append(msg_dict) json.dump(peer_list, f) # XXX: Tendría ahora que haber uno para VOTE # que es ya el otro importante if msg_header == "VOTE": pass pub_socket.send(msg) else: eventlet.sleep(2) except Exception as e: logger.warning(e)
def __init__(self, clock=time.time): BaseHub.__init__(self, clock) self.poll = zmq.Poller()