root / ncclient / transport / session.py @ d6688264
History | View | Annotate | Download (3 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 |
from Queue import Queue |
17 |
|
18 |
from ncclient.capabilities import Capabilities, CAPABILITIES |
19 |
from ncclient.glue import Subject |
20 |
|
21 |
from hello import HelloHandler |
22 |
|
23 |
import logging |
24 |
logger = logging.getLogger('ncclient.transport.session')
|
25 |
|
26 |
class Session(Subject): |
27 |
|
28 |
"TODO: docstring"
|
29 |
|
30 |
def __init__(self): |
31 |
"Subclass constructor should call this"
|
32 |
Subject.__init__(self)
|
33 |
self.setName('session') |
34 |
self._q = Queue()
|
35 |
self._client_capabilities = CAPABILITIES
|
36 |
self._server_capabilities = None # yet |
37 |
self._id = None # session-id |
38 |
self._connected = False # to be set/cleared by subclass implementation |
39 |
logger.debug('%r created: client_capabilities=%r' %
|
40 |
(self, self._client_capabilities)) |
41 |
|
42 |
def _post_connect(self): |
43 |
"Greeting stuff"
|
44 |
init_event = Event() |
45 |
error = [None] # so that err_cb can bind error[0]. just how it is. |
46 |
# callbacks
|
47 |
def ok_cb(id, capabilities): |
48 |
self._id = id |
49 |
self._server_capabilities = Capabilities(capabilities)
|
50 |
init_event.set() |
51 |
def err_cb(err): |
52 |
error[0] = err
|
53 |
init_event.set() |
54 |
listener = HelloHandler(ok_cb, err_cb) |
55 |
self.add_listener(listener)
|
56 |
self.send(HelloHandler.build(self._client_capabilities)) |
57 |
logger.debug('starting main loop')
|
58 |
self.start()
|
59 |
# we expect server's hello message
|
60 |
init_event.wait() |
61 |
# received hello message or an error happened
|
62 |
self.remove_listener(listener)
|
63 |
if error[0]: |
64 |
raise error[0] |
65 |
logger.info('initialized: session-id=%s | server_capabilities=%s' %
|
66 |
(self._id, self._server_capabilities)) |
67 |
|
68 |
def connect(self, *args, **kwds): |
69 |
"Subclass implements"
|
70 |
raise NotImplementedError |
71 |
|
72 |
def run(self): |
73 |
"Subclass implements"
|
74 |
raise NotImplementedError |
75 |
|
76 |
def send(self, message): |
77 |
"TODO: docstring"
|
78 |
logger.debug('queueing %s' % message)
|
79 |
self._q.put(message)
|
80 |
|
81 |
### Properties
|
82 |
|
83 |
@property
|
84 |
def client_capabilities(self): |
85 |
return self._client_capabilities |
86 |
|
87 |
@property
|
88 |
def server_capabilities(self): |
89 |
return self._server_capabilities |
90 |
|
91 |
@property
|
92 |
def connected(self): |
93 |
return self._connected |
94 |
|
95 |
@property
|
96 |
def id(self): |
97 |
return self._id |
98 |
|
99 |
@property
|
100 |
def can_pipeline(self): |
101 |
return True |