def _worker(self,worker_url, worker_id=uuid.uuid4()): if (sys.platform.lower().find("linux") >= 0): setproctitle.setproctitle("server-worker") lib.debug.info (worker_url) context = zmq.Context() # Socket to talk to dispatcher socket = context.socket(zmq.REP) socket.poll(timeout=1) socket.connect(worker_url) while True: (request_id_rep, state_name_rep, topic_rep, msg_rep) = socket.recv_multipart() rep_sock.send_multipart([request_id_rep, state_name, msg_rep]) lib.debug.debug(msg_rep) try: msg_reved = simplejson.loads(msg_rep) if (msg_reved['status'] == "free"): hosts_recieved[msg_reved['hostid']] = msg_reved lib.debug.debug("sending state : " + state_name_rep + " : ") else: return (msg_reved['status'] + " : " + msg_reved['request_id']) except: lib.debug.error(str(state_name) + " : " + str(request_id) + " : " + str(sys.exc_info())) return (str(state_name) + " : " + str(request_id) + " : " + str(sys.exc_info())) while True: received = socket.recv_multipart() lib.debug.info("Received request: [ {0} ] -> [ {1} ]".format(str(worker_id),msg_type_args)) reply = self.process(received) reply_to_send = simplejson.dumps(reply) socket.send_multipart([bytes(unicode(hostid)),bytes(unicode(request_id)),bytes(unicode(reply_to_send))]) lib.debug.info("Replied to request: [ {0} ] -> [ {1} ]".format(str(worker_id), msg_type_args))
def handler(socket): """ event loop - processes replies to sent messages """ while True: ti = str(threading.currentThread().ident) print ti + "] Waiting for a reply" r = socket.recv_multipart() print ti + "] Got reply: ", r if len(r) == 2: msgtxt = r[0] msgid = r[1] callback_registry_lock.acquire() if msgid in callback_registry: print ti + "] Reply is good." cbthread = threading.Thread(target = callback_registry[msgid], args=(msgtxt,)) cbthread.start() del callback_registry[msgid] else: print ti + "] Reply is bad (unexpected). Discarding." callback_registry_lock.release()
def get_replay(name, query, config, context=None): endpoint = config.get('replay_endpoints', {}).get(name, None) if not endpoint: raise IOError("No appropriate replay endpoint " "found for {0}".format(name)) if not context: context = zmq.Context(config['io_threads']) # A replay endpoint isn't PUB/SUB but REQ/REP, as it allows # for bidirectional communication socket = context.socket(zmq.REQ) try: socket.connect(endpoint) except zmq.ZMQError as e: raise IOError("Error when connecting to the " "replay endpoint: '{0}'".format(str(e))) # REQ/REP dance socket.send(fedmsg.encoding.dumps(query)) msgs = socket.recv_multipart() socket.close() for m in msgs: try: yield fedmsg.encoding.loads(m) except ValueError: # We assume that if it isn't JSON then it's an error message raise ValueError(m)
def recv(cls, socket, routed=False): message = socket.recv_multipart() if routed: split = message.index("") + 1 routing = message[:split] message = message[split:] else: routing = [] module, classname = message[:2] buffers = message[3:] attribute_dict = json_decode(message[2], buffers) try: instance = sys.modules[module].__dict__[classname](**attribute_dict) except: print "Communicable could not instantiate %s from module %s with kwargs %s" % ( module, classname, attribute_dict, ) raise instance._remote = True instance._routing = routing instance._socket = socket instance._replied = False return instance
def _read_one(socket): name, req_id, action, return_type, ser_type, opts = socket.recv_multipart() msg = { 'req_id': int(req_id), 'action': action.decode(), 'return_type': return_type.decode(), 'ser_type': ser_type.decode(), 'opts': opts, } return name, msg
def watch_batch(self, batch_id): # Setup a connection to the validator ctx = zmq.Context() socket = ctx.socket(zmq.DEALER) socket.connect(current_app.config['SAWTOOTH_VALIDATOR_URL']) # Construct the request request = client_batch_submit_pb2.ClientBatchStatusRequest( batch_ids=[batch_id], wait=True).SerializeToString() # Construct the message wrapper correlation_id = batch_id + uuid.uuid4( ).hex # This must be unique for all in-process requests msg = Message(correlation_id=correlation_id, message_type=Message.CLIENT_BATCH_STATUS_REQUEST, content=request) # Send the request socket.send_multipart([msg.SerializeToString()]) # Receive the response resp = socket.recv_multipart()[-1] # Parse the message wrapper msg = Message() msg.ParseFromString(resp) # Validate the response type if msg.message_type != Message.CLIENT_BATCH_STATUS_RESPONSE: current_app.logger.error("Unexpected response message type") return # Parse the response response = client_batch_submit_pb2.ClientBatchStatusResponse() response.ParseFromString(msg.content) # Validate the response status if response.status != client_batch_submit_pb2.ClientBatchSubmitResponse.OK: current_app.logger.error("watch batch status failed: {}".format( response.response_message)) return # Close the connection to the validator socket.close() return client_batch_submit_pb2.ClientBatchStatus.Status.Name( response.batch_statuses[0].status)
def _reply_heartbeat(self, target): """Worker will kill its jobs when it lost connection with the master. """ socket = self.ctx.socket(zmq.REP) socket.linger = 0 socket.setsockopt(zmq.RCVTIMEO, remote_constants.HEARTBEAT_RCVTIMEO_S * 1000) heartbeat_master_port =\ socket.bind_to_random_port("tcp://*") self.master_heartbeat_address = "{}:{}".format(self.worker_ip, heartbeat_master_port) logger.set_dir( os.path.expanduser('~/.parl_data/worker/{}'.format( self.master_heartbeat_address.replace(':', '_')))) self.heartbeat_socket_initialized.set() logger.info("[Worker] Connect to the master node successfully. " "({} CPUs)".format(self.cpu_num)) while self.master_is_alive and self.worker_is_alive: try: message = socket.recv_multipart() worker_status = self._get_worker_status() socket.send_multipart([ remote_constants.HEARTBEAT_TAG, to_byte(str(worker_status[0])), to_byte(str(worker_status[1])), to_byte(worker_status[2]), to_byte(str(worker_status[3])) ]) except zmq.error.Again as e: self.master_is_alive = False except zmq.error.ContextTerminated as e: break socket.close(0) logger.warning( "[Worker] lost connection with the master, will exit reply heartbeat for master." ) self.worker_status.clear() self.log_server_proc.kill() self.log_server_proc.wait() # exit the worker self.worker_is_alive = False self.exit()
def update_readable(self, socket): m = socket.recv_multipart() topic = m[0] self.rx.ParseFromString(m[1]) if self.debug: print("status_update ", topic, str(self.rx)) if self.rx.type == MT_HALRCOMP_INCREMENTAL_UPDATE: # incremental update for rp in self.rx.pin: lp = self.pinsbyhandle[rp.handle] self.pin_update(rp, lp) return if self.rx.type == MT_HALRCOMP_FULL_UPDATE: # full update if self.rx.uuid: self.set_uuid(self.rx.uuid) if self.rx.pparams: if self.debug: print("pparams keepalive=", self.rx.pparams.keepalive_timer) for c in self.rx.comp: for rp in c.pin: lname = str(rp.name) if "." in lname: # strip comp prefix cname, pname = lname.split(".", 1) lp = self.pinsbyname[pname] lp.handle = rp.handle self.pinsbyhandle[rp.handle] = lp self.pin_update(rp, lp) self.synced = True return if self.rx.type == MT_PING: # for now, ignore # TBD: add a timer, reset on any message received # if timeout, emit "rcomp channel down" signal if self.debug: print("halrcomp ping") return if self.rx.type == MT_HALRCOMP_ERROR: print("proto error on subscribe: ", str(self.rx.note)) self.emit('state', state.ERROR, str(self.rx.note)) print("status_update: unknown message type: ", str(self.rx))
def update_readable(self, socket): m = socket.recv_multipart() topic = m[0] self.rx.ParseFromString(m[1]) if self.debug: print "status_update ", topic, str(self.rx) if self.rx.type == MT_HALRCOMP_INCREMENTAL_UPDATE: # incremental update for rp in self.rx.pin: lp = self.pinsbyhandle[rp.handle] self.pin_update(rp,lp) return if self.rx.type == MT_HALRCOMP_FULL_UPDATE: # full update if self.rx.uuid: self.set_uuid(self.rx.uuid) if self.rx.pparams: if self.debug: print "pparams keepalive=", self.rx.pparams.keepalive_timer for c in self.rx.comp: for rp in c.pin: lname = str(rp.name) if "." in lname: # strip comp prefix cname,pname = lname.split(".",1) lp = self.pinsbyname[pname] lp.handle = rp.handle self.pinsbyhandle[rp.handle] = lp self.pin_update(rp,lp) self.synced = True return if self.rx.type == MT_PING: # for now, ignore # TBD: add a timer, reset on any message received # if timeout, emit "rcomp channel down" signal if self.debug: print "halrcomp ping" return if self.rx.type == MT_HALRCOMP_ERROR: print "proto error on subscribe: ",str(self.rx.note) self.emit('state', state.ERROR,str(self.rx.note)) print "status_update: unknown message type: ",str(self.rx)
def recv(cls, socket, routed=False): message = socket.recv_multipart() if routed: split = message.index('') + 1 routing = message[:split] message = message[split:] else: routing = [] module, classname = message[:2] buffers = message[3:] attribute_dict = json_decode(message[2], buffers) try: instance = sys.modules[module].__dict__[classname](**attribute_dict) except: print "Communicable could not instantiate %s from module %s with kwargs %s" % (module, classname, attribute_dict) raise instance._remote = True instance._routing = routing instance._socket = socket instance._replied = False return instance
def _recv_callback(self, socket, event): ''' This will receive all messages ''' # Ignore message that are not POLLIN if event is not zmq.POLLIN: return # Ignore messages on closed sockets if self.socket.closed: return try: n = self._threads while n: msg = socket.recv_multipart(flags=zmq.NOBLOCK) self._thread_pool.apply_async(self._recv_thread, args=(msg, self.receive_plugins)) n -= 1 except zmq.ZMQError as e: if e.errno is not zmq.EAGAIN: raise
def _reply_heartbeat(self): """Reply heartbeat signals to the master node.""" socket = self.ctx.socket(zmq.REP) socket.linger = 0 socket.setsockopt(zmq.RCVTIMEO, remote_constants.HEARTBEAT_RCVTIMEO_S * 1000) reply_master_heartbeat_port =\ socket.bind_to_random_port(addr="tcp://*") self.reply_master_heartbeat_address = "{}:{}".format( get_ip_address(), reply_master_heartbeat_port) self.heartbeat_socket_initialized.set() connected = False while self.client_is_alive and self.master_is_alive: try: message = socket.recv_multipart() elapsed_time = datetime.timedelta(seconds=int(time.time() - self.start_time)) socket.send_multipart([ remote_constants.HEARTBEAT_TAG, to_byte(self.executable_path), to_byte(str(self.actor_num)), to_byte(str(elapsed_time)), to_byte(str(self.log_monitor_url)), ]) # TODO: remove additional information except zmq.error.Again as e: if connected: logger.warning("[Client] Cannot connect to the master." "Please check if it is still alive.") else: logger.warning( "[Client] Cannot connect to the master." "Please check the firewall between client and master.(e.g., ping the master IP)" ) self.master_is_alive = False socket.close(0) logger.warning("Client exit replying heartbeat for master.")
def get_reply(socket): return socket.recv_multipart()
def get_ip(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('www.google.com', 80)) ip = s.getsockname()[0] s.close() return ip myip = get_ip() socket.connect(args.connect_address) command = 'LIST' socket.send_string(command) peers = socket.recv_multipart()[0].split(' ') cities = """Berlin London Paris Dublin """.splitlines() def get_cities(): for c in cities: if c: yield c def worker(peer):
socket.setsockopt(zmq.SUBSCRIBE, "ses_vc_switch") except Exception, e: self.logger.warning("Can not connect to msg_svr %s %s", self.msg_svr, e) self.logger.debug("Starting to gather ad information...") updateTimerStart = time.time() self.updateCachedAd() while True: now = time.time() if now - updateTimerStart > self.addbUpInterval: self.logger.debug("Update the cached ad guid...") self.updateCachedAd() updateTimerStart = now self.logger.debug("Receive....") evtType, data = socket.recv_multipart() if evtType == "ses_vc_switch": events = event_pack.unpack(data) self.logger.debug("Received len %s", len(events)) self.logger.debug("data: %s", events) self.vc_switchEventHandler(events[0]) else: self.logger.warning("Get wrong msg %s", evtType) def loadcfg(path): try: f = open(path) content = f.read() name, val = xmlhelper.parseSimpleXmlConfig(content)
def get_ip(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('www.google.com', 80)) ip = s.getsockname()[0] s.close() return ip myip = get_ip() socket.connect(args.connect_address) command = 'LIST' socket.send_string(command) peers = socket.recv_multipart()[0].split(' ') cities = """Berlin London Paris Dublin """.splitlines() def get_cities(): for c in cities: if c: yield c def worker(peer): seeker = context.socket(zmq.DEALER)
import argparse import zmq context = zmq.Context() socket = context.socket(zmq.DEALER) parser = argparse.ArgumentParser() parser.add_argument('-c', '--connect-address', default='tcp://127.0.0.1:5555') parser.add_argument('-p', '--port', default='5556') args = parser.parse_args() def get_ip(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('www.google.com', 80)) ip = s.getsockname()[0] s.close() return ip myip = get_ip() socket.connect(args.connect_address) # First just register to the server command = '{}:{}'.format(myip, args.port) socket.send_string(command) print socket.recv_multipart()
def main(screen, level=None, ai=False): pygame.init() # start added fullscreen functionality size=(640,480) screen=pygame.display.set_mode(size, DOUBLEBUF) flags=screen.get_flags() # end added fullscreen functionality pygame.mouse.set_visible(0) if level == None: level = 1 ballimg = pygame.Surface((10,10)) ballimg.fill((255,255,255)) paddleimg = pygame.Surface((50,10)) paddleimg.fill((255,255,255)) blockimg = pygame.Surface((20,10)) blockimg.fill((255,255,255)) startballdelay = 30; count = 0; balloff = False; deaths = 0 font = pygame.font.Font('freesansbold.ttf', 18) points = font.render('Points: 0', True, (255,255,255)) pointsrect = points.get_rect() pointsrect.x = 630 - pointsrect.width pointsrect.y = 10 if type(level) == int: blocks = load_level('./data/levels/lvl'+str(level)+'.lvl'); level += 1 else: blocks = load_level(level) ball = Ball(ballimg, 320,430, 0, 0, (640,480), blocks) paddle = Paddle(paddleimg, 300,450, ball, (640,480)) clock = pygame.time.Clock() ball.update() paddle.update() blocks.update() screen.blit(font.render('Deaths: '+str(deaths), True, (255,255,255)), (10, 450)) screen.blit(points, pointsrect) screen.blit(ball.image, ball.rect) screen.blit(paddle.image, paddle.rect) blocks.draw(screen) pygame.display.flip() while 1: clock.tick(60) # start added fullscreen functionality #events in your pygame app event1 = pygame.event.poll() if event1.type == KEYDOWN: if event1.key == K_ESCAPE: break elif event1.key ==K_f: #toggle fullscreen by pressing F key. if flags&FULLSCREEN==False: flags|=FULLSCREEN pygame.display.set_mode(size, flags) else: flags^=FULLSCREEN pygame.display.set_mode(size, flags) # end added fullscreen functionality if not pygame.display.get_active(): ball.paused = True paddle.paused = True else: if not ball.paused: ball.paused = False if not paddle.paused: paddle.paused = False for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYDOWN: if event.key == K_ESCAPE: return if event.key == K_RETURN: ball.pause() paddle.pause() if not balloff: if count < startballdelay: count += 1 ball.x = paddle.rect.centerx-ball.rect.width/2 ball.y = 430 else: count = 0 ball.dx = random.randint(-7,7) ball.dy = random.randint(-7,7) if ball.dy == 0: ball.dy = 1 balloff = True if not ball.paused: ball.image.fill((random.randint(0,255), random.randint(0,255), random.randint(0,255))) if ball.belowscreen: ball.x = paddle.rect.centerx-ball.rect.width/2 ball.y = 430 ball.dx = 0 ball.dy = 0 balloff = False ball.belowscreen = False deaths += 1 if len(blocks) == 0: if type(level) == int: level += 1 if './data/levels/lvl'+str(level)+'.lvl' in os.listdir('./data/levels/'): blocks = load_level('./data/levels/lvl'+str(level)+'.lvl') else: lastscreeen(screen, ball.points, deaths) return ball.blocks = blocks ball.dx = 0 ball.dy = 0 balloff = False ball.belowscreen = False else: lastscreen(screen, ball.points, deaths) return if not ai: ## MAIN CONTROL INPUT HERE # Pupil labs message retrieval try: topic,msg = socket.recv_multipart() msg = json.loads(msg) print msg if len(msg) == 1: msg_dict=msg[0] if pos_name in msg_dict: try: # extract screen coordinate and transform to game coordinates screen_coord = msg_dict.get(pos_name) screen_x = screen_coord[0] paddle.x = float(screen_x)*640.0-25.0 except KeyError: pass except KeyError: pass else: paddle.x = ball.x-ball.rect.width/2-paddle.rect.width/2 points = font.render('Points: '+str(ball.points), True, (255,255,255)) pointsrect = points.get_rect() pointsrect.x = 630 - pointsrect.width pointsrect.y = 450 paddle.update() ball.update() blocks.update() screen.fill((0,0,0)) screen.blit(font.render('Deaths: '+str(deaths), True, (255,255,255)), (10, 450)) screen.blit(points, pointsrect) screen.blit(paddle.image, paddle.rect) screen.blit(ball.image, ball.rect) blocks.draw(screen) pygame.display.flip()
print ti + "] Handling new message " #print msg if len(msg) == 4: msgfrom = msg[0] msgtxt = msg[1] msgid = msg[2] reply_wanted = msg[3] print "\tid:" + msgid + " reply_wanted:" + reply_wanted if reply_wanted == "1": print "\tthey want a reply. sending it." socket.send_multipart([msgfrom, 'fooreply', msgid]) routerport = 8123 myname = "ZMQROUTERDEMO" context = zmq.Context() print "Create ROUTER socket on " + str(routerport) socket = context.socket(zmq.ROUTER) socket.bind("tcp://*:" + str(routerport)) socket.setsockopt(zmq.IDENTITY, myname) print "main] loop running" while True: m = socket.recv_multipart() thread = threading.Thread(target=handler, args=(socket,m,)) thread.start()
#!/usr/bin/python import sys import zmq import time import socket print("Server Started") context = zmq.Context() socket = context.socket(zmq.ROUTER) socket.setsockopt(zmq.SNDHWM, 10000) socket.bind("tcp://*:5556") while True: # socket.send_string("%i %i %i" % (11102, *mouse.position)) string = socket.recv_multipart() print(string) res = socket.send_string("GOT IT !") print(res) # rc = zmq_connect(socket, "pgm://192.168.1.1;239.192.1.1:5555"); assert (rc == 0);