def add_resolved_remote_dep(self, dep): # print(colored("New remote dep! {}, type: {}".format(dep, type(dep)), "green")) if isinstance(dep, dict): k = dep.keys()[0] proxy_lst = [] try: if isinstance(dep[k], list): for u in dep[k]: if u not in self._resolved_remote_deps: (name, _, _) = utils.uri_split(u) proxy_lst.append( utils.get_pyro4proxy(u, name.split(".")[0])) self._resolved_remote_deps.append(u) if k in self.deps and isinstance(self.deps[k], list): self.deps[k].extend(proxy_lst) else: self.deps[k] = proxy_lst else: if dep[k] not in self._resolved_remote_deps: (name, _, _) = utils.uri_split(dep[k]) self.deps[k] = utils.get_pyro4proxy( dep[k], name.split(".")[0]) self._resolved_remote_deps.append(dep[k]) if (self._unr_remote_deps is not None): if k in self._unr_remote_deps: self._unr_remote_deps.remove(k) except Exception: template = "[control.add_resolved_remote_dep()] An exception of type {0} occurred. Arguments:\n{1!r}" print template.format(type(ex).__name__, ex.args) self.check_remote_deps() else: print("Error in control.add_resolved_remote_dep(): No dict", dep)
def _proxy_robot(self): proxys = None if not self.ns: try: uri = "PYRO:" + self.name + "@" + \ self.ip + ":" + str(self.port_robot) self.node = utils.get_pyro4proxy(uri, self.name) except Exception: print("ERROR: invalid URI: %d" % uri) exit() else: # NameServer o BigBrother for x in utils.get_all_ip_address(broadcast=True): # print "Locating on :", x try: # print "CONFIG", Pyro4.config.asDict() Pyro4.config.BROADCAST_ADDRS = x ns = Pyro4.locateNS() except Exception: ns = None if not ns: print("ERROR: Unable to locate a name server.") exit() # BigBrother try: bb_uri = ns.lookup("bigbrother") bb = utils.get_pyro4proxy(bb_uri, self.bigbrother_passw) robot_uri = bb.lookup(self.name) print robot_uri except Pyro4.errors.NamingError: # Busco en NS de robot anonimo robot_uri = ns.lookup(self.name) try: self.node = utils.get_pyro4proxy(robot_uri, self.name) except Exception: self.node = None if (self.node): try: proxys = self.node.get_uris() # print proxys except Exception: print( "ERROR: Unable to obtain list of robot components: \ \n-->[URI]: %s \n-->[NAME]: %s" % (robot_uri, self.name)) raise os._exit(0) else: print("Robot not found.") os._exit(0) return proxys
def __init__(self, name, port_robot=4041, bigbrother_passw=None): """ClientRobot initialization method. Args: name (str): Name or URI of the robot to be returned. port_robot (int): (Optional) Port where the robot works. bigbrother_passw (str): Bigbrother's password. """ Pyro4.config.SERIALIZERS_ACCEPTED = [ "json", "marshal", "serpent", "pickle" ] if "@" in name: self.name = name[0:name.find("@")] self.ip = name[name.find("@") + 1:] self.ns = False else: self.name = name self.ip = "" self.ns = True self.port_robot = port_robot self.bigbrother_passw = bigbrother_passw if ( bigbrother_passw) else DEFAULT_BB_PASSWORD try: proxys = self._proxy_robot() for p in proxys: con = p.split("@")[0].split(".")[1] proxy = utils.get_pyro4proxy(p, self.name) setattr(self, con, proxy) except Exception: print("ERROR: conection") raise exit()
def proxy(self, obj, passw=None): all_proxys = [] for x in self.lookup(obj): if passw is None: passw = obj.split(".")[0] all_proxys.append(utils.get_pyro4proxy(x, passw)) return all_proxys
def request_loop(self, key): try: uris = [] trys = 10 r_obj = key.split("#")[0] # Obj needed claimant = self.async_waitings[key][ "claimant"] # Robot.Comp needs c_name, c_comp = claimant.split(".") # Robot and comp needs init_time = time.time() while not uris or self.async_waitings[key]["target_type"] == 3: interval = (time.time() - init_time) if interval > 30: # Check exists if self.async_waitings[key]["claimant"].split( ".")[0] in self.robots.keys(): init_time = time.time() else: break uris, tt = self.lookup(r_obj, target_type=True, returnAsList=True) self.async_waitings[key]["target_type"] = tt if DEBUGGER: print("[time:{}]".format(interval), r_obj, uris, tt, ("keys[{}]".format(len( self.robots)), list(self.robots.keys()))) time.sleep(5) if uris: if DEBUGGER: print( colored( "\nURI Obtained: {} for: {}".format(uris, key), "green")) print(c_name, c_comp) try: for robots in self.components.get(c_comp): (name, _, _) = utils.uri_split(robots) if DEBUGGER: print( colored( "Robot: {} and Looking for: {}".format( robots, name), "yellow")) if name == self.async_waitings[key]["claimant"]: if (tt == 2 or tt == 4) and (isinstance( uris, list)): uris = uris[0] while trys > 0: try: p = utils.get_pyro4proxy( robots, name.split(".")[0]) p.add_resolved_remote_dep( {r_obj: uris}) if DEBUGGER: print( colored( "\nURI SENDED: {}".format( key), "green")) break except Exception: if DEBUGGER: print( colored( "\nURI ERROR: {}".format( key), "red")) trys -= 1 time.sleep(3) raise except Exception: if DEBUGGER: print( colored( "\nImposible realizar callback a {}". format(claimant), 'red')) else: pass if claimant in self.claimant_list: self.claimant_list.remove(claimant) self.async_waitings.pop(key, None) # Remove if exists except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" print(template.format(type(ex).__name__, ex.args)) time.sleep(2)