root / src / session.py @ d0296fee
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 |
from content import Creator, Parser |
16 |
|
17 |
from threading import Thread |
18 |
from listener import Subject, Listener |
19 |
|
20 |
class SessionError(ncclientError): pass |
21 |
|
22 |
class Session(Thread, Subject, Listener): |
23 |
|
24 |
CLIENT, SERVER = range(2) |
25 |
|
26 |
def __init__(self, capabilities=None, listeners=[]): |
27 |
Thread.__init__(self, name='session') |
28 |
listeners.append(self)
|
29 |
Subject.__init__(self, listeners=listeners)
|
30 |
Thread.setDaemon(True)
|
31 |
self._capabilities = {
|
32 |
CLIENT: capabilities, |
33 |
SERVER: None # yet |
34 |
} |
35 |
self._id = None # session-id |
36 |
self._connected = False |
37 |
self._initialised = False |
38 |
self._q = Queue.Queue()
|
39 |
|
40 |
def _init(self, id, capabilities): |
41 |
if isinstance(id, int) and isinstance(capabilities, Capabilities): |
42 |
self.id = id |
43 |
self.capabilities[SERVER] = capabilities
|
44 |
self.initialised = True |
45 |
else: # there was an error in parsing or such |
46 |
raise ValueError |
47 |
|
48 |
def _greet(self): |
49 |
hello = Creator() |
50 |
# ...
|
51 |
self._q.add(hello)
|
52 |
|
53 |
def _close(self): |
54 |
raise NotImplementedError |
55 |
|
56 |
def connect(self): |
57 |
raise NotImplementedError |
58 |
|
59 |
def send(self, message): |
60 |
if self.ready: |
61 |
self._q.add(message)
|
62 |
else:
|
63 |
raise SessionError('Session not ready') |
64 |
|
65 |
### Thread methods
|
66 |
|
67 |
def run(self): |
68 |
raise NotImplementedError |
69 |
|
70 |
### Listener methods - these are relevant for the initial greeting only
|
71 |
|
72 |
def reply(self, data): |
73 |
id, capabilities = None, None |
74 |
try:
|
75 |
p = Parser() |
76 |
# ...
|
77 |
self._init(id, capabilities) |
78 |
except: # ... |
79 |
pass
|
80 |
finally:
|
81 |
self.remove_listener(self) |
82 |
|
83 |
def error(self, data): |
84 |
self._close()
|
85 |
raise SSHError('Session initialization failed') |
86 |
|
87 |
### Getter methods and properties
|
88 |
|
89 |
def get_capabilities(self, whose): |
90 |
return self._capabilities[whose] |
91 |
|
92 |
ready = property(lambda self: self._connected and self._initialised) |
93 |
|
94 |
id = property(lambda self: self._id) |
95 |
|
96 |
client_capabilities = property(lambda self: self._capabilities[CLIENT]) |
97 |
|
98 |
server_capabilities = property(lambda self: self._capabilities[SERVER]) |