1
2 """Base classes handy for use with PB clients.
3 """
4
5 from twisted.spread import pb
6
7 from twisted.spread.pb import PBClientFactory
8 from twisted.internet import protocol, reactor
9 from twisted.python import log
10
13 """Reconnecting client factory for PB brokers.
14
15 Like PBClientFactory, but if the connection fails or is lost, the factory
16 will attempt to reconnect.
17
18 Instead of using f.getRootObject (which gives a Deferred that can only
19 be fired once), override the gotRootObject method.
20
21 Instead of using the newcred f.login (which is also one-shot), call
22 f.startLogin() with the credentials and client, and override the
23 gotPerspective method.
24
25 gotRootObject and gotPerspective will be called each time the object is
26 received (once per successful connection attempt). You will probably want
27 to use obj.notifyOnDisconnect to find out when the connection is lost.
28
29 If an authorization error occurs, failedToGetPerspective() will be
30 invoked.
31
32 To use me, subclass, then hand an instance to a connector (like
33 TCPClient).
34 """
35
36
37
38 hungConnectionTimer = None
39 HUNG_CONNECTION_TIMEOUT = 120
40
46
52
55
61
62
63
65 raise RuntimeError, "login is one-shot: use startLogin instead"
66
68 self._credentials = credentials
69 self._client = client
70
76
77
78
85
86 self.hungConnectionTimer = reactor.callLater(self.HUNG_CONNECTION_TIMEOUT, hungConnection)
87
92
93
94
96 """The remote avatar or perspective (obtained each time this factory
97 connects) is now available."""
98 self.stopHungConnectionTimer()
99
101 """The remote root object (obtained each time this factory connects)
102 is now available. This method will be called each time the connection
103 is established and the object reference is retrieved."""
104 self.stopHungConnectionTimer()
105
107 """The login process failed, most likely because of an authorization
108 failure (bad password), but it is also possible that we lost the new
109 connection before we managed to send our credentials.
110 """
111 log.msg("ReconnectingPBClientFactory.failedToGetPerspective")
112 self.stopHungConnectionTimer()
113 if why.check(pb.PBConnectionLost):
114 log.msg("we lost the brand-new connection")
115
116 return
117
118 self.stopTrying()
119 log.err(why)
120 reactor.stop()
121