1 # Copyright 2009 Shikhar Bhushan
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from threading import Thread, Event
17 from Queue import Queue
20 from capabilities import Capabilities, CAPABILITIES
21 from error import ClientError
22 from subject import Subject
24 logger = logging.getLogger('ncclient.session')
26 class SessionError(ClientError): pass
28 class Session(Thread, Subject):
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
37 self._init_event = Event()
39 self._connected = False # to be set/cleared by subclass
41 def _post_connect(self):
42 # start the subclass' main loop
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
53 def initialize(self, id, capabilities):
54 self._id, self._capabilities = id, Capabilities(capabilities)
55 self._init_event.set()
57 def initialize_error(self, err):
59 self._init_event.set()
61 def send(self, message):
62 logger.debug('queueing message: \n%s' % message)
66 raise NotImplementedError
69 raise NotImplementedError
71 def capabilities(self, whose='client'):
73 return self._client_capabilities
74 elif whose == 'server':
75 return self._server_capabilities
80 def client_capabilities(self):
81 return self._client_capabilities
84 def server_capabilities(self):
85 return self._server_capabilities
89 return self._connected