summaryrefslogtreecommitdiff
path: root/src/leap/base/network.py
blob: 58f903e119b891c138fb88b00c168bb6050e1ac7 (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
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
from __future__ import (print_function)

from leap.base.checks import LeapNetworkChecker
from leap.base.constants import ROUTE_CHECK_INTERVAL
from leap.util.coroutines import (launch_thread, process_events)

from time import sleep

class NetworkChecker(object):
    """
    Manages network checking thread that makes sure we have a working network
    connection.
    """
    def __init__(self, *args, **kwargs):
        self.status_signals = kwargs.pop('status_signals', None)
        self.watcher_cb = kwargs.pop('status_signals', None)

    def start(self):
        self._launch_recurrent_network_checks((self.watcher_cb,))

    def stop(self):
        raise NotImplementedError

    def run_checks(self):
        pass

    #private methods

    #here all the observers in fail_callbacks expect one positional argument,
    #which is exception so we can try by passing a lambda with logger to
    #check it works.
    def _network_checks_thread(self, fail_callbacks):
        print('fail_callbacks: %s' % fail_callbacks)
        print(len(fail_callbacks))
        observer_dict = dict(((
            observer, process_events(observer)) for observer in fail_callbacks))
        netchecker = LeapNetworkChecker()
        while True:
            try:
                netchecker.check_internet_connection()
                sleep(ROUTE_CHECK_INTERVAL)
            except Exception as exc:
                for obs in observer_dict:
                    observer_dict[obs].send(exc)


    def _launch_recurrent_network_checks(fail_callbacks):
        print(type(fail_callbacks))
        watcher = launch_thread(
            network_checks_thread,
            (fail_callbacks,))
        return watcher