Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/lib/purk/scripts/timeout.py
blob: 2f0f58524308da11f23b567e4f7d84525c42aa0c (plain)
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
import time

import ui
from conf import conf

def setupRaw(e):
    e.network._message_timeout = False

def onSocketConnect(e):
    timeout = conf.get("server_traffic_timeout", 120)*1000
    e.network._message_timeout = False
    if timeout:
        e.network._message_timeout_source = ui.register_timer(timeout, check_timeout, e.network)
    else:
        e.network._message_timeout_source = None

def check_timeout(network):
    if network._message_timeout:
        network.raw("PING %s" % network.me)
        timeout = conf.get("server_death_timeout", 240)*1000
        network._message_timeout_source = ui.register_timer(timeout, check_death_timeout, network)
        return False
    else:
        network._message_timeout = True
        return True # call this function again

def check_death_timeout(network):
    if network._message_timeout:
        network.raw("QUIT :Server missing, presumed dead")
        network.disconnect(error="The server seems to have stopped talking to us")
    else:
        network._message_timeout = False
        timeout = conf.get("server_traffic_timeout", 120)*1000
        if timeout:
            network._message_timeout_source = ui.register_timer(timeout, check_timeout, network)
        else:
            network._message_timeout_source = None

def onDisconnect(e):
    try:
        if e.network._message_timeout_source:
            e.network._message_timeout_source.unregister()
            e.network._message_timeout_source = None
    except AttributeError:
        pass