def _respond(self, req): """ Reponse """ # Note: In production code this needs to be Async since tx over # the wire round trip time >= over the ram rtt. Alternatively it # should aknowledge the inproc message and then manage the remote # connection without time contrains # return (self.upload("Nodemodule: %s" % req).replace("Nodemodule", # store all incoming messages to the queue # in order to proccess in the node_main self.rx_q.put(req, block=False, timeout=self._rx_timeout) # Detect and handle registration message if req.msg_type == NodeMessenger.REG: return self.node_register(req) log.info("Forwarding message") print(req) # Wrap it around an uplink message uplink_msg = self.remote.messenger.preamble_msg([req]) # This is the part that uplink response message is stripped out # I am taking one wrong assumptions here, for demo purposes # That the message contains one peripheral peripheral[0] # TODO make it real code return (self.upload(uplink_msg).peripheral[0])
def _respond(self, req): """ Method defines how the data should be proccessed and return a response to caller. Should be overridden by user """ # Note: Returning None will cancell server response but can block # Socket based on zmq configuration log.info("Server Received message") print(req) # Detect and handle registration message if req.msg_type == self.ul_messenger.REG: return self.network_register(req) return req
def node_main(self): """ User implemented main loop for node """ test_msg_pl = self.messenger.new_service("Hello %d" % self.counter) test_msg = self.messenger.solicited_msg(test_msg_pl) self.send_msg(test_msg) log.info("Nodemodule: Sending") print(test_msg) gevent.sleep(0.3) if self.has_msg(): log.info("Nodemodule: Received") rep = self.get_msg(blk=True) print("%s" % rep) self.counter += 1
def _respond(self, req): """ Method defines how the data should be proccessed and return a response to caller. Should be overridden by user """ log.info("Server Received message") print(req) # Detect and handle registration message if req.metadata.message_type == self.messenger.REG: return self.network_register(req) # I am taking two wrong assumptions here, for demo purposes # That the message contains one peripheral peripheral[0] # And that it contains one service payload[0] # TODO make it real code msg = req.peripheral[0].payload[0].msg req.peripheral[0].payload[0].msg = "%s to you too" % msg return req
def network_register(self): try: reg_msg = self.ul_messenger.register_msg() # Attempt to register and get the response reginfo = self.upload(reg_msg) # If server accepted registration continue if reginfo.metadata.message_type == self.ul_messenger.ACK: node_id = int([n for n in reginfo.control.params][0]) self.messenger.set_id(node_id) log.info("Registration Accepted, new id %d" % node_id) return else: raise Exception() except Exception as e: log.error("Failed to register to network %s" % e) sys.exit(1) print(reg_msg)
def node_register(self): """ User accessible method for handling registration message """ try: # Prepare and send the registration message init_msg = self.messenger.register_msg() self._send(msg=init_msg) # Accept the new module_id from server reginfo = self._recv() # If server accepted registration continue if reginfo.msg_type == NodeMessenger.ACK: node_id = [n for n in reginfo.payload][0].msg node_id = int(node_id) self.messenger.set_id(node_id) log.info("Registration Accepted, new id %d" % node_id) return else: raise Exception() except Exception as e: log.error("Failed to register module %s" % e) sys.exit(1)
def info(self, *args): """ Print a debug message and notify the user with the LED.""" CLogger.info(args[-1])