3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
6 from twisted.spread import pb
8 class MasterDuckPond(pb.Cacheable):
9 def __init__(self, ducks):
13 print "I have [%d] ducks" % len(self.ducks)
14 def addDuck(self, duck):
15 self.ducks.append(duck)
16 for o in self.observers: o.callRemote('addDuck', duck)
17 def removeDuck(self, duck):
18 self.ducks.remove(duck)
19 for o in self.observers: o.callRemote('removeDuck', duck)
20 def getStateToCacheAndObserveFor(self, perspective, observer):
21 self.observers.append(observer)
22 # you should ignore pb.Cacheable-specific state, like self.observers
23 return self.ducks # in this case, just a list of ducks
24 def stoppedObserving(self, perspective, observer):
25 self.observers.remove(observer)
27 class SlaveDuckPond(pb.RemoteCache):
28 # This is a cache of a remote MasterDuckPond
30 return len(self.cacheducks)
32 return self.cacheducks
33 def setCopyableState(self, state):
34 print " cache - sitting, er, setting ducks"
35 self.cacheducks = state
36 def observe_addDuck(self, newDuck):
37 print " cache - addDuck"
38 self.cacheducks.append(newDuck)
39 def observe_removeDuck(self, deadDuck):
40 print " cache - removeDuck"
41 self.cacheducks.remove(deadDuck)
43 pb.setUnjellyableForClass(MasterDuckPond, SlaveDuckPond)