~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Epsilon/epsilon/hotfixes/timeoutmixin_calllater.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
from twisted.internet import reactor
3
 
 
4
 
class TimeoutMixin:
5
 
    """Mixin for protocols which wish to timeout connections
6
 
 
7
 
    @cvar timeOut: The number of seconds after which to timeout the connection.
8
 
    """
9
 
    timeOut = None
10
 
 
11
 
    __timeoutCall = None
12
 
 
13
 
    def callLater(self, period, func):
14
 
        return reactor.callLater(period, func)
15
 
 
16
 
 
17
 
    def resetTimeout(self):
18
 
        """Reset the timeout count down"""
19
 
        if self.__timeoutCall is not None and self.timeOut is not None:
20
 
            self.__timeoutCall.reset(self.timeOut)
21
 
 
22
 
    def setTimeout(self, period):
23
 
        """Change the timeout period
24
 
 
25
 
        @type period: C{int} or C{NoneType}
26
 
        @param period: The period, in seconds, to change the timeout to, or
27
 
        C{None} to disable the timeout.
28
 
        """
29
 
        prev = self.timeOut
30
 
        self.timeOut = period
31
 
 
32
 
        if self.__timeoutCall is not None:
33
 
            if period is None:
34
 
                self.__timeoutCall.cancel()
35
 
                self.__timeoutCall = None
36
 
            else:
37
 
                self.__timeoutCall.reset(period)
38
 
        elif period is not None:
39
 
            self.__timeoutCall = self.callLater(period, self.__timedOut)
40
 
 
41
 
        return prev
42
 
 
43
 
    def __timedOut(self):
44
 
        self.__timeoutCall = None
45
 
        self.timeoutConnection()
46
 
 
47
 
    def timeoutConnection(self):
48
 
        """Called when the connection times out.
49
 
        Override to define behavior other than dropping the connection.
50
 
        """
51
 
        self.transport.loseConnection()
52
 
 
53
 
 
54
 
def install():
55
 
    global TimeoutMixin
56
 
 
57
 
    from twisted.protocols import policies
58
 
    policies.TimeoutMixin.__dict__ = TimeoutMixin.__dict__
59
 
    policies.TimeoutMixin.__dict__['module'] = 'twisted.protocols.policies'
60
 
    TimeoutMixin = policies.TimeoutMixin