root / ncclient / transport / session.py @ c35cebbf
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 |
|
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(Thread, Subject): |
24 |
|
25 |
"TODO: docstring"
|
26 |
|
27 |
def __init__(self): |
28 |
"TODO: docstring"
|
29 |
Subject.__init__(self)
|
30 |
Thread.__init__(self, name='session') |
31 |
self.setDaemon(True) |
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 |
|
37 |
def _post_connect(self): |
38 |
"TODO: docstring"
|
39 |
self.send(HelloHandler.build(self._client_capabilities)) |
40 |
error = None
|
41 |
init_event = Event() |
42 |
# callbacks
|
43 |
def ok_cb(id, capabilities): |
44 |
self._id, self._server_capabilities = id, Capabilities(capabilities) |
45 |
init_event.set() |
46 |
def err_cb(err): |
47 |
error = err |
48 |
init_event.set() |
49 |
listener = HelloHandler(ok_cb, err_cb) |
50 |
self.add_listener(listener)
|
51 |
# start the subclass' main loop
|
52 |
self.start()
|
53 |
# we expect server's hello message
|
54 |
init_event.wait() |
55 |
# received hello message or an error happened
|
56 |
self.remove_listener(listener)
|
57 |
if error:
|
58 |
raise error
|
59 |
logger.info('initialized: session-id=%s | server_capabilities=%s' %
|
60 |
(self.id, self.server_capabilities)) |
61 |
|
62 |
def connect(self, *args, **kwds): |
63 |
"TODO: docstring"
|
64 |
raise NotImplementedError |
65 |
|
66 |
def run(self): |
67 |
raise NotImplementedError |
68 |
|
69 |
### Properties
|
70 |
|
71 |
@property
|
72 |
def client_capabilities(self): |
73 |
return self._client_capabilities |
74 |
|
75 |
@property
|
76 |
def server_capabilities(self): |
77 |
return self._server_capabilities |
78 |
|
79 |
@property
|
80 |
def connected(self): |
81 |
return self._connected |
82 |
|
83 |
@property
|
84 |
def id(self): |
85 |
return self._id |