1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Base classes handy for use with PB clients.
18 """
19
20 from twisted.spread import pb
21
22 from twisted.spread.pb import PBClientFactory
23 from twisted.internet import protocol, reactor
24 from twisted.python import log
25
28 """Reconnecting client factory for PB brokers.
29
30 Like PBClientFactory, but if the connection fails or is lost, the factory
31 will attempt to reconnect.
32
33 Instead of using f.getRootObject (which gives a Deferred that can only
34 be fired once), override the gotRootObject method.
35
36 Instead of using the newcred f.login (which is also one-shot), call
37 f.startLogin() with the credentials and client, and override the
38 gotPerspective method.
39
40 gotRootObject and gotPerspective will be called each time the object is
41 received (once per successful connection attempt). You will probably want
42 to use obj.notifyOnDisconnect to find out when the connection is lost.
43
44 If an authorization error occurs, failedToGetPerspective() will be
45 invoked.
46
47 To use me, subclass, then hand an instance to a connector (like
48 TCPClient).
49 """
50
51
52
53 hungConnectionTimer = None
54 HUNG_CONNECTION_TIMEOUT = 120
55
61
67
70
76
77
78
80 raise RuntimeError, "login is one-shot: use startLogin instead"
81
83 self._credentials = credentials
84 self._client = client
85
91
92
93
100
101 self.hungConnectionTimer = reactor.callLater(self.HUNG_CONNECTION_TIMEOUT, hungConnection)
102
107
108
109
111 """The remote avatar or perspective (obtained each time this factory
112 connects) is now available."""
113 self.stopHungConnectionTimer()
114
116 """The remote root object (obtained each time this factory connects)
117 is now available. This method will be called each time the connection
118 is established and the object reference is retrieved."""
119 self.stopHungConnectionTimer()
120
122 """The login process failed, most likely because of an authorization
123 failure (bad password), but it is also possible that we lost the new
124 connection before we managed to send our credentials.
125 """
126 log.msg("ReconnectingPBClientFactory.failedToGetPerspective")
127 self.stopHungConnectionTimer()
128 if why.check(pb.PBConnectionLost):
129 log.msg("we lost the brand-new connection")
130
131 return
132
133 self.stopTrying()
134 log.err(why)
135 reactor.stop()
136