def _on_receive(self, sender_nid, msg_bytes): pickler = IncomingMessageUnpickler(self, StringIO(msg_bytes)) try: loaded = pickler.load() except Exception: return # malformed input try: local_path, message, sender = loaded except Exception: return # malformed input cell = self.guardian.lookup_cell(Uri.parse(local_path)) if not cell: if ('_watched', ANY) == message: watched_ref = Ref(cell=None, node=self, uri=Uri.parse(self.nid + local_path), is_local=True) _, watcher = message watcher << ('terminated', watched_ref) elif message in (('terminated', ANY), ('_watched', ANY), ('_unwatched', ANY)): pass else: self._remote_dead_letter(local_path, message, sender) else: cell.receive(message, sender)
def __setstate__(self, uri): # if it's a tuple, it's a remote `Ref` and the tuple origates from IncomingMessageUnpickler, # otherwise it must be just a local `Ref` being pickled and unpickled for whatever reason: if isinstance(uri, tuple): self.is_local = False uri, self.node = uri self.uri = Uri.parse(uri)
def _remote_dead_letter(self, path, msg, sender): ref = Ref(cell=None, uri=Uri.parse(self.nid + path), node=self, is_local=True) if not (msg == ('_unwatched', ANY) or msg == ('_watched', ANY)): Events.log(DeadLetter(ref, msg, sender))
def lookup_ref(self, uri): if not isinstance(uri, (Uri, str)): raise TypeError("%s.lookup_ref expects a str or Uri" % type(self).__name__) # pragma: no cover uri = uri if isinstance(uri, Uri) else Uri.parse(uri) assert not uri.node or uri.node == self.uri.node cell = self.lookup_cell(uri) if not cell: raise LookupFailed("Look-up of local actor failed: %s" % (uri,)) return cell.ref
def lookup_ref(self, uri): if not isinstance(uri, (Uri, str)): raise TypeError("%s.lookup_ref expects a str or Uri" % type(self).__name__) # pragma: no cover uri = uri if isinstance(uri, Uri) else Uri.parse(uri) assert not uri.node or uri.node == self.uri.node cell = self.lookup_cell(uri) if not cell: raise LookupFailed("Look-up of local actor failed: %s" % (uri, )) return cell.ref
def __init__(self, nid=None, enable_remoting=False, enable_relay=False, hub_kwargs={}): self.nid = nid self._uri = Uri(name=None, parent=None, node=nid) self.guardian = Guardian(uri=self._uri, node=self) self._hub = (HubWithNoRemoting() if not enable_remoting else Hub( nid, enable_relay, on_node_down=lambda ref, nid: ref << ('_node_down', nid), on_receive=self._on_receive, **hub_kwargs))
def lookup_str(self, addr): if not isinstance(addr, str): raise TypeError("%s.lookup_str expects a str" % type(self).__name__) # pragma: no cover return self.lookup(Uri.parse(addr))
def lookup_str(self, addr): return self.lookup(Uri.parse(addr))