Statistics
| Branch: | Tag: | Revision:

root / ncclient / transport / session.py @ 68006767

History | View | Annotate | Download (2.5 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 Thread, Event
16
from ncclient.capabilities import Capabilities, CAPABILITIES
17

    
18
import hello
19
from . import logger
20
from ncclient.glue import Subject
21

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

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