diff options
| author | antialias <antialias@leap.se> | 2012-08-13 22:53:40 -0700 | 
|---|---|---|
| committer | antialias <antialias@leap.se> | 2012-08-13 22:53:40 -0700 | 
| commit | e1103904fbdd9b54b53075956c279271c17e9a8f (patch) | |
| tree | 9e87eecd30d23d32a323ca365684dea63706d284 /src/leap/Connection.py | |
| parent | c46d8da153ac658c8bd145376e22b1218db1090a (diff) | |
First (non-working) pass at abstracting exisiting functionality into OO framework.
Diffstat (limited to 'src/leap/Connection.py')
| -rw-r--r-- | src/leap/Connection.py | 129 | 
1 files changed, 129 insertions, 0 deletions
| diff --git a/src/leap/Connection.py b/src/leap/Connection.py new file mode 100644 index 00000000..6534560b --- /dev/null +++ b/src/leap/Connection.py @@ -0,0 +1,129 @@ +""" +Base Connection Classs +""" +from __future__ import (division, unicode_literals, print_function) +#import threading +from functools import partial +import logging + +from leap.utils.coroutines import spawn_and_watch_process +from leap.baseapp.config import get_config, get_vpn_stdout_mockup +from leap.eip.vpnwatcher import EIPConnectionStatus, status_watcher +from leap.eip.vpnmanager import OpenVPNManager, ConnectionRefusedError + +from leap.Configuration  import Configuration +from leap.Authentication import Authentication + +logger = logging.getLogger(name=__name__) + +class Connection(Configuration, Authentication): +    def __init__(self, *args, **kwargs): +        self.connection_state = None +        self.desired_connection_state = None +        super(Connection, self).__init__(*args, **kwargs) + +    def connect(self): +        """ +        entry point for connection process +        """ +        pass + +    def disconnect(self): +        """ +        disconnects client +        """ +        pass + +    def shutdown(self): +        """ +        shutdown and quit +        """ +        self.desired_con_state = self.status.DISCONNECTED + +    def connection_state(self): +        """ +        returns the current connection state +        """ +        return self.status.current + +    def desired_connection_state(self): +        """ +        returns the desired_connection state +        """ +        return self.desired_connection_state + +    def poll_connection_state(self): +        """ +        """ +        try: +            state = self.get_connection_state() +        except ConnectionRefusedError: +            # connection refused. might be not ready yet. +            return +        if not state: +            return +        (ts, status_step, +         ok, ip, remote) = state +        self.status.set_vpn_state(status_step) +        status_step = self.status.get_readable_status() +        return (ts, status_step, ok, ip, remote) + +    def get_icon_name(self): +        """ +        get icon name from status object +        """ +        return self.status.get_state_icon() + +    # +    # private methods +    # + +    def _disconnect(self): +        """ +        private method for disconnecting +        """ +        if self.subp is not None: +            self.subp.terminate() +            self.subp = None +        # XXX signal state changes! :) + +    def _is_alive(self): +        """ +        don't know yet +        """ +        pass + +    def _connect(self): +        """ +        entry point for connection cascade methods. +        """ +        #conn_result = ConState.DISCONNECTED +        try: +            conn_result = self._try_connection() +        except UnrecoverableError as except_msg: +            logger.error("FATAL: %s" % unicode(except_msg)) +            conn_result = self.status.UNRECOVERABLE +        except Exception as except_msg: +            self.error_queue.append(except_msg) +            logger.error("Failed Connection: %s" % +                         unicode(except_msg)) +        return conn_result +	 + + +class ConnectionError(Exception): +    """ +    generic connection error +    """ +    def __str__(self): +        if len(self.args) >= 1: +            return repr(self.args[0]) +        else: +            raise self() + + +class UnrecoverableError(ConnectionError): +    """ +    we cannot do anything about it, sorry +    """ +    pass | 
