summaryrefslogtreecommitdiff
path: root/src/leap/base/connection.py
blob: 41d13935dcb8a6387eb51d2e1dd0d40648551c87 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Base Connection Classs
"""
from __future__ import (division, unicode_literals, print_function)

import logging

from leap.base.authentication import Authentication

logger = logging.getLogger(name=__name__)


class Connection(Authentication):
    # JSONLeapConfig
    #spec = {}

    def __init__(self, *args, **kwargs):
        self.connection_state = None
        self.desired_connection_state = None
        #XXX FIXME diamond inheritance gotcha..
        #If you inherit from >1 class,
        #super is only initializing one
        #of the bases..!!
        # I think we better pass config as a constructor
        # parameter -- kali 2012-08-30 04:33
        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 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