def test_split_id(): assert tools.split_id("actor@hive") == ["actor", "hive"] assert tools.split_id("actor") == ["actor", None] # We really shouldn't have any formatted like this, but just # in case... assert tools.split_id("actor@hive@garbage") == ["actor", "hive@garbage"]
def _process_message(self, message): # Route appropriately here actor_id, hive_id = split_id(message.to) ## Is the actor local? Send it! if hive_id == self.hive_id: try: actor = self._actor_registry[actor_id] except IndexError: # For some reason this actor wasn't found, so we may need to # inform the original sender _log.warning('recipient not found for message: {0}'.format( message)) self.return_to_sender(message) # Maybe not the most opportune place to attach this message.hive_proxy = actor.hive # TODO: More error handling here! ;) actor.handle_message(message) ## Looks like the actor must be remote, forward it! else: # Get the associated ambassador ## TODO: error handling if hive doesn't exist ;) ambassador_id = self._ambassadors[hive_id] ambassador = self._actor_registry[ambassador_id] # repackage the message for sending repackaged_message = self._repackage_message_for_forwarding( message, ambassador) # send it off! ambassador.handle_message(repackaged_message)
def _process_message(self, message): # Route appropriately here actor_id, hive_id = split_id(message.to) ## Is the actor local? Send it! if hive_id == self.hive_id: try: actor = self._actor_registry[actor_id] except IndexError: # For some reason this actor wasn't found, so we may need to # inform the original sender _log.warning( 'recipient not found for message: {0}'.format(message)) self.return_to_sender(message) # Maybe not the most opportune place to attach this message.hive_proxy = actor.hive # TODO: More error handling here! ;) actor.handle_message(message) ## Looks like the actor must be remote, forward it! else: # Get the associated ambassador ## TODO: error handling if hive doesn't exist ;) ambassador_id = self._ambassadors[hive_id] ambassador = self._actor_registry[ambassador_id] # repackage the message for sending repackaged_message = self._repackage_message_for_forwarding( message, ambassador) # send it off! ambassador.handle_message(repackaged_message)
def remove_actor(self, actor_id): """ Remove an actor from the hive """ if is_qualified_id(actor_id): actor_id = split_id(actor_id)[0] self._actor_registry.pop(actor_id)
def register_ambassador(self, message): """ Register this actor as being the ambassador for some specific hive id """ from_actor_id, from_hive_id = split_id(message.from_id) # Make sure this actor is from our hive assert from_hive_id == self.hive_id or from_hive_id is None self._ambassadors[message.body["hive_id"]] = from_actor_id
def _send_distant_message(self, msg): _, hive_id = split_id(msg.to) try: writer = self.known_distant_hives[hive_id] except: pdb.set_trace() _log.critical("this hive is not known to us") message_to_send = self._pack_message_to_send(msg) writer.write(message_to_send)
def unregister_ambassador(self, message): """ Unregister this actor as being the ambassador for some specific hive id """ from_actor_id, from_hive_id = split_id(message.from_id) assert from_hive_id == self.hive_id or from_hive_id is None old_ambassador_id = self._ambassadors.pop(message.body["hive_id"]) # Make sure this actor is really the one it said it was # (though this only possibly could help find bugs) assert old_ambassador_id == from_actor_id
def __init__(self, hive, id): self.hive = hive self.id = id self.local_id = split_id(id)[0] # Routing of messages to handler functions self.message_routing = {} # Registry on coroutines that are currently waiting for a response self._waiting_coroutines = {}
def _process_message(self, message): actor_id, hive_id = split_id(message.to) # if hive_id == "general": # pdb.set_trace() if hive_id == None and actor_id == self.hive_id: hive_id = actor_id ## Is the actor local? Send it! if hive_id == self.hive_id: try: actor = self._actor_registry[actor_id] except IndexError: # For some reason this actor wasn't found, so we may need to # inform the original sender _log.warning( 'recipient not found for message: {0}'.format(message)) self.return_to_sender(message) # Maybe not the most opportune place to attach this message.hive_proxy = actor.hive # TODO: More error handling here! ;) actor.handle_message(message) ## Looks like the actor must be remote, forward it! else: if hive_id not in self.known_distant_hives: _log.critical("unknown hive") pdb.set_trace() else: self._send_distant_message(message)
def _register_client(self, client_id, writer): _, sender_hive_id = split_id(client_id) if sender_hive_id not in self.known_distant_hives: self.known_distant_hives[sender_hive_id] = writer