[Twisted-Python] "Eww... Here, smell this!" (was Re: Multiplex)

Kevin Turner acapnotic at twistedmatrix.com
Sun Aug 12 06:05:09 EDT 2001

On Wed, Aug 08, 2001 at 06:48:59AM -0500, Glyph Lefkowitz wrote:
> This is what Cached was designed for :-).

I must be missin' something.  Here's my attempt.  Problems include not
being sure if I can really talk in the Cache -> Cached direction, and
the transparent automatic repeater creation alchemy doesn't even come

(no, it doesn't run.  yes, the names suck.  but none of that matters if
it doesn't work at all.)

class PlexPerspective(Perspective):
    """This is the perspective given by Bob's repeater service."""
    def perspective_setPlexTag(self, tag):
        """Register a copytag as being a plexed class."""
        # Do some sanity checking, make sure tag has a safe prefix
        # like "PLEX_" or somesuch.
        pb.setCopyTags(tag, PlexRepeater)

class PlexBase(Copied):
    """Plex base class -- always returns a consistant copytag

    (even if it's been through a repeater, where everything is a
    PlexRepeater, no matter what the tag.)
    def getStateToCopy(self):
        # Make sure _copytag gets into our state, which it otherwise
        # wouldn't do if our _copytag refers to the class attribute.
        state = pb.Copied.getStateToCopy(self)
        state['_copytag'] = self._copytag
        return state

    def getTypeToCopy(self):
        return self._copytag

class PlexRepeater(PlexBase, Cached, Copy):
    """This is how 'plexes look in the repeater.

    Repeaters don't have unique properties as they're all instances
    of this class, but they must remember the copytag to give to
    their children."""
    def setCopiedState(self, state):
        """Leaf -> Repeater alchemy.

        This is so new plexed objects may be created transparently, if
        a peer just passes through an instance of their leaf-class."""
        # XXX: How do I find out which broker I arrived through?
        # There isn't a setCopiedStateFor()

        moms_perspective = moms_broker.getPerspective()

        # I need to convince that broker that Mom is really a
        # remote cache of me.
        puid = self.ProcessUniqueID()
        moms_broker.remotelyCachedLUIDs[puid] = moms_luid
        moms_broker.remotelyCachedObjects[moms_luid] = Local(self)

        # Add Mom as an Observer
        observer = CacheObserver(moms_broker, self, moms_perspective)
        # Don't need to get the state, actually, but that is the
        # method that keeps my observer list healthy.
        self.getStateToCacheAndObserveFor(moms_perspective, observer)

        # XXX: Now I have to convince Mom's broker on her end to make
        # a CacheProxy for me, with Mom as the cNotProxy.

        pb.Copy.setCopiedState(self, state)

    def remoteMessageReceived(self, broker, message, args, kw):
        """Pass the word along to all the kids."""
        # ??? Do I receive remote messages?
        # Or do I have to pass this method as part of my cached state?

        for o in self.observers:
            self.remoteCacheDo(bla, bla, args, kw)

    def getStateToCacheAndObserveFor(self, perspective, observer):
        """keep a list of my kids."""


        return self.getStateToCopyFor(self, perspective)

class PlexLeaf(PlexBase, Cache):
    """This is subclassed by Alec, Amy, and Angus.

    it will be the setCopierForClass for a PLEX_ copytag, and thus
    created whenever a peer (most likely the repeater) passes a plex to

    It receives updates from the plex object it caches,
    and can talk back to that object as well (causing it to broadcast,
    if it is a repeater).

    My subclass should define a _copytag attribute to identify me when
    I'm passed to my peer if I was initialized locally, and not by the
    broker as a Cache.

    def __getattr__(self, key):

        # Shout out through the plex
        if key[:6] == "shout_":
            return getattr(self.cached, key[6:])

More information about the Twisted-Python mailing list