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.
15 from threading import Thread, Event
17 from ncclient.capabilities import Capabilities, CAPABILITIES
18 from ncclient.glue import Subject
19 from ncclient.transport import logger
23 class Session(Thread, Subject):
29 Subject.__init__(self)
30 Thread.__init__(self, name='session')
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
37 def _post_connect(self):
39 self.send(hello.build(self._client_capabilities))
43 def ok_cb(id, capabilities):
44 self._id, self._server_capabilities = id, Capabilities(capabilities)
49 listener = hello.HelloListener(ok_cb, err_cb)
50 self.add_listener(listener)
51 # start the subclass' main loop
53 # we expect server's hello message
55 # received hello message or an error happened
56 self.remove_listener(listener)
59 logger.info('initialized: session-id=%s | server_capabilities=%s' %
60 (self.id, self.server_capabilities))
62 def connect(self, *args, **kwds):
64 raise NotImplementedError
67 raise NotImplementedError
72 def client_capabilities(self):
73 return self._client_capabilities
76 def server_capabilities(self):
77 return self._server_capabilities
81 return self._connected