def __init__(self, ip='127.0.0.1', port=50020, shouldRecord=False, shouldSave=False): self._ip = ip self._port = port self._subscribers = {} self.shouldRecord = shouldRecord self.shouldSave = shouldSave if shouldRecord: logging.info("Trying to connect to pupil labs...") self._ctx = zmq.Context() self._remote = zmq.Socket(self._ctx, zmq.REQ) self._remote.connect("tcp://{}:{}".format(self._ip, self._port)) self._remote.send_string("PUB_PORT") pub_port = self._remote.recv_string() self._pub_socket = zmq.Socket(self._ctx, zmq.PUB) self._pub_socket.connect("tcp://{}:{}".format(self._ip, pub_port)) # Setup zmq context and remote helper # In order for the annotations to be correlated correctly with the rest of # the data it is required to change Pupil Capture's time base to this scripts # clock. We only set the time base once. Consider using Pupil Time Sync for # a more precise and long term time synchronization self._time_fn = time # Use the appropriate time function here # Set Pupil Capture's time base to this scripts time. (Should be done before # starting the recording) self._remote.send_string("T {}".format(self._time_fn())) self._remote.recv_string() sleep(1.) else: logging.warning("Not connecting to pupil labs")
def start(self): """prepare the socks, return self to run join""" def _handle(sig, frames): self.SIG_QUEUE.append(sig) for sig in self.QUEUE_SIGS: signal(sig, _handle) ctx = zmq.Context() self.sub = zmq.Socket(ctx, zmq.SUB) self.sub.setsockopt(zmq.SUBSCRIBE, "{}-{}-{}".format(self.page_id,self.domain, self.controller)) self.sub.connect(self.config['WORKER_SOCK']) self.heartbeat_socket = zmq.Socket(ctx, zmq.PUSH) self.heartbeat_socket.connect(self.config['HEARTBEAT_SOCK']) self.db_conn = self.connect_db() self.cursor = self.db_conn.cursor() poller = zmq.Poller() poller.register(self.sub, zmq.POLLIN) self.poller = poller return self
def create_bound_pair(self, type1, type2, interface='tcp://127.0.0.1'): """Create a bound socket pair using a random port.""" s1 = zmq.Socket(self.context, type1) port = s1.bind_to_random_port(interface) s2 = zmq.Socket(self.context, type2) s2.connect('%s:%s' % (interface, port)) return s1, s2
def __init__(self, stype, cluster_name, cluster_index, usr=None, passwd=None): if cluster_index == 0: if usr is None or passwd is None: raise ValueError( 'The first node should have an api username and password') NSAPI.__init__(self, usr, passwd) else: NSAPI.__init__(self, '', '') self.stype = stype # Service type (_nsdeps._tcp.local.) self.cluster_name = cluster_name # Cluster name self.cluster_index = cluster_index # Cluster index self.zeroconf = Zeroconf() self.browser = ServiceBrowser(self.zeroconf, stype, self) self.displays = [] self.parent_ip = None self.zmq_context = zmq.Context.instance() self.zmq_out = zmq.Socket(zmq.PUB) self.zmq_out.bind('tcp://{}:{}'.format('*', self.BROADCAST_PORT)) if self.cluster_index > 0: self.zmq_in = zmq.Socket(zmq.SUB) self.zmq_in_poller = None self.zmq_in_connected = False self.running = True threading.Thread(target=self.receive_worker).start()
def broker(): pub = zmq.Socket(ctx, zmq.PUB) pull = zmq.Socket(ctx, zmq.PULL) pub.bind('ipc:///tmp/volttron-platform-agent-subscribe') pull.bind('ipc:///tmp/volttron-platform-agent-publish') while True: message = pull.recv_multipart() print(message) pub.send_multipart(message)
def init_socket(wid, pid, session_id): ctx = zmq.Context() GlobalSocket.pipe_from_cpp = zmq.Socket(ctx, zmq.PULL) GlobalSocket.pipe_from_cpp.bind("ipc://pyhusky-session-" + session_id + "-proc-" + pid + "-" + wid) GlobalSocket.pipe_to_cpp = zmq.Socket(ctx, zmq.PUSH) GlobalSocket.pipe_to_cpp.connect("ipc://cpphusky-session-" + session_id + "-proc-" + pid + "-" + wid)
def create_bound_pair(self, type1=zmq.PAIR, type2=zmq.PAIR, interface='tcp://127.0.0.1'): """Create a bound socket pair using a random port.""" s1 = zmq.Socket(self.context, type1) s1.setsockopt(zmq.LINGER, 0) port = s1.bind_to_random_port(interface) s2 = zmq.Socket(self.context, type2) s2.setsockopt(zmq.LINGER, 0) s2.connect('%s:%s' % (interface, port)) self.sockets.extend([s1,s2]) return s1, s2
def pupil_initialize(ip_address, port): ctx = zmq.Context() socket = zmq.Socket(ctx, zmq.REQ) socket.connect('tcp://' + str(ip_address) + ':' + str(port)) socket.send_string("PUB_PORT") pub_port = socket.recv_string() pub_socket = zmq.Socket(ctx, zmq.PUB) pub_socket.connect('tcp://' + str(ip_address) + ':{}'.format(pub_port)) return socket
def __init__(self, ctx, url, block_until_topic_subscribed='notify'): self.socket = zmq.Socket(ctx, zmq.PUB) self.socket.connect(url) if block_until_topic_subscribed: xpub = zmq.Socket(ctx, zmq.XPUB) xpub.connect(url) while True: if block_until_topic_subscribed in xpub.recv(): break xpub.close()
def broker_test(): pub = zmq.Socket(ctx, zmq.PUB) pull = zmq.Socket(ctx, zmq.PULL) pub.bind('ipc:///tmp/volttron-platform-agent-subscribe') pull.bind('ipc:///tmp/volttron-platform-agent-publish') pub.send_multipart(['topic1', 'Hello world1']) time.sleep(2) pub.send_multipart(['foo', 'bar']) time.sleep(2) pub.send_multipart(['topic2', 'Goodbye']) time.sleep(2) pub.send_multipart(['platform', 'Hello from platform']) time.sleep(2) pub.send_multipart(['platform.shutdown', 'Goodbye'])
def open_connection(self, type): if type == zmq.DEALER: self.socket = vip.Socket(self.context) if self._identity: self.socket.identity = self._identity else: self.socket = zmq.Socket()
def open_connection(self, type): if type == zmq.DEALER: self.socket = GreenSocket(self.context) if self._identity: self.socket.identity = self._identity.encode('utf-8') else: self.socket = zmq.Socket()
def __init__(self, ip, port): self.ctx = zmq.Context() self.sock = zmq.Socket(self.ctx,zmq.STREAM) self.sock.bind("tcp://{ip}:{port}".format(ip=ip, port = port)) self.clients = {} self.websockets = set()
def __init__(self, ip, port): self.ip = ip self.port = port self.ctx = zmq.Context() self.socket = zmq.Socket(self.ctx, zmq.REQ) self.logger = logging.getLogger(__name__) self.logger.info('Creating zeromq socket object')
def subscriber(): sub = zmq.Socket(ctx, zmq.SUB) sub.connect('ipc:///tmp/volttron-platform-agent-subscribe') sub.subscribe = '' while True: print sub.recv_multipart() print "777"
def connect_pupil_capture(): """connect to pupil capture using zmq protocol""" try: context = zmq.Context() socket = zmq.Socket(context, zmq.REQ) socket.connect('tcp://' + ip + ':' + str(port)) socket.send_string("PUB_PORT") pub_port = socket.recv_string() pub_socket = zmq.Socket(context, zmq.PUB) pub_socket.connect('tcp://' + ip + ':{}'.format(pub_port)) return socket, pub_socket except Exception as err: print('Cannot connect to Pupil Capture', err) sys.exit()
def zRead(self, message_parts): sock = zmq.Socket(self._ctx, zmq.REQ) sock.setsockopt(zmq.LINGER, 0) sock.connect("tcp://{}:{}".format(self._host, self._port)) for i, part in enumerate(message_parts): if i == len(message_parts) - 1: f = 0 else: f = zmq.SNDMORE print("Send {} {}".format(i, part)) sock.send(part, flags=f) print("Poll") # use poll for timeouts: poller = zmq.Poller() poller.register(sock, zmq.POLLIN) if poller.poll(5*1000): # 5s timeout in milliseconds recv = sock.recv_multipart() print("RX {}".format(recv)) sock.close() return recv else: raise IOError("Timeout processing ZMQ request")
def main(args): socket = zmq.Socket(zmq.Context.instance(), zmq.PULL) socket.connect(args.endpoint) ptrn = re.compile(' \[([^\]]+)\] ') i = 0 while True: raw = socket.recv() i += 1 if i % 50000 != 0: continue log = msgpack.unpackb(raw) mo = ptrn.search(log) ts = mo.group(1) dt = time.strptime(ts, '%d/%b/%Y:%H:%M:%S +0800') ts = time.mktime(dt) delta = time.time() - ts if delta < 600: print 'catch it' break print 'delta %d seconds' % int(delta)
def checkConnection(tcpAddress): ctx = zmq.Context() socket = zmq.Socket(ctx, zmq.REQ) socket.connect(tcpAddress) # set up a poller poller = zmq.Poller() poller.register(socket, zmq.POLLIN) socket.send_string('t') # check if sending/receiving will be successfull evts = poller.poll(1000) if not evts: connectionOK = False #this is a fatal error else: connectionOK = True # have to actually receive the str in order to continue t = socket.recv_string() poller.unregister(socket) return connectionOK, ctx, socket
def check_pgm_addr(addr): if addr.find('127.0.0.1') != -1: return False pid = os.fork() if pid > 0: r_pid, ret = os.wait() if ret != 0: warning("PGM address {} is invalid!".format(addr)) return False else: return True else: #quiet things down so fatal-looking error messages don't confuse people try: null_fd = os.open("/dev/null", os.O_RDWR) os.dup2(null_fd, sys.stdin.fileno()) os.dup2(null_fd, sys.stdout.fileno()) os.dup2(null_fd, sys.stderr.fileno()) zmq_context = zmq.Context(1) s = zmq.Socket(c, zmq.PUB) s.connect(addr) s.setsockopt(zmq.LINGER, 200) s.send('TESTING') zmq_context.term() #should _always_ exit - bad things will probably happen otherwise except: os._exit(1) finally: os._exit(0)
def _setup_authorization(self, instance_info): """ Setup authorized connection with remote instance :param instance_name: dicovery information(server key, name, vip-address) of remote instance :return: """ try: instance_name = instance_info['instance-name'] serverkey = instance_info['serverkey'] address = instance_info['vip-address'] web_address = instance_info['web-address'] except KeyError as exc: _log.error( "Missing parameter in instance info message {}".format(exc)) return sock = zmq.Socket(zmq.Context(), zmq.DEALER) num = random.random() # socket identity and zap domain must be bytes sock.identity = f"instance.{instance_name}.{num}".encode('utf-8') sock.zap_domain = b'vip' self._poller.register(sock, zmq.POLLIN) keystore = KeyStore() vip_address = f"{address}?serverkey={serverkey}&publickey={keystore.public}&secretkey={keystore.secret}" ext_platform_address = Address(vip_address) ext_platform_address.identity = sock.identity try: ext_platform_address.connect(sock) except zmq.error.ZMQError as ex: _log.error("ZMQ error on external connection {}".format(ex)) self._web_addresses.remove(web_address) if not self._web_addresses: _log.debug("MULTI_PLATFORM SETUP MODE COMPLETED")
def __init__(self, context, socket_type, io_loop=None): self.io_loop = io_loop or self._default_loop() self._recv_futures = deque() self._send_futures = deque() self._state = 0 self._sock = zmq.Socket(context, socket_type) self._init_io_state()
def connect(self, reply_to, endpoint): if self.connected: return # Create new outgoing socket (drop any messages in transit) self.mailbox = zmq.Socket(self._ctx, zmq.DEALER) # Set our caller 'From' identity so that receiving node knows # who each message came from. # Set our own identity on the socket so that receiving node # knows who each message came from. Note that we cannot use # the UUID directly as the identity since it may contain a # zero byte at the start, which libzmq does not like for # historical and arguably bogus reasons that it nonetheless # enforces. # we set linger to 0 by default (In zyre this is done by czmq's zsys) self.mailbox.setsockopt(zmq.LINGER, 0) self.mailbox.setsockopt(zmq.IDENTITY, b'1' + reply_to.bytes) # Set a high-water mark that allows for reasonable activity self.mailbox.setsockopt(zmq.SNDHWM, PyrePeer.PEER_EXPIRED * 100) # Send messages immediately or return EAGAIN self.mailbox.setsockopt(zmq.SNDTIMEO, 0) # Connect through to peer node logger.debug("Connecting to peer {0} on endpoint {1}".format( self.identity, endpoint)) self.mailbox.connect(endpoint) self.endpoint = endpoint self.connected = True self.ready = False
def _setup_authorization(self, instance_info): """ Setup authorized connection with remote instance :param instance_name: dicovery information(server key, name, vip-address) of remote instance :return: """ try: instance_name = instance_info['instance-name'] serverkey = instance_info['serverkey'] address = instance_info['vip-address'] web_address = instance_info['web-address'] except KeyError as exc: _log.error( "Missing parameter in instance info message {}".format(exc)) return sock = zmq.Socket(zmq.Context(), zmq.DEALER) num = random.random() sock.identity = 'platform-' + '-' + instance_name + '-' + str(num) sock.zap_domain = 'vip' self._poller.register(sock, zmq.POLLIN) keystore = KeyStore() vip_address = "{0}?serverkey={1}&publickey={2}&secretkey={3}".format( address, str(serverkey), str(keystore.public), str(keystore.secret)) ext_platform_address = Address(vip_address) ext_platform_address.identity = sock.identity try: ext_platform_address.connect(sock) except zmq.error.ZMQError as ex: _log.error("ZMQ error on external connection {}".format(ex)) self._web_addresses.remove(web_address) if not self._web_addresses: _log.debug("MULTI_PLATFORM SETUP MODE COMPLETED")
def __init__(self, ctx, url, topics=(), block_until_connected=True, hwm=None): self.socket = zmq.Socket(ctx, zmq.SUB) assert type(topics) != str if hwm is not None: self.socket.set_hwm(hwm) if block_until_connected: # connect node and block until a connecetion has been made monitor = self.socket.get_monitor_socket() self.socket.connect(url) while True: status = recv_monitor_message(monitor) if status["event"] == zmq.EVENT_CONNECTED: break elif status["event"] == zmq.EVENT_CONNECT_DELAYED: pass else: raise Exception("ZMQ connection failed") self.socket.disable_monitor() else: self.socket.connect(url) for t in topics: self.subscribe(t)
def __init__(self, socktype=zmq.DEALER): self.ctx = zmq.Context.instance() self.socket = zmq.Socket(self.ctx, socktype) self.socket.setsockopt(zmq.LINGER, 0) self.socket.setsockopt(zmq.SWAP, 25000000) # 25MB disk swap address = "tcp://{0}".format(config.address) self.socket.connect(address)
def connect(port): """Create a connection to the dabmux stats server returns: the socket""" sock = zmq.Socket(ctx, zmq.REQ) sock.connect("tcp://localhost:%s" % (port)) return sock
def connect(self): """ Connects to defined zmq socket. """ self.socket.connect(f'tcp://{self.ip}:{self.port}') self.socket.send_string('PUB_PORT') self.pub_port = self.socket.recv_string() self.pub_socket = zmq.Socket(self.ctx, zmq.PUB) self.pub_socket.connect(f"tcp://{self.ip}:{self.pub_port}")
def create_pipe(ctx, hwm=1000): socket0 = zmq.Socket(ctx, zmq.PAIR) socket0.set_hwm(hwm) socket0.setsockopt(zmq.LINGER, 0) socket1 = zmq.Socket(ctx, zmq.PAIR) socket1.set_hwm(hwm) socket1.setsockopt(zmq.LINGER, 0) while True: try: endpoint = "inproc://$pipe-{}".format(random.randint( 1, 9999999999)) socket1.bind(endpoint) except zmq.ZMQError: continue else: break socket0.connect(endpoint) return socket1, socket0
def setup_subscriber(self): #Subscribe to sub topic ctx = zmq.Context() self._sub = zmq.Socket(ctx, zmq.SUB) self._sub.connect(subscribe_address) #Setup a poller for use with the subscriber self._poller = zmq.Poller() self._poller.register(self._sub)