def __new__(cls, *args, **kwds): actor = Actor(local_theatre()) type = cls.__name__ module_name = inspect.getmodule(cls).__name__ name = type + '-' if len(args) > 0: args_list = list(args) name = args_list.pop(0) args = tuple(args_list) suggested_loc = local_theatre().where_to_create() (actor_id, exists) = actor.theatre.globally_register_named_singleton(name, suggested_loc, (cls, module_name, type, args, kwds)) return Reference(actor_id)
def __new__(cls, *args, **kwds): type = cls.__name__ module_name = inspect.getmodule(cls).__name__ core_loc = local_theatre().where_to_create() if core_loc == local_theatre().gethostname(): actor_id = local_theatre().create_actor(cls, module_name, type, args, kwds) else: network_locator = rpc.RPCConnector(core_loc) core = network_locator.connect() actor_id = core.create_actor(cls, module_name, type, args, kwds) core.disconnect() return Reference(actor_id)
def __new__(cls, *args, **kwds): actor = Actor(local_theatre()) type = cls.__name__ (actor_id, exists) = actor.theatre.globally_register_global_singleton(actor, type) if not exists: actorstate = object.__new__(cls) actorstate.__init__(type, type) actorstate.singleton = True actorstate.add_birth(args, kwds) actor.setstate(actorstate) actor.start() return Reference(actor_id)
def birth(self, port): print local_theatre().gethostname() local_theatre().say_hello() opened = open_socket(port) print opened print opened.getsockname() #ClientActor(opened) new_socket = opened.accept() print new_socket rfile = new_socket.makefile('r') wfile = new_socket.makefile('w') print wfile while 1: print "Reading data..." data = rfile.readline() if not data or len(data.strip()) < 1: break wfile.write("Echo: %s" % data) wfile.flush() new_socket.close() opened.close() print "Done"
def __migrate_remote(self, actor, address): module = actor.getmodule() modulename = module.__name__ if modulename == '__main__': raise Exception('Cannot migrate actors defined in the main module') if ids.ip_from_loc(self.here) != ids.ip_from_loc(address): # Non-local migration src = inspect.getsource(module) local_theatre().store_script(module.__name__, src) conn = rpc.RPCConnector(address) remote_theatre = conn.connect() remote_theatre.get_script(modulename) remote_theatre.disconnect() conn = rpc.RPCConnector(address) actor.state.add_arrived() remote_theatre = conn.connect() remote_theatre.incoming_actor(actor.state) remote_theatre.disconnect()
def get_script(self, module_name): if sys.modules.has_key(module_name): return try: __import__(module_name) return except: self.__lock.release() path = local_theatre().import_script(module_name) imp.acquire_lock() imp.load_source(modulename, path) imp.release_lock() return
def incoming_actor(self, actorstate): # Create actor but don't start # TODO: FIX- generation of id, handle local and non-local migrations. log.debug(self, 'receieved migrating actor %s' % actorstate) actor = Actor(local_theatre()) actor.state = actorstate old_actor_id = actor.state.actor_id type = actor.state.actortype if ids.ip(old_actor_id) == ids.ip_from_loc(self.here): # Migration has been local actor_id = ids.change_port(old_actor_id, ids.port_from_loc(self.here)) else: actor_id = ids.change_host(old_actor_id, self.here, self.shared_data.next_id_num()) actor.state.actor_id = actor_id self.__local_actor_store.add_network_actor(actor, actor_id) actor.start() # At the moment even core migrations trigger re-registrations # Could probably forgo these if core migrations are frequent. local_theatre().globally_reregister_network_actor(actor_id, type, old_actor_id) names = actor.state.names for name in names: local_theatre().name(name, actor_id)
def migrate_to(self, actor, address): print 'in migrator' log.debug(self, 'about to migrate %s to %s' % (actor, address)) if address == local_theatre().gethostname(): # Still add an arrive message since the actor requested a local # migration and may be relying the arrived notification actor.state.add_arrived() log.debug(self, 'pointless (local) migration') return True # Inform the local locator that the actor cannot service any # further requests on this host self.__local_actor_store.remove(actor.state.actor_id) # try: self.__migrate_remote(actor, address) # except: # return False # Stop the actor's process loop actor.stop() log.debug(self, 'stopped local runner for actor %s' % actor) return True
def close_socket(socket): manager_loc = local_theatre().get_manager_loc() network_locator = rpc.RPCConnector(manager_loc) manager = network_locator.connect() manager.close_socket(socket.socket_id)
def connect_socket(address): manager_loc = local_theatre().get_manager_loc() network_locator = rpc.RPCConnector(manager_loc) manager = network_locator.connect() return manager.connect_socket(address)
def open_socket(port): manager_loc = local_theatre().get_manager_loc() network_locator = rpc.RPCConnector(manager_loc) manager = network_locator.connect() return manager.open_socket(port)
def migrate_or_die(): atheatre = local_theatre().atheatre() if atheatre: migrate_to(atheatre)
def here(): return local_theatre().gethostname()
def birth(self, port): print local_theatre().gethostname() sleep(1) port.getsockname()
def create_actor(self, cls, module_name, type, args, kwds): return local_theatre().create_actor(cls, module_name, type, args, kwds)
def local(actor_id): return Reference("%s-%s" % (actor_id , local_theatre().gethostname()))
def send(actor_ref, name, *args, **kwds): actor_id = actor_ref if hasattr(actor_ref, 'actor_id'): actor_id = actor_ref.actor_id message = RequestMessage(0, name, args, kwds) return local_theatre().send(actor_id, message)