def __init__(self, url='zmq+tcp://localhost:5555+5556', channel='socketio', write_only=False, logger=None): if zmq is None: raise RuntimeError('zmq package is not installed ' '(Run "pip install pyzmq" in your ' 'virtualenv).') r = re.compile(':\d+\+\d+$') if not (url.startswith('zmq+tcp://') and r.search(url)): raise RuntimeError('unexpected connection string: ' + url) url = url.replace('zmq+', '') (sink_url, sub_port) = url.split('+') sink_port = sink_url.split(':')[-1] sub_url = sink_url.replace(sink_port, sub_port) sink = zmq.Context().socket(zmq.PUSH) sink.connect(sink_url) sub = zmq.Context().socket(zmq.SUB) sub.setsockopt_string(zmq.SUBSCRIBE, u'') sub.connect(sub_url) self.sink = sink self.sub = sub self.channel = channel super(ZmqManager, self).__init__(channel=channel, write_only=write_only, logger=logger)
def assert_different(ctx): try: this_thread_context = zmq.Context() except: test_result.append('fail') raise test_result.append(ctx is this_thread_context)
def shutdown_listener(): ctx = zmq.Context() # Request - act as client socket = ctx.socket(zmq.REQ) socket.connect('tcp://localhost:%s' % config.ZEROMQ_PORT) socket.send('KILL') socket.recv() # ignore reply
def open_sockets(self): self.zmq_context = zmq.Context() LOG.warn("pyzmq version is %s" % zmq.pyzmq_version()) #*********************************************************************# #* Create ROUTER socket for Felix instances to connect to. *# #*********************************************************************# self.felix_router_socket = self.zmq_context.socket(zmq.ROUTER) self.felix_router_socket.bind("tcp://*:%s" % PLUGIN_ENDPOINT_PORT) #*********************************************************************# #* Create ROUTER socket for ACL Manager(s) to connect to. *# #*********************************************************************# self.acl_get_socket = self.zmq_context.socket(zmq.ROUTER) self.acl_get_socket.bind("tcp://*:%s" % PLUGIN_ACLGET_PORT) #*********************************************************************# #* Create PUB socket for sending ACL updates to ACL Manager(s). *# #*********************************************************************# self.acl_pub_socket = self.zmq_context.socket(zmq.PUB) self.acl_pub_socket.bind("tcp://*:%s" % PLUGIN_ACLPUB_PORT) #*********************************************************************# #* Spawn green thread for handling RESYNCSTATE requests on the *# #* Felix-ROUTER socket. *# #*********************************************************************# eventlet.spawn_n(self.felix_router_thread) #*********************************************************************# #* Spawn green thread for handling GETGROUPS and GETRULES requests *# #* on the ACL-GET socket. *# #*********************************************************************# eventlet.spawn_n(self.acl_get_thread) LOG.info("Started threads")
def connect(self): context = zmq.Context() inproc_server = context.socket(zmq.PULL) ipc_socket = cfg.CONF.df.publisher_multiproc_socket LOG.debug("about to bind to IPC socket: %s" % ipc_socket) inproc_server.bind('ipc://%s' % ipc_socket) return inproc_server
def register_opts(conf): """Registration of options for this driver.""" #NOTE(ewindisch): ZMQ_CTX and matchmaker # are initialized here as this is as good # an initialization method as any. # We memoize through these globals global ZMQ_CTX global matchmaker global CONF if not CONF: conf.register_opts(zmq_opts) CONF = conf # Don't re-set, if this method is called twice. if not ZMQ_CTX: ZMQ_CTX = zmq.Context(conf.rpc_zmq_contexts) if not matchmaker: # rpc_zmq_matchmaker should be set to a 'module.Class' mm_path = conf.rpc_zmq_matchmaker.split('.') mm_module = '.'.join(mm_path[:-1]) mm_class = mm_path[-1] # Only initialize a class. if mm_path[-1][0] not in string.ascii_uppercase: LOG.error(_("Matchmaker could not be loaded.\n" "rpc_zmq_matchmaker is not a class.")) raise RPCException(_("Error loading Matchmaker.")) mm_impl = importutils.import_module(mm_module) mm_constructor = getattr(mm_impl, mm_class) matchmaker = mm_constructor()
def initialize(self): self.zmq_context = zmq.Context() LOG.warn("pyzmq version is %s" % zmq.pyzmq_version()) bind_address = cfg.CONF.bind_host or '*' # Create ROUTER socket for Felix instances to connect to. self.felix_router_socket = self.zmq_context.socket(zmq.ROUTER) self.felix_router_socket.bind("tcp://%s:%s" % (bind_address, PLUGIN_ENDPOINT_PORT)) # Create ROUTER socket for ACL Manager(s) to connect to. self.acl_get_socket = self.zmq_context.socket(zmq.ROUTER) self.acl_get_socket.bind("tcp://%s:%s" % (bind_address, PLUGIN_ACLGET_PORT)) # Create PUB socket for sending ACL updates to ACL Manager(s). self.acl_pub_socket = self.zmq_context.socket(zmq.PUB) self.acl_pub_socket.bind("tcp://%s:%s" % (bind_address, PLUGIN_ACLPUB_PORT)) eventlet.spawn(self.acl_heartbeat_thread) # Spawn green thread for handling RESYNCSTATE requests on the # Felix-ROUTER socket. eventlet.spawn(self.felix_router_thread) # Spawn green thread for handling GETGROUPS requests on the ACL-GET # socket. eventlet.spawn(self.acl_get_thread) LOG.info("Started threads")
def listen(): context = zmq.Context() socket = context.socket(zmq.SUB) #socket.setsockopt(zmq.SUBSCRIBE,"1") socket.setsockopt_string(zmq.SUBSCRIBE, '') zmq_url = "tcp://%s:%s" % (options.zmqhost, options.zmqport) print("ZMQ: subscribe to %s" % zmq_url) socket.connect(zmq_url) while True: string = socket.recv().decode('utf-8') sys.stdout.flush() inp = json.loads(string) print(inp) x = inp["x"] y = inp["y"] z = inp["z"] rx = inp["rx"] ry = inp["ry"] rz = inp["rz"] inp["x"] = (-1 * x) + 0.35 # X-pos ligt naar links inp["y"] = z - 0.08 # Ik ga er van uit dat de Z as omhoog wijst uit het vlak inp["z"] = y + 0.2 inp["rx"] = math.radians(-90) + rx inp["ry"] = -ry inp["rz"] = rz print(inp) json.dumps(inp, sort_keys=True) socketio.emit('posupdate', inp)
def __init__(self, port=9999, **kwargs): from eventlet.green import zmq self._port = port self._url = 'tcp://*:' + str(port) self._context = zmq.Context(**kwargs) self._recv_sock = self._context.socket(zmq.PULL) self._recv_sock.bind(self._url)
def init_zmq(self): self.context = zmq.Context() self.server = self.context.socket(zmq.ROUTER) bind_addr = self.conf.get('bind_addr', 'ipc:///tmp/run/event-agent.sock') self.server.bind(bind_addr) self.backend = self.context.socket(zmq.DEALER) self.backend.bind('inproc://event-front')
def __init__(self, *args, **kwargs): super(Driver, self).__init__(*args, **kwargs) self.publisherCtx = zmq.Context() self.publisher = self.publisherCtx.socket(zmq.PUB) self.publisher.bind("ipc:///tmp/controller.ipc") self.publisherTopic = "controller" self.publisherTopoTopic = "topology" self.subscriberCtx = zmq.Context() self.subscriber = self.subscriberCtx.socket(zmq.SUB) self.subscriber.connect("ipc:///tmp/sdk4sdn.ipc") self.subscriber.setsockopt(zmq.SUBSCRIBE, 'sdk4sdn') # Start the thread to handle events from the SDK4SDN self.subscriberThread = hub.spawn(self.sdk4sdn_handler) self.dpstore = {}
def __init__(self): super(ZMQPublisherAgent, self).__init__() self._endpoint = "{}://{}:{}".format( cfg.CONF.df.publisher_transport, cfg.CONF.df.publisher_bind_address, cfg.CONF.df.publisher_port, ) self.context = zmq.Context()
def init(self): context = zmq.Context() context.setsockopt(socket.SO_REUSEADDR, 1) self._socket = context.socket(zmq.REQ) self._socket.setsockopt(zmq.SNDTIMEO, 5) self._socket.setsockopt(zmq.RCVTIMEO, 5) self._socket.setsockopt(zmq.LINGER, 0) self._socket.connect(self._conn)
def cb(gh, nfmsg, nfa, data): ev = decode(nfa) msg = jsonutils.dumps(ev) + '\n' ctx = zmq.Context(1) pub = ctx.socket(zmq.XREQ) pub.bind(ADDR_IPC) pub.send(msg.encode('utf-8')) pub.close() return 0
def create_bound_pair(self, type1, type2, interface='tcp://127.0.0.1'): """Create a bound socket pair using a random port.""" self.context = context = zmq.Context() s1 = context.socket(type1) port = s1.bind_to_random_port(interface) s2 = context.socket(type2) s2.connect('%s:%s' % (interface, port)) self.sockets = [s1, s2] return s1, s2, port
def send_update(update): data = {'msg': update.msg, 'timestamp': str(update.timestamp)} ctx = zmq.Context() # Request - act as client socket = ctx.socket(zmq.REQ) socket.connect('tcp://localhost:%s' % config.ZEROMQ_PORT) socket.send(json.dumps(data)) reply = socket.recv() print('ZEROMQ reply -', reply)
def connect(self): context = zmq.Context() socket = context.socket(zmq.SUB) for uri in self.uri_list: #TODO(gampel) handle exp zmq.EINVAL,zmq.EPROTONOSUPPORT LOG.debug("about to connect to network publisher at %s" % uri) socket.connect(uri) for topic in self.topic_list: socket.setsockopt(zmq.SUBSCRIBE, topic) return socket
def loop(): while True: if self.callback: ctx = zmq.Context(1) sub = ctx.socket(zmq.XREQ) sub.connect(ADDR_IPC) msg = sub.recv() if len(msg): self.callback(jsonutils.loads(msg)) sub.close() time.sleep(1.0)
def __init__(self, backend, host, port): threading.Thread.__init__(self) self.host = host self.port = port self.backend_info = {'negotiated_protocols':{}, 'datapaths':{}, 'connected' : False} self.running_modules = {} self.of_datapath = None self.ofproto = None self.ofproto_parser = None self.backend = backend self.heartbeat_time = time.time() context = zmq.Context() self.socket = context.socket(zmq.DEALER) self.socket.setsockopt(zmq.IDENTITY, self.backend.backend_name)
def test_different_context_in_different_thread(self): context = zmq.Context() test_result = [] def assert_different(ctx): try: this_thread_context = zmq.Context() except: test_result.append('fail') raise test_result.append(ctx is this_thread_context) Thread(target=assert_different, args=(context,)).start() while not test_result: sleep(0.1) self.assertFalse(test_result[0])
def run(self): context = zmq.Context() self.socket = context.socket(zmq.DEALER) self.socket.setsockopt(zmq.IDENTITY, self.id) logger.debug('Connecting to Core on %s:%s...', self.host, self.port) self.socket.connect("tcp://" + str(self.host) + ":" + str(self.port)) # self.socket.send(b"First Hello from " + self.id) while True: message = self.socket.recv_multipart() msg = self.get_multipart_message(message) self.handle_read(msg) self.socket.close() context.term()
def run(self): context = zmq.Context() self.socket = context.socket(zmq.ROUTER) self.socket.setsockopt(zmq.IDENTITY, self.id) self.socket.bind('tcp://*:5555') while True: message = self.socket.recv_multipart() # msg = self.get_multipart_message(message) # print "whuwzp: get a msg:",msg print "========================================" print "whuwzp: get a message:", message self.handle_read(message) self.socket.close() context.term()
def receive(self): context = zmq.Context() self._socket = context.socket(zmq.REP) self._socket.setsockopt(zmq.SNDTIMEO, 5) self._socket.setsockopt(zmq.RCVTIMEO, 5) self._socket.setsockopt(zmq.LINGER, 0) self._socket.bind("tcp://%s:%d" % (self._ip, self._port)) while self._running: # Wait for next request from client try: LOG.info('waiting for message') message = self._socket.recv() LOG.info('received %s' % message) response = self.call_agent(message) LOG.info('sending %s' % response) self._socket.send(response, zmq.NOBLOCK) LOG.info('%s sent' % response) except Exception as e: LOG.error('an exception occured %s, %s' % (e, traceback.format_exc()))
def broadcast_vote(options, proofs, signature): """ Emite una papeleta cifrada a los nodos conocidos en un formato reconocible por ellos. :param options: el texto cifrado de la opción escogida. :param proofs: las pruebas DCP. :param signature: la firma digital. """ socket = zmq.Context().socket(zmq.DEALER) for peer in peer_list: socket.connect("tcp://{}:{}".format(peer["ip_address"], peer["rep_port"])) socket.send(b"", zmq.SNDMORE) socket.send(b"VOTE " + json.dumps({ "options": options, "proofs": proofs, "signature": signature, }).encode()) socket.close()
def main(): logging.basicConfig( format="%(asctime)s:%(levelname)s:%(name)s:%(threadName)s:%(message)s", level=logging.DEBUG) endpoint = os.environ.get( 'STEM_CONTROL_ENDPOINT', 'tcp://127.0.0.1:3900') ctx = zmq.Context() controller = SimController(ctx, endpoint) def signal_handler(s, f): log.info("Signal received stopping") controller.stop() signal.signal(signal.SIGINT, signal_handler) log.info("Starting webserver") controller.threads.append(eventlet.spawn(web.start_server, controller)) log.info("Starting controller") controller.run()
def _send(self, host, port, data): retry = 0 data = json.encoder.JSONEncoder().encode(data) received = None while not received: try: LOG.debug('%d: sending to %s:%s (%s)' % (retry, host, port, data)) context = zmq.Context() _socket = context.socket(zmq.REQ) _socket.connect('tcp://%s:%d' % (host, port)) _socket.send(data) received = _socket.recv() except Exception as e: LOG.error(traceback.format_exc()) LOG.error('%s' % sys.exc_info()[0]) time.sleep(1) if retry == 9: raise e retry = retry + 1 finally: _socket.close()
def listen_thread(): ctx = zmq.Context() # Reply - act as server socket = ctx.socket(zmq.REP) socket.bind('tcp://*:%s' % config.ZEROMQ_PORT) while True: request = socket.recv() print('ZEROMQ request -', request) socket.send('OK') if request == 'KILL': socket.close() print('Socket closed.') break with webapp.app_context(): data = json.loads(request) print('Sending to SocketIO ...') socketio.emit( 'new_update', data['msg'] + ' at ' + data['timestamp'], namespace='/browser', )
def run(self): self.ctx = zmq.Context() publishers = self.config['PUBLISHERS'] multiply = self.config['MULTIPLY'] bind_host = self.config['BIND_HOST'] port = self.config['START_PORT'] workers = self.config['WORKERS'] pool = eventlet.greenpool.GreenPool(size=len(publishers) * workers) for publisher in publishers: queues = [eventlet.queue.Queue() for _ in xrange(workers)] pool.spawn_n(self.receiver, publisher, queues) for queue in queues: pub_sock = self.ctx.socket(zmq.PUB) (bind_addr, new_port) = try_bind(pub_sock, bind_host, port) port = new_port + 1 pool.spawn_n(self.replicator, queue, bind_addr, pub_sock, multiply) pool.waitall()
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, frame_len, filename=None, zmq_addr=None, fps=None): # Save values self.fps = fps self.frame_len = frame_len # Can't be both if (filename is not None) and (zmq_addr is not None): raise RuntimeError("Can't read both from file and from ZMQ !") # Init file if filename is not None: self._file = file(filename, 'rb') self._read = self._read_file # Init ZMQ else: self._zmq = zmq.Socket(zmq.Context(), zmq.SUB) self._zmq.connect(zmq_addr) self._zmq.setsockopt(zmq.SUBSCRIBE, "") self._read = self._read_zmq self._frame = '' # Start the thread eventlet.spawn(self._run)