def on_message(self, msg): """Processes given message. Decides what kind of message it is -- client or worker -- and calls the appropriate method. If unknown, the message is ignored. :param msg: message parts :type msg: list of str :rtype: None """ rp, msg = split_address(msg) try: # # Dispatch on first frame after path # t = msg.pop(0) if t.startswith(b'MDPW'): logging.debug('Recieved message from worker {}'.format(rp)) self.on_worker(t, rp, msg) elif t.startswith(b'MDPC'): logging.debug('Recieved message from client {}'.format(rp)) self.on_client(t, rp, msg) else: logging.error('Broker unknown Protocol: "{}"'.format(t)) except: logging.error( "An error occured while trying to process message: rp: {}, msg: {}\n{}".format( rp, msg, traceback.format_exc() ) )
def _on_message(self, msg): """Helper method called on message receive. msg is a list w/ the message parts """ # 1st part is empty msg.pop(0) # 2nd part is protocol version # TODO: version check proto = msg.pop(0) # 3rd part is message type msg_type = msg.pop(0) # XXX: hardcoded message types! # any message resets the liveness counter self.need_handshake = False self.curr_liveness = self.HB_LIVENESS if msg_type == b'\x05': # disconnect self.curr_liveness = 0 # reconnect will be triggered by hb timer elif msg_type == b'\x02': # request # remaining parts are the user message envelope, msg = split_address(msg) envelope.append(b'') envelope = [b'', self._proto_version, b'\x03'] + envelope # REPLY self.envelope = envelope self.on_request(msg) else: # invalid message # ignored pass return
def _on_message(self, msg): """Helper method called on message receive. msg is a list w/ the message parts """ # 1st part is empty msg.pop(0) # 2nd part is protocol version # TODO: version check proto = msg.pop(0) # 3nd part is message type msg_type = msg.pop(0) # XXX: hardcoded message types! # any message resets the liveness counter self.need_handshake = False self.curr_liveness = self.HB_LIVENESS if msg_type == '\x05': # disconnect print ' DISC' self.curr_liveness = 0 # reconnect will be triggered by hb timer elif msg_type == '\x02': # request # remaining parts are the user message envelope, msg = split_address(msg) envelope.append(b'') envelope = [ b'', self._proto_version, '\x03'] + envelope # REPLY self.envelope = envelope self.on_request(msg) else: # invalid message # ignored pass return
def on_reply(self, rp, msg): """Process worker REPLY command. Route the `msg` to the client given by the address(es) in front of `msg`. :param rp: return address stack :type rp: list of str :param msg: message parts :type msg: list of str :rtype: None """ ret_id = rp[0] wrep = self._workers[ret_id] service = wrep.service # make worker available again try: wq, wr = self._services[service] cp, msg = split_address(msg) self.client_response(cp, service, msg) wq.put(wrep.id) if wr: proto, rp, msg = wr.pop(0) self.on_client(proto, rp, msg) except KeyError: # unknown service self.disconnect(ret_id) return
def _on_message(self, *msg): """Helper method called on message receive. msg is a list w/ the message parts """ msg = list(msg) #logger.debug( "%s <- %s", self, msg ) msg.pop(0) # 1st part is empty proto = msg.pop(0) # 2nd part is protocol version msg_type = msg.pop(0) # 3rd part is message type assert proto == TxMDPWorker._mdp_ver # do something intelligent if msg_type == b'\x05': # disconnect self.curr_liveness = 0 # reconnect will be triggered by hb timer elif msg_type == b'\x04': # heartbeat self.curr_liveness = self._hb_liveness elif msg_type == b'\x02': # request # remaining parts are the user message envelope, msg = split_address(msg) envelope.append(b'') envelope = [ b'', self._mdp_ver, b'\x03'] + envelope # REPLY self.envelope = envelope logger.debug( "%s <- num frames %d", self, len(msg) ) self.on_request(self,msg) else: logger.warn( "%s <- unknown message: %s", self, msg )
def on_reply(self, rp, msg): """Process worker REPLY command. Route the `msg` to the client given by the address(es) in front of `msg`. :param rp: return address stack :type rp: list of str :param msg: message parts :type msg: list of str :rtype: None """ ret_id = rp[0] wrep = self._workers.get(ret_id) if not wrep: # worker not found, ignore message return service = wrep.service # make worker available again try: wq, wr = self._services[service] cp, msg = split_address(msg) self.client_response(cp, service, msg) wq.put(wrep.id) if wr: proto, rp, msg = wr.pop(0) self.on_client(proto, rp, msg) except KeyError: # unknown service self.disconnect(ret_id) return
def on_message(self, msg): """Processes given message. Decides what kind of message it is -- client or worker -- and calls the appropriate method. If unknown, the message is ignored. :param msg: message parts :type msg: list of str :rtype: None """ _LOG.debug("Received: %s" % msg) rp, msg = split_address(msg) # TODO: this condition should be changed to something better if len(msg) < 2: _LOG.debug("Unrecognized message.") return # dispatch on first frame after path t = msg.pop(0) if t.startswith(b'MNPW'): self.on_worker(t, rp, msg) elif t.startswith(b'MNPC'): self.on_client(t, rp, msg) else: _LOG.debug('Broker unknown Protocol: "%s"' % t) return
def on_message(self, msg): """Processes given message. Decides what kind of message it is -- client or worker -- and calls the appropriate method. If unknown, the message is ignored. :param msg: message parts :type msg: list of str :rtype: None """ #logger.debug( "INCOMING: %s", msg ) rp, msg = split_address(msg) # dispatch on first frame after path proto = msg.pop(0) if proto.startswith(self._mdp_worker_ver): self.on_worker(proto, rp, msg) elif proto.startswith(self._mdp_client_ver): self.on_client(proto, rp, msg) else: logger.debug( "bad msg: %s", msg ) logger.warn( "unknown protocol: %s", proto )
def on_reply(self, rp, msg): """Process worker REPLY command. Route the `msg` to the client given by the address(es) in front of `msg`. :param rp: return address stack :type rp: list of str :param msg: message parts :type msg: list of str :rtype: None """ ret_id = rp[0] wrep = self._workers.get(ret_id) if not wrep: # # worker not found, ignore message # logging.error( "Worker with return id {} not found. Ignore message.".format( ret_id)) return service = wrep.service logging.info("Worker {} sent reply.".format(service)) try: wq, wr = self._services[service] # # Send response to client # cp, msg = split_address(msg) self.client_response(cp, service, msg) # # make worker available again # wq.put(wrep.id) if wr: logging.info("Sending queued message to worker {}".format(service)) proto, rp, msg = wr.pop(0) self.on_client(proto, rp, msg) except KeyError: # # unknown service # self.disconnect(ret_id)
def on_message(self, msg): """Processes given message. Decides what kind of message it is -- client or worker -- and calls the appropriate method. If unknown, the message is ignored. :param msg: message parts :type msg: list of str :rtype: None """ rp, msg = split_address(msg) # dispatch on first frame after path t = msg.pop(0) if t.startswith(b'MDPW'): self.on_worker(t, rp, msg) elif t.startswith(b'MDPC'): self.on_client(t, rp, msg) else: print 'Broker unknown Protocol: "%s"' % t return
def on_reply(self, rp, msg): """Process worker REPLY command. Route the `msg` to the client given by the address(es) in front of `msg`. :param rp: return address stack :type rp: list of str :param msg: message parts :type msg: list of str :rtype: None """ ret_id = rp[0] wtracker = self._workers.get(ret_id) if not wtracker: # worker not found, ignore message return service = wtracker.service # make worker available again try: wq, wr = self._services[service] cp, msg = split_address(msg) if cp[0] == 'BROKER': self.update_worker_info(ret_id, msg) return cmd = msg.pop(0) self.client_response(cp, service, cmd, bytes_to_hexstring(wtracker.id), msg) wq.put(wtracker.id) self.change_worker_status(rp[0], WORKER_ONLINE_STATUS) if wr: proto, rp, msg = wr.pop(0) self.on_client(proto, rp, msg) except KeyError: # unknown service _LOG.info("Worker with id: '%s' reports an unknown service." % bytes_to_hexstring(ret_id)) self.disconnect(ret_id) return
def _on_message(self, msg): """Helper method called on message receive. :param msg: a list w/ the message parts :type msg: a list of byte-strings """ _LOG.debug("Received: %s." % msg) # 1st part is empty msg.pop(0) # 2nd part is protocol version proto = msg.pop(0) if proto != WORKER_PROTO: # ignore message from not supported protocol pass # 3rd part is message type msg_type = msg.pop(0) # XXX: hardcoded message types! # any message resets the retries counter self.need_handshake = False self.curr_retries = self.HB_RETRIES if msg_type == MSG_DISCONNECT: # disconnect _LOG.info("Broker wants us to disconnect.") self.curr_retries = 0 # reconnect will be triggered by hb timer elif msg_type == MSG_QUERY: # request # remaining parts are the user message _LOG.debug("Received new request: %s." % msg) envelope, msg = split_address(msg) envelope.append(b'') envelope = [b'', WORKER_PROTO, MSG_REPLY] + envelope # reply self.envelope = envelope self.on_request(msg) else: # invalid message # ignored _LOG.debug('ignoring message with invalid id') pass return
def on_reply(self, rp, msg): """Process worker REPLY command. Route the `msg` to the client given by the address(es) in front of `msg`. :param rp: return address stack :type rp: list of str :param msg: message parts :type msg: list of str :rtype: None """ ret_id = rp[0] try: wrep = self._get_worker(ret_id) except IndexError: logger.warn( "<- unknown worker(%s), ignoring message", ret_id ) return logger.debug( "<- %s: reply, num frames: %d", wrep, len(msg) ) service = wrep.service # make worker available again try: wqueue, req_queue = self._services[service] cp, msg = split_address(msg) self.client_response(cp, service, msg) wqueue.put(wrep.id) self.check_for_work( service ) except KeyError: logger.warn( "%s: unknown service: %s", wrep, service ) self.disconnect(ret_id)
def _on_message(self, msg): """Helper method called on message receive. msg is a list w/ the message parts """ logging.debug('Received message: {}'.format(msg)) # # 1st part is empty # msg.pop(0) # # 2nd part is protocol version # TODO: version check # proto = msg.pop(0) # # 3rd part is message type # msg_type = msg.pop(0) # # XXX: hardcoded message types! # any message resets the liveness counter # self.curr_liveness = self.HB_LIVENESS if msg_type == W_DISCONNECT: # # Disconnect. Reconnection will be triggered by hb timer # self.curr_liveness = 0 elif msg_type == W_READY: # # The message contains the unique id attached to the worker. # if len(msg) > 0: # # This above check is used for supporting older version of # the code. # self._unique_id = msg[0] elif msg_type == W_REQUEST: # # Request. Remaining parts are the user message # envelope, msg = split_address(msg) envelope.append(EMPTY_FRAME) envelope = [EMPTY_FRAME, self._proto_version, W_REPLY] + envelope self.envelope = envelope self.on_request(msg) else: # # invalid message # ignored # pass