Statistics
| Branch: | Tag: | Revision:

root / ncclient / transport / session.py @ 1d540e60

History | View | Annotate | Download (2.7 kB)

1
# Copyright 2009 Shikhar Bhushan
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
from threading import Event
16

    
17
from ncclient.capabilities import Capabilities, CAPABILITIES
18
from ncclient.glue import Subject
19

    
20
from . import logger
21
from hello import HelloHandler
22

    
23
class Session(Subject):
24
    
25
    "TODO: docstring"
26
    
27
    def __init__(self):
28
        "TODO: docstring"
29
        Subject.__init__(self)
30
        self.setName('session')
31
        self.setDaemon(True) #hmm
32
        self._client_capabilities = CAPABILITIES
33
        self._server_capabilities = None # yet
34
        self._id = None # session-id
35
        self._connected = False # to be set/cleared by subclass implementation
36
        logger.debug('[session object created] client_capabilities=%r' %
37
                     self._client_capabilities)
38
    
39
    def _post_connect(self):
40
        "TODO: docstring"
41
        init_event = Event()
42
        error = [None] # so that err_cb can bind error[0]. just how it is.
43
        # callbacks
44
        def ok_cb(id, capabilities):
45
            self._id = id
46
            self._server_capabilities = Capabilities(capabilities)
47
            init_event.set()
48
        def err_cb(err):
49
            error[0] = err
50
            init_event.set()
51
        listener = HelloHandler(ok_cb, err_cb)
52
        self.add_listener(listener)
53
        self.send(HelloHandler.build(self._client_capabilities))
54
        logger.debug('[starting main loop]')
55
        self.start()
56
        # we expect server's hello message
57
        init_event.wait()
58
        # received hello message or an error happened
59
        self.remove_listener(listener)
60
        if error[0]:
61
            raise error[0]
62
        logger.info('initialized: session-id=%s | server_capabilities=%s' %
63
                     (self._id, self._server_capabilities))
64
    
65
    def connect(self, *args, **kwds):
66
        "TODO: docstring"
67
        raise NotImplementedError
68

    
69
    def run(self):
70
        raise NotImplementedError
71
    
72
    ### Properties
73
    
74
    @property
75
    def client_capabilities(self):
76
        return self._client_capabilities
77
    
78
    @property
79
    def server_capabilities(self):
80
        return self._server_capabilities
81
    
82
    @property
83
    def connected(self):
84
        return self._connected
85
    
86
    @property
87
    def id(self):
88
        return self._id