root / ncclient / session.py @ 2acc860a
History | View | Annotate | Download (2.8 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 |
import logging |
16 |
from threading import Thread, Event |
17 |
from Queue import Queue |
18 |
|
19 |
import content |
20 |
from capabilities import Capabilities, CAPABILITIES |
21 |
from error import ClientError |
22 |
from subject import Subject |
23 |
|
24 |
logger = logging.getLogger('ncclient.session')
|
25 |
|
26 |
class SessionError(ClientError): pass |
27 |
|
28 |
class Session(Thread, Subject): |
29 |
|
30 |
def __init__(self): |
31 |
Thread.__init__(self, name='session') |
32 |
Subject.__init__(self, listeners=[HelloListener(self)]) |
33 |
self._client_capabilities = CAPABILITIES
|
34 |
self._server_capabilities = None # yet |
35 |
self._id = None # session-id |
36 |
self._error = None |
37 |
self._init_event = Event()
|
38 |
self._q = Queue()
|
39 |
self._connected = False # to be set/cleared by subclass |
40 |
|
41 |
def _post_connect(self): |
42 |
# start the subclass' main loop
|
43 |
self.start()
|
44 |
# queue client's hello message for sending
|
45 |
self.send(content.Hello.build(self._client_capabilities)) |
46 |
# we expect server's hello message, wait for _init_event to be set by HelloListener
|
47 |
self._init_event.wait()
|
48 |
# there may have been an error
|
49 |
if self._error: |
50 |
self._close()
|
51 |
raise self._error |
52 |
|
53 |
def initialize(self, id, capabilities): |
54 |
self._id, self._capabilities = id, Capabilities(capabilities) |
55 |
self._init_event.set()
|
56 |
|
57 |
def initialize_error(self, err): |
58 |
self._error = err
|
59 |
self._init_event.set()
|
60 |
|
61 |
def send(self, message): |
62 |
logger.debug('queueing message: \n%s' % message)
|
63 |
self._q.put(message)
|
64 |
|
65 |
def connect(self): |
66 |
raise NotImplementedError |
67 |
|
68 |
def run(self): |
69 |
raise NotImplementedError |
70 |
|
71 |
def capabilities(self, whose='client'): |
72 |
if whose == 'client': |
73 |
return self._client_capabilities |
74 |
elif whose == 'server': |
75 |
return self._server_capabilities |
76 |
|
77 |
### Properties
|
78 |
|
79 |
@property
|
80 |
def client_capabilities(self): |
81 |
return self._client_capabilities |
82 |
|
83 |
@property
|
84 |
def server_capabilities(self): |
85 |
return self._server_capabilities |
86 |
|
87 |
@property
|
88 |
def connected(self): |
89 |
return self._connected |
90 |
|
91 |
@property
|
92 |
def id(self): |
93 |
return self._id |