1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

# This file is part of Buildbot.  Buildbot is free software: you can 

# redistribute it and/or modify it under the terms of the GNU General Public 

# License as published by the Free Software Foundation, version 2. 

# 

# This program is distributed in the hope that it will be useful, but WITHOUT 

# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 

# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 

# details. 

# 

# You should have received a copy of the GNU General Public License along with 

# this program; if not, write to the Free Software Foundation, Inc., 51 

# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 

# 

# Copyright Buildbot Team Members 

 

 

"""Base classes handy for use with PB clients. 

""" 

 

from twisted.spread import pb 

 

from twisted.spread.pb import PBClientFactory 

from twisted.internet import protocol, reactor 

from twisted.python import log 

from twisted.cred import error 

 

class ReconnectingPBClientFactory(PBClientFactory, 

                                  protocol.ReconnectingClientFactory): 

    """Reconnecting client factory for PB brokers. 

 

    Like PBClientFactory, but if the connection fails or is lost, the factory 

    will attempt to reconnect. 

 

    Instead of using f.getRootObject (which gives a Deferred that can only 

    be fired once), override the gotRootObject method. 

 

    Instead of using the newcred f.login (which is also one-shot), call 

    f.startLogin() with the credentials and client, and override the 

    gotPerspective method. 

 

    gotRootObject and gotPerspective will be called each time the object is 

    received (once per successful connection attempt). You will probably want 

    to use obj.notifyOnDisconnect to find out when the connection is lost. 

 

    If an authorization error occurs, failedToGetPerspective() will be 

    invoked. 

 

    To use me, subclass, then hand an instance to a connector (like 

    TCPClient). 

    """ 

 

    # hung connections wait for a relatively long time, since a busy master may 

    # take a while to get back to us. 

    hungConnectionTimer = None 

    HUNG_CONNECTION_TIMEOUT = 120 

 

    def clientConnectionFailed(self, connector, reason): 

        PBClientFactory.clientConnectionFailed(self, connector, reason) 

        if self.continueTrying: 

            self.connector = connector 

            self.retry() 

 

    def clientConnectionLost(self, connector, reason): 

        PBClientFactory.clientConnectionLost(self, connector, reason, 

                                             reconnecting=True) 

        RCF = protocol.ReconnectingClientFactory 

        RCF.clientConnectionLost(self, connector, reason) 

 

    def startedConnecting(self, connector): 

        self.startHungConnectionTimer(connector) 

 

    def clientConnectionMade(self, broker): 

        self.resetDelay() 

        PBClientFactory.clientConnectionMade(self, broker) 

        self.doLogin(self._root, broker) 

        self.gotRootObject(self._root) 

 

    # newcred methods 

 

    def login(self, *args): 

        raise RuntimeError, "login is one-shot: use startLogin instead" 

 

    def startLogin(self, credentials, client=None): 

        self._credentials = credentials 

        self._client = client 

 

    def doLogin(self, root, broker): 

        # newcred login() 

        d = self._cbSendUsername(root, self._credentials.username, 

                                 self._credentials.password, self._client) 

        d.addCallbacks(self.gotPerspective, self.failedToGetPerspective, 

                errbackArgs=(broker,)) 

 

    # timer for hung connections 

 

    def startHungConnectionTimer(self, connector): 

        self.stopHungConnectionTimer() 

        def hungConnection(): 

            log.msg("connection attempt timed out (is the port number correct?)") 

            self.hungConnectionTimer = None 

            connector.disconnect() 

            # (this will trigger the retry) 

        self.hungConnectionTimer = reactor.callLater(self.HUNG_CONNECTION_TIMEOUT, hungConnection) 

 

    def stopHungConnectionTimer(self): 

        if self.hungConnectionTimer: 

            self.hungConnectionTimer.cancel() 

        self.hungConnectionTimer = None 

 

    # methods to override 

 

    def gotPerspective(self, perspective): 

        """The remote avatar or perspective (obtained each time this factory 

        connects) is now available.""" 

        self.stopHungConnectionTimer() 

 

    def gotRootObject(self, root): 

        """The remote root object (obtained each time this factory connects) 

        is now available. This method will be called each time the connection 

        is established and the object reference is retrieved.""" 

        self.stopHungConnectionTimer() 

 

    def failedToGetPerspective(self, why, broker): 

        """The login process failed, most likely because of an authorization 

        failure (bad password), but it is also possible that we lost the new 

        connection before we managed to send our credentials. 

        """ 

        log.msg("ReconnectingPBClientFactory.failedToGetPerspective") 

        self.stopHungConnectionTimer() 

        # put something useful in the logs 

        if why.check(pb.PBConnectionLost): 

            log.msg("we lost the brand-new connection") 

            # fall through 

        elif why.check(error.UnauthorizedLogin): 

            log.msg("unauthorized login; check slave name and password") 

            # fall through 

        else: 

            log.err(why, 'While trying to connect:') 

            self.stopTrying() 

            reactor.stop() 

            return 

 

        # lose the current connection, which will trigger a retry 

        broker.transport.loseConnection()