def run(self): self.uri, self.ns_daemon, self.bc_server = naming.startNS( self.hostname) self.started.set() if self.bc_server: self.bc_server.runInThread() self.ns_daemon.requestLoop()
def run(self): # start the nameserver, it configures a broadcast server for us self.uri, self.ns_daemon, self.bc_server = naming.startNS( host=self.hostname) # start this broadcast server in its own thread self.bc_server.runInThread() # signal the main thread that setup is done self.started.set() # serve away... self.ns_daemon.requestLoop()
def __init__(self, group=None, target=None, name=None, **kwargs): super().__init__(group=group, target=target, name=name) self.ns_uri, self.ns_daemon, self.bc_server_daemon = naming.startNS(host=kwargs['host']) self.daemon = Pyro4.Daemon(host=kwargs['host']) self.data_manager = managers.DataManager(sys.path[0]) self.uri_dm = self.daemon.register(self.data_manager) self.ns_daemon.nameserver.register('data_manager', self.uri_dm) chord_hash = int(hashlib.sha1(bytes('{host}:{port}'.format_map({'host': kwargs['host'], 'port': str(self.uri_dm).split('@')[1].split(':')[1]}), 'utf-8')).hexdigest(), 16) self.chord_widgets = Chord(kwargs['host'], int(str(self.uri_dm).split('@')[1].split(':')[1]), chord_hash, 160, widget=True) self.chord_widgets.data_manager_uri = str(self.uri_dm) self.chord_widgets.uri = 'PYRO:{name}@{ip}:{port}'.format_map({'name': 'chord_widgets', 'ip': kwargs['host'], 'port': str(self.uri_dm).split('@')[1].split(':')[1]}) self.chord_pages = Chord(kwargs['host'], int(str(self.uri_dm).split('@')[1].split(':')[1]), chord_hash, 160, widget=False) self.chord_pages.data_manager_uri = str(self.uri_dm) self.chord_pages.uri = 'PYRO:{name}@{ip}:{port}'.format_map({'name': 'chord_pages', 'ip': kwargs['host'], 'port': str(self.uri_dm).split('@')[1].split(':')[1]}) daemon_thread = threading.Thread(target=self.daemon_instance, args=(self.daemon,), name='demonio') ns_thread = threading.Thread(target=self.name_server_daemon, args=(self.ns_daemon,), name='demonio del name server') bc_thread = threading.Thread(target=self.broadcast_server_daemon, args=(self.bc_server_daemon,), name='demonio del broadcast') daemon_thread.start() ns_thread.start() bc_thread.start()
def main(): uris_list = [] ns_uri, ns_daemon, bc_server = naming.startNS( socketutil.getIpAddress('localhost', workaround127=True)) daemon = Pyro4.Daemon( socketutil.getIpAddress('localhost', workaround127=True)) algo1 = Tester() algo2 = Tester() for item in [algo1]: uri = daemon.register(item, 'algo') uris_list.append(uri) # se dejan las uris en una lista daemon_thread = threading.Thread(target=daemon_instance, args=(daemon, ), name='demonio') daemon_thread.start() print(ns_uri) print(uris_list) for uri in uris_list: print(uri) ns_daemon.nameserver.register('tester', uri) ns_thread = threading.Thread(target=name_server_daemon, args=(ns_daemon, ), name='demonio del name server') bc_thread = threading.Thread(target=broadcast_server_daemon, args=(bc_server, ), name='demonio del broadcast') ns_thread.start() bc_thread.start()
def share_local_object(object_name, whitelist=None, host="localhost", port=9090): """ Share a local object of the given name over the network. :param str object_name: name of the local object :param whitelist: shared functions/classes of the local object as tuple pairs (module, function) or (module, class); whitelist all (do not filter) if None or empty :type whitelist: [(str,str)] or None :param str host: ip address of the local name server :param port: port of the local sharing server :type port: int or str This function shares a custom object with whitelisted attributes through a custom implementation. It is more secure but more limited as functionality since it requires serialization extensions. """ Pyro4.config.AUTOPROXY = True Pyro4.config.REQUIRE_EXPOSE = False port = int(port) if isinstance(port, str) else port # pyro daemon try: pyro_daemon = Pyro4.Daemon(host=host) LOG.debug("Pyro4 daemon started successfully") pyrod_running = False # address already in use OS error except OSError: pyro_daemon = Pyro4.Proxy("PYRO:" + Pyro4.constants.DAEMON_NAME + "@" + host) pyro_daemon.ping() LOG.debug("Pyro4 daemon already started, available objects: %s", pyro_daemon.registered()) pyrod_running = True # name server try: ns_server = Pyro4.locateNS(host=host, port=port) LOG.debug("Pyro4 name server already started") nsd_running = True # network unreachable and failed to locate the nameserver error except (OSError, Pyro4.errors.NamingError): from Pyro4 import naming ns_uri, ns_daemon, _bc_server = naming.startNS(host=host, port=port) ns_server = Pyro4.Proxy(ns_uri) LOG.debug("Pyro4 name server started successfully with URI %s", ns_uri) nsd_running = False # main retrieval of the local object module = importlib.import_module(object_name) def proxymethod(fun): """Decorator to autoproxy all callables.""" def wrapper(*args, **kwargs): rarg = fun(*args, **kwargs) def proxify_type(rarg): if rarg is None or type(rarg) in (bool, int, float, str): # pylint: disable=C0123 return rarg if isinstance(rarg, tuple): return tuple(proxify_type(e) for e in rarg) if isinstance(rarg, list): return [proxify_type(e) for e in rarg] if isinstance(rarg, dict): return { proxify_type(k): proxify_type(v) for (k, v) in rarg.items() } pyro_daemon.register(rarg) return rarg import types if isinstance(rarg, types.GeneratorType): def generator_wrapper(): for nxt in rarg: yield proxify_type(nxt) return generator_wrapper() return proxify_type(rarg) return wrapper class ModuleObject: # pylint: disable=R0903 """Module wrapped for transferability.""" for fname, fobj in inspect.getmembers(module, inspect.isfunction): if not whitelist or (object_name, fname) in whitelist: setattr(ModuleObject, fname, staticmethod(proxymethod(fobj))) for cname, cobj in inspect.getmembers(module, inspect.isclass): if not whitelist or (object_name, cname) in whitelist: setattr(ModuleObject, cname, staticmethod(proxymethod(cobj))) local_object = ModuleObject() # we should register to the pyro daemon before entering its loop uri = pyro_daemon.register(local_object) if not pyrod_running: loop = DaemonLoop(pyro_daemon) loop.start() if not nsd_running: loop = DaemonLoop(ns_daemon) loop.start() # we should register to the name server after entering its loop ns_server.register(object_name, uri) LOG.info("Local object '%s' sharing - ready", object_name) loop.join()
def run(self): self.uri, self.ns_daemon, self.bc_server = naming.startNS(self.hostname) self.started.set() if self.bc_server: self.bc_server.runInThread() self.ns_daemon.requestLoop()