def start_node(robot, proc_pipe, msg): """Start node.""" try: # Set process name setproctitle.setproctitle("PYRO4BOT." + robot["node"]["name"] + "." + "ROBOT") # Daemon proxy for node robot robot["node"]["port_node"] = utils.get_free_port( robot["node"]["port_node"]) daemon = Pyro4.Daemon(host=robot["node"]["ip"], port=robot["node"]["port_node"]) daemon._pyroHmacKey = robot["node"]["password"].encode() pyro4bot_class = control.Pyro4bot_Loader(globals()["Robot"], **robot) new_object = pyro4bot_class() # Associate object to the daemon uri_node = daemon.register(new_object, objectId=robot["node"]["name"]) # Get and save exposed methods exposed = Pyro4.core.DaemonObject(daemon).get_metadata( robot["node"]["name"], True) # Hide methods from Control safe_exposed = {} for k in exposed.keys(): safe_exposed[k] = list(set(exposed[k]) - set(dir(control.Control))) safe_exposed["methods"].extend(["__docstring__", "__exposed__"]) new_object.exposed = safe_exposed new_object.mypid = os.getpid() new_object.uri_node = uri_node # Get docstring from exposed methods on node new_object.docstring = new_object.get_docstring(new_object, exposed) # Printing info print( colored( "____________STARTING PYRO4BOT NODE %s_______________________" % robot["node"]["name"], "yellow")) print("[%s] PYRO4BOT: %s" % (colored("OK", 'green'), uri_node)) new_object.start_components() msg.put((uri_node, os.getpid())) proc_pipe.send("OK") new_object.register_node() daemon.requestLoop() print("[%s] Final shutting %s" % (colored("Down", 'green'), uri_node)) os._exit(0) except Exception: print("ERROR: start_node in robotstarter.py") proc_pipe.send("ERROR") raise
def get_ns(self): if self.nameserver is None: default_ns = Pyro4.config.BROADCAST_ADDRS # Looking for Network NameServer for x in utils.get_all_ip_address(broadcast=True): if (self.nameserver): break try: Pyro4.config.BROADCAST_ADDRS = x self.broadcast_ns = x self.nameserver = Pyro4.locateNS() self.nameserver.ping() printInfo("NameServer located.") except Exception: self.nameserver = None if (self.nameserver): # ¿BigBrother or Random NS? try: ns_uri = self.nameserver.lookup("bigbrother") possible_ns = utils.get_pyro4proxy(ns_uri, ROUTER_PASSWORD) if (possible_ns.ready()): self.ns_uri = ns_uri self.nameserver = possible_ns printInfo("[NS_Type] BigBrother ----> %s" % self.ns_uri) self.usingBB = True except Pyro4.errors.NamingError: printInfo("[NS_Type] Generic NameServer ----> %s" % self.nameserver.lookup("Pyro.NameServer")) if self.nameserver is None: printInfo("NameServer not found.", "red") # Creating a NameServer try: port = utils.get_free_port(self.port_ns, ip=self.ip) Pyro4.config.SERIALIZERS_ACCEPTED = ["json", "marshal", "serpent", "pickle"] self.nsThread = threading.Thread( target=nm.startNSloop, kwargs={'host': self.ip, 'port': port}) self.nsThread.start() time.sleep(1) printInfo("NameServer created.") except Exception: printInfo("Error creating NameServer", "red") self.nameserver = None # Locate NS attempts = 0 while attempts < 10: try: Pyro4.config.BROADCAST_ADDRS = default_ns self.nameserver = Pyro4.locateNS() self.nameserver.ping() break except Exception: attempts += 1 time.sleep(0.3) return self.nameserver if self.nameserver else None
def new_uri(self, name, mode="public"): if mode == "local": ip = "127.0.0.1" else: ip = self.ip port_node = utils.get_free_port(self.port, interval=10, ip=ip) start_port = utils.get_free_port(self.start_port, ip=ip) if name.find(self.botName) > -1: if name != self.botName: uri = "PYRO:" + name + "@" + ip + ":" + str(start_port) else: uri = "PYRO:" + name + "@" + ip + ":" + str(port_node) else: uri = "PYRO:" + name + "@" + self.ip + ":" + str(start_port) self.URIS[name] = uri self.start_port = start_port + 1 self.port_node = port_node + 1 return uri
def start_pyro4bot_object(self, d, proc_pipe): """Start PYRO4BOT component.""" (name_ob, ip, ports) = utils.uri_split(d["pyro4id"]) try: # Daemon proxy for sensor daemon = Pyro4.Daemon(host=ip, port=utils.get_free_port(ports, ip=ip)) daemon._pyroHmacKey = self.node["password"].encode() proc_pipe.send("CONTINUE") deps = utils.prepare_proxys(d, self.node["password"]) # assinging tty out and err for object default_tty = utils.get_tty() utils.set_tty_err(d["tty_err"]) # Preparing class for pyro4 pyro4bot_class = control.Pyro4bot_Loader(globals()[d["cls"]], **deps) new_object = pyro4bot_class() # Associate object to the daemon uri = daemon.register(new_object, objectId=name_ob) # Get and save exposed methods exposed = Pyro4.core.DaemonObject(daemon).get_metadata( name_ob, True) # Hide methods from Control safe_exposed = {} for k in exposed.keys(): safe_exposed[k] = list( set(exposed[k]) - set(dir(control.Control))) safe_exposed["methods"].extend(["__docstring__", "__exposed__"]) new_object.exposed.update(safe_exposed) setproctitle.setproctitle("PYRO4BOT." + name_ob) # Save dosctring documentation inside sensor object new_object.docstring.update( self.get_docstring(new_object, safe_exposed)) daemon.requestLoop() # getting tty default to exit utils.set_tty_out(default_tty) utils.set_tty_err(default_tty) print("[%s] Shutting %s" % (colored("Down", 'green'), d["pyro4id"])) except Exception as e: proc_pipe.send("FAIL") print("ERROR: creating sensor robot object: " + d["pyro4id"]) print(utils.format_exception(e))
def create_uriresolver_proxy(self): try: Pyro4.config.HOST = "localhost" Pyro4.config.SERIALIZERS_ACCEPTED = ["json", "marshal", "serpent", "pickle"] self.port = utils.get_free_port(self.port) self.daemonproxy = Pyro4.Daemon( host="127.0.0.1", port=self.port) # Daemon proxy for NODE self.daemonproxy._pyroHmacKey = bytes(self.password) self.daemonproxy.requestLoop() except Pyro4.errors.ConnectionClosedError: print("Error al conectar al proxy") except Exception: print("ERROR: creating _client_robot") finally: if (self.uri is not None): print("[%s] Shutting %s" % (colored("Down", 'green'), self.uri.asString())) os._exit(0)