From 58344bb28c1c0f25ed37624ff487cc8f24821d52 Mon Sep 17 00:00:00 2001 From: antialias Date: Fri, 28 Sep 2012 18:16:47 -0400 Subject: Functionality to shutdown network checker when openvpn is stopped. But thread not being successfully killed. --- src/leap/base/network.py | 40 +++++++++++++++++++++++++++++----------- src/leap/baseapp/eip.py | 2 ++ src/leap/baseapp/network.py | 7 ++++--- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/leap/base/network.py b/src/leap/base/network.py index 58f903e1..a1e7c880 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -1,12 +1,18 @@ # -*- 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.util.coroutines import (launch_thread, process_events) +from leap.base.exceptions import TunnelNotDefaultRouteError +from leap.util.coroutines import (launch_thread_no_daemon, process_events) from time import sleep +logger = logging.getLogger(name=__name__) + + class NetworkChecker(object): """ Manages network checking thread that makes sure we have a working network @@ -15,12 +21,17 @@ class NetworkChecker(object): def __init__(self, *args, **kwargs): self.status_signals = kwargs.pop('status_signals', None) self.watcher_cb = kwargs.pop('status_signals', None) + self.excp_logger = lambda exc: logger.error("%s", exc.message) + self.checker = LeapNetworkChecker() def start(self): - self._launch_recurrent_network_checks((self.watcher_cb,)) + self.process_handle = self._launch_recurrent_network_checks((self.excp_logger,)) def stop(self): - raise NotImplementedError + #TODO: Thread still not being stopped when openvpn is stopped. + logger.debug("stopping network checker...") + self.process_handle._Thread__stop() + logger.debug("network checked stopped.") def run_checks(self): pass @@ -31,24 +42,31 @@ class NetworkChecker(object): #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)) + #TODO: replace this with waiting for a signal from openvpn + while True: + try: + self.checker.check_tunnel_default_interface() + break + except TunnelNotDefaultRouteError: + sleep(1) + observer_dict = dict((( observer, process_events(observer)) for observer in fail_callbacks)) - netchecker = LeapNetworkChecker() while True: try: - netchecker.check_internet_connection() + self.checker.check_tunnel_default_interface() + self.checker.check_internet_connection() sleep(ROUTE_CHECK_INTERVAL) except Exception as exc: for obs in observer_dict: observer_dict[obs].send(exc) + sleep(ROUTE_CHECK_INTERVAL) - def _launch_recurrent_network_checks(fail_callbacks): - print(type(fail_callbacks)) - watcher = launch_thread( - network_checks_thread, + def _launch_recurrent_network_checks(self, fail_callbacks): + #we need to wrap the fail callback in a turple + watcher = launch_thread_no_daemon( + self._network_checks_thread, (fail_callbacks,)) return watcher diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py index b0e14be7..ad074abc 100644 --- a/src/leap/baseapp/eip.py +++ b/src/leap/baseapp/eip.py @@ -224,9 +224,11 @@ class EIPConductorAppMixin(object): # we could bring Timer Init to this Mixin # or to its own Mixin. self.timer.start(constants.TIMER_MILLISECONDS) + self.network_checker.start() return if self.eip_service_started is True: + self.network_checker.stop() self.conductor.disconnect() if self.debugmode: self.startStopButton.setText('&Connect') diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index 42a42fcd..75690cc9 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -36,7 +36,7 @@ class NetworkCheckerAppMixin(object): errq = self.conductor.error_queue while errq.qsize() != 0: - logger.debug('%s errors left in conductor queue', errq.qsize()) + logger.debug('%s errors left in network queue', errq.qsize()) # we get exception and original traceback from queue error, tb = errq.get() @@ -44,7 +44,7 @@ class NetworkCheckerAppMixin(object): logger.error('%s: %s', error.__class__.__name__, error.message) if issubclass(error.__class__, eip_exceptions.EIPClientError): - self.handle_eip_error(error) + self.handle_network_error(error) else: # deprecated form of raising exception. @@ -53,4 +53,5 @@ class NetworkCheckerAppMixin(object): if error.failfirst is True: break - + def handle_network_error(self, error): + pass -- cgit v1.2.3