def get_conectors(obj): request = obj._etc["_REQUEST_"] proxys = obj._etc["_PROXYS"] attemps = ATTEMPS while attemps > 0: available_interfaces = obj.dsc_get(obj._etc["robot"] + "/*/InterfacesOK") PROXYS = { a: available_interfaces[prox] for a, prox in proxys.items() if prox in available_interfaces } if len(PROXYS) == len(proxys): attemps = 0 else: attemps = attemps - 1 if attemps % 5 == 0: obj.L_print("Component waiting for PROXYS {}".format(proxys), True) time.sleep(0.3) PROXYS = {attr: Proxy(uri) for attr, uri in PROXYS.items() if Proxy(uri)} proxys = {attr: prox for attr, prox in proxys.items() if attr in PROXYS} for attr, Prox in PROXYS.items(): setattr(obj, attr, Prox) obj._etc["_PROXYS"] = proxys errors = [x for x in request if x not in PROXYS] if len(errors) > 0: obj.L_error("_REQUEST_ {} not satified".format(errors)) return False if len(proxys) != len(PROXYS): obj.L_warning("{} have not conneted".format( [x for x in proxys if x not in PROXYS])) obj._PROC["status"] = "OK"
def cleaner(self): while self.worker_run: delete = [] for comp in self.Comps: ser = self.Control_Service(comp) control = Proxy(ser) if not control(): delete.append(comp) for c in delete: del (self.Comps[c]) self.L_info("{} disconnected".format(c)) Robots = list(self.Robots) for R in Robots: if len(self.Components(R)) == 0: self.Unregister_Robot(R) self.L_info("{} Unregistered ".format(R)) Nodes = list(self.Nodes) delete = [] for N in Nodes: ser = self.Nodes[N][1][1] control = Proxy(ser) if not control(): delete.append(N) for c in delete: del (self.Nodes[N]) self.L_info("{} Node disconnected".format(c)) time.sleep(2)
def status(self): component="{}/{}".format(self.robot_dir,self.component) uri_comp = self.BB.Control_Service(component) proxy=Proxy(uri_comp) if proxy(): proxy.show_PROC() else: P_Log("[FR][ERROR][FY] Connecting to Component {} seems Stopped".format(component))
def Status_robot(search): dsc = Discovery() controls = {} for s in search: controls.update(dsc.Get(s + "/Control")) for uri in controls.values(): if uri != "0.0.0.0:0": proxy = Proxy(uri) if proxy(): info_comp = proxy.Get_INFO() show_PROC(info_comp) if len(controls) == 0: P_Log("[FY] Nothing to show")
def Stop_robot(search): dsc = Discovery() controls = {} for s in search: controls.update(dsc.Get(s + "/Control")) for uri in controls.values(): if uri != "0.0.0.0:0": proxy = Proxy(uri) if proxy(): name = proxy.shutdown() P_Log("[FR][STOP][FY] Signal Stop in Component [FW]{}".format( name)) if len(controls) == 0: P_Log("[FY] Nothing to Stop")
def Get_BigBrother_proxy(broadcast_port=9999): #P_Log("[FY] Finding BigBrother on ethernet segment") uri_BB=BB.Get_BigBrother(broadcast_port) if uri_BB=="0.0.0.0:0": #P_Log("[FR] [warning][FW] BigBrother not found") return "0.0.0.0:0",None else: proxy=Proxy(uri_BB) if proxy(): return uri_BB,proxy return uri_BB,None
def _conectors(obj, reqs): #importante : dentro de los threads no podemos acceder a proxys externos # gevent no funciona bien en el intercambio con thread # solucion crear el proxy dentro.] items = list(reqs) BB_name = Proxy(obj._etc["BB_uri"]) BB_name.connect() while obj._PROC["running"] != "RUNNING": retrys = RETRYS while items != [] and retrys > 0: item = items.pop() uri = BB_name.Get_Interface_Uri(reqs[item]) if uri == "0.0.0.0:0": items.append(item) time.sleep(0.3) else: obj.L_info("{} connect to {}".format(item, uri)) setattr(obj, item, uri) retrys = retrys - 1 if items == []: obj._PROC["running"] = "RUNNING" else: obj.L_info("Error connecting {}".format(items))
def connect_component(obj): obj._PROC["running"] = "stop" obj.worker_run = obj._etc.get("def_worker", True) obj._PROC["BB_proxy"] = Proxy(obj._etc["BB_uri"]) #loading topics and events name = obj._etc["name"] robot = obj._etc["robot"] uri = obj._etc["MQTT_uri"] sync = obj._etc["public_sync"] frec = obj._etc["frec"] #loading publications if len(obj._etc["_PUB"]) + len(obj._etc["_PUB_EVENTS"]) > 0: obj._PROC["PUB"] = Publication(name, uri, obj, sync, frec) obj._PROC["PUB"].add_topics(*obj._etc["_PUB"]) for ent, events in obj._etc["_PUB_EVENTS"].items(): for e in events: obj._PROC["PUB"].add_event(ent, e) if sync: obj._PROC["PUB"].start() #conectar subcriptores name = obj._etc["name"] uri = obj._etc["MQTT_uri"] if len(obj._etc["_SUB"]) + len(obj._etc["_SUB_EVENTS"]) > 0: obj._PROC["SUB"] = subscriptions(name, uri, obj) topics = { x: k.replace("LOCAL/", robot + "/") for x, k in obj._etc["_SUB"].items() } obj._PROC["SUB"].subscribe_topics(**topics) events = { x: k.replace("LOCAL/", robot + "/") for x, k in obj._etc["_SUB_EVENTS"].items() } obj._PROC["SUB"].subscribe_events(**events) obj._PROC["SUB"].connect() #lanzar proxys remotes y locales _REQ_INTERFACES HECHO req = { x: k.replace("LOCAL/", robot + "/") for x, k in obj._etc["_REQ"].items() } obj._PROC["running"] = "WAITING REQUIRES" name = obj._etc["name"] + ".conectors" t = threading.Thread(target=_conectors, args=( obj, req, ), name=name) t.setDaemon(True) t.start()
def __init__(self, robot, broadcast=broadcast_port): self._etc = {} self._PROC = {} self._PROC["event_handlers"] = {} super().__init__() self._etc["name"] = robot self.robot = robot self._etc["BB_uri"] = BB.Get_BigBrother(port=broadcast) self._etc["BB"] = Proxy(self._etc["BB_uri"]) if self._etc["BB"](): self._etc["BROKER"] = self._etc["BB"].Broker(self.robot) self._etc["TOPICS"] = self._etc["BB"].Topics(robot) self._etc["SERVICES"] = self._etc["BB"].Services(robot) self._etc["EVENTS"] = self._etc["BB"].Events(robot) self._PROC["SUB"] = subscriptions(robot + "/client", self._etc["BROKER"], self) else: self.L_error("Conneting BIGBROTHER on {}".format(self._etc["BB"])) exit()
def SERVICES(self, **services): robot = self._etc["name"] self._etc["SERVICES"] = {} available = self._etc["BB"].Services(self._etc["name"]) for item, service in services.items(): service = "{}/{}".format(self._etc["name"], service) if service in available: uri = self._etc["BB"].Get_Interface_Uri(service) if uri != "0.0.0.0:0": proxy = Proxy(uri) if proxy(): setattr(self, item, proxy) self.L_info("{} Connected".format(item, uri)) else: self.L_error("Conneting {} on {}".format(item, uri)) exit() else: self.L_error("{} Not Found".format(service)) exit() else: self.L_error("{} Not available".format(service)) exit()
def Start_robot(Filename=None, Init={}, Model={}): loader = Loader_PYRobot(Filename, Init, Model) robot_name = Init["NAME"] onlinecomps = Find_robot("{}/*".format(robot_name), show=False) loader.Check() components = loader.Get_Skel() dsc = Discovery() for c, comp in components.items(): name = comp["_etc"]["name"] host = comp["_etc"]["host"] if name not in onlinecomps: comp = json.dumps(comp) if host == "localhost": utils.run_component("_comp", comp, run="start") else: uris = get_node_interfaces(host) if host in uris: proxy = Proxy(uris[host]) if proxy(): proxy.Run_comp(comp) P_Log( "[FY]component:[FW]{} [FY]starting in host:[FW]{}".format( name, host)) else: P_Log("[FY]component:[FW]{} [FY] is online on [FW]{}".format( name, host)) trys = 6 while trys > 0: controls = dsc.Get(robot_name + "/*/ControlOK") if len(controls) == len(components): trys = 0 else: trys = trys - 1 time.sleep(0.09) if len(controls) != len(components): P_Log("[FY][Warning][FW] some components are not running") P_Log("[FY] Waiting for connect loggins...") controls = dsc.Get(robot_name + "/*/ControlOK") for uri in controls.values(): if uri != "0.0.0.0:0": proxy = Proxy(uri) if proxy(): proxy.Set_Logging(0)
#print(st.component) if st.uri_BB != "0.0.0.0:0": st.Get_BB_Robot() st.Get_MQTT() st.Get_BB_Comp("{}/{}".format(robot, comp)) st.start() else: P_Log("[FR][ERROR][FY]BigBrother Not found") else: uri = get_node(node) if uri == "0.0.0.0:0": P_Log("[FR][ERROR][FY] Node {} not is online".format(node)) exit() else: P_Log("[FY] Running in node {}:{}".format(node, uri)) proxy = Proxy(uri) print(proxy) gen = Get_General(robot) print(gen) ins = Get_Instance(robot, comp) print(ins) if sys.argv[2] == "stop": st = Comp_Starter(robot, comp) if st.uri_BB != "0.0.0.0:0": st.stop() if sys.argv[2] == "status": st = Comp_Starter(robot, comp) if st.uri_BB != "0.0.0.0:0": st.status() if sys.argv[2] == "kill":