diff options
author | kali <kali@leap.se> | 2012-10-08 07:57:42 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-10-08 07:57:42 +0900 |
commit | 4d195cd5b6e6600ccf0df78946cab6038a1fcaec (patch) | |
tree | 1362b2844f7e008cad8c29f26861566129af6edd /src/leap/base/network.py | |
parent | 3a77603eae5fea0b1efb226860e0264ccf96f41b (diff) | |
parent | 6728eb9afb21bad867e4052a6190a9bdb34c928a (diff) |
Merge branch 'feature/network_check' into develop
Conflicts:
src/leap/baseapp/mainwindow.py
src/leap/eip/tests/test_checks.py
Diffstat (limited to 'src/leap/base/network.py')
-rw-r--r-- | src/leap/base/network.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/leap/base/network.py b/src/leap/base/network.py new file mode 100644 index 00000000..e90139c4 --- /dev/null +++ b/src/leap/base/network.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from __future__ import (print_function) +import logging +import threading + +from leap.base.checks import LeapNetworkChecker +from leap.base.constants import ROUTE_CHECK_INTERVAL +from leap.base.exceptions import TunnelNotDefaultRouteError +from leap.util.coroutines import (launch_thread, process_events) + +from time import sleep + +logger = logging.getLogger(name=__name__) + + +class NetworkCheckerThread(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) + self.error_cb = kwargs.pop( + 'error_cb', + lambda exc: logger.error("%s", exc.message)) + self.shutdown = threading.Event() + + # XXX get provider_gateway and pass it to checker + # see in eip.config for function + # #718 + self.checker = LeapNetworkChecker() + + def start(self): + self.process_handle = self._launch_recurrent_network_checks( + (self.error_cb,)) + + def stop(self): + self.shutdown.set() + logger.debug("network checked stopped.") + + 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): + #TODO: replace this with waiting for a signal from openvpn + while True: + try: + self.checker.check_tunnel_default_interface() + break + except TunnelNotDefaultRouteError: + # XXX ??? why do we sleep here??? + sleep(1) + + fail_observer_dict = dict((( + observer, + process_events(observer)) for observer in fail_callbacks)) + while not self.shutdown.is_set(): + try: + self.checker.check_tunnel_default_interface() + self.checker.check_internet_connection() + sleep(ROUTE_CHECK_INTERVAL) + except Exception as exc: + for obs in fail_observer_dict: + fail_observer_dict[obs].send(exc) + sleep(ROUTE_CHECK_INTERVAL) + #reset event + self.shutdown.clear() + + def _launch_recurrent_network_checks(self, fail_callbacks): + #we need to wrap the fail callback in a tuple + watcher = launch_thread( + self._network_checks_thread, + (fail_callbacks,)) + return watcher |