~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/pb/trap_client.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2009 Twisted Matrix Laboratories.
 
4
# See LICENSE for details.
 
5
 
 
6
from twisted.spread import pb, jelly
 
7
from twisted.python import log
 
8
from twisted.internet import reactor
 
9
 
 
10
class MyException(pb.Error): pass
 
11
class MyOtherException(pb.Error): pass
 
12
 
 
13
class ScaryObject:
 
14
    # not safe for serialization
 
15
    pass
 
16
 
 
17
def worksLike(obj):
 
18
    # the callback/errback sequence in class One works just like an
 
19
    # asynchronous version of the following:
 
20
    try:
 
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
 
28
    except:
 
29
        print " something else happened"
 
30
    else:
 
31
        print " method successful, response:", response
 
32
 
 
33
class One:
 
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"
 
39
        return None
 
40
    def check_MyException(self, failure):
 
41
        which = failure.trap(MyException, MyOtherException)
 
42
        if which == MyException:
 
43
            print " remote raised a MyException"
 
44
        else:
 
45
            print " remote raised a MyOtherException"
 
46
        return None
 
47
    def catch_everythingElse(self, failure):
 
48
        print " something else happened"
 
49
        log.err(failure)
 
50
        return None
 
51
 
 
52
    def doCall(self, explanation, arg):
 
53
        print explanation
 
54
        try:
 
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"
 
62
 
 
63
    def callOne(self):
 
64
        self.doCall("callOne: call with safe object", "safe string")
 
65
    def callTwo(self):
 
66
        self.doCall("callTwo: call with dangerous object", ScaryObject())
 
67
    def callThree(self):
 
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")
 
72
    def callFour(self):
 
73
        self.doCall("callFour: call on stale reference", "dummy")
 
74
        
 
75
    def got_obj(self, obj):
 
76
        self.remote = 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)
 
83
 
 
84
factory = pb.PBClientFactory()
 
85
reactor.connectTCP("localhost", 8800, factory)
 
86
deferred = factory.getRootObject()
 
87
deferred.addCallback(One().got_obj)
 
88
reactor.run()