3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
6 from twisted.spread import pb, jelly
7 from twisted.python import log
8 from twisted.internet import reactor
10 class MyException(pb.Error): pass
11 class MyOtherException(pb.Error): pass
14 # not safe for serialization
18 # the callback/errback sequence in class One works just like an
19 # asynchronous version of the following:
21 response = obj.callMethod(name, arg)
22 except pb.DeadReferenceError:
23 print " stale reference: the client disconnected or crashed"
24 except jelly.InsecureJelly:
25 print " InsecureJelly: you tried to send something unsafe to them"
26 except (MyException, MyOtherException):
27 print " remote raised a MyException" # or MyOtherException
29 print " something else happened"
31 print " method successful, response:", response
34 def worked(self, response):
35 print " method successful, response:", response
36 def check_InsecureJelly(self, failure):
37 failure.trap(jelly.InsecureJelly)
38 print " InsecureJelly: you tried to send something unsafe to them"
40 def check_MyException(self, failure):
41 which = failure.trap(MyException, MyOtherException)
42 if which == MyException:
43 print " remote raised a MyException"
45 print " remote raised a MyOtherException"
47 def catch_everythingElse(self, failure):
48 print " something else happened"
52 def doCall(self, explanation, arg):
55 deferred = self.remote.callRemote("fooMethod", arg)
56 deferred.addCallback(self.worked)
57 deferred.addErrback(self.check_InsecureJelly)
58 deferred.addErrback(self.check_MyException)
59 deferred.addErrback(self.catch_everythingElse)
60 except pb.DeadReferenceError:
61 print " stale reference: the client disconnected or crashed"
64 self.doCall("callOne: call with safe object", "safe string")
66 self.doCall("callTwo: call with dangerous object", ScaryObject())
68 self.doCall("callThree: call that raises remote exception", "panic!")
69 def callShutdown(self):
70 print "telling them to shut down"
71 self.remote.callRemote("shutdown")
73 self.doCall("callFour: call on stale reference", "dummy")
75 def got_obj(self, obj):
77 reactor.callLater(1, self.callOne)
78 reactor.callLater(2, self.callTwo)
79 reactor.callLater(3, self.callThree)
80 reactor.callLater(4, self.callShutdown)
81 reactor.callLater(5, self.callFour)
82 reactor.callLater(6, reactor.stop)
84 factory = pb.PBClientFactory()
85 reactor.connectTCP("localhost", 8800, factory)
86 deferred = factory.getRootObject()
87 deferred.addCallback(One().got_obj)