Statistics
| Branch: | Tag: | Revision:

root / ncclient / session.py @ 8024f8b5

History | View | Annotate | Download (2.9 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 listeners import HelloListener
23
from subject import Subject
24

    
25
logger = logging.getLogger('ncclient.session')
26

    
27
class SessionError(ClientError): pass
28

    
29
class Session(Thread, Subject):
30
    
31
    def __init__(self):
32
        Thread.__init__(self, name='session')
33
        Subject.__init__(self)
34
        self._client_capabilities = CAPABILITIES
35
        self._server_capabilities = None # yet
36
        self._id = None # session-id
37
        self._error = None
38
        self._init_event = Event()
39
        self._q = Queue()
40
        self._connected = False # to be set/cleared by subclass implementation
41
    
42
    def _post_connect(self):
43
        # start the subclass' main loop
44
        listener = HelloListener(self)
45
        self.add_listener(listener)
46
        self.start()
47
        # queue client's hello message for sending
48
        self.send(content.Hello.build(self._client_capabilities))
49
        # we expect server's hello message, wait for _init_event to be set
50
        self._init_event.wait()
51
        self.remove_listener(listener)
52
        # there may have been an error
53
        if self._error:
54
            self._close()
55
            raise self._error
56
    
57
    def hello(self, id, capabilities):
58
        self._id, self._capabilities = id, Capabilities(capabilities)
59
        self._init_event.set()
60
    
61
    def hello_error(self, err):
62
        self._error = err
63
        self._init_event.set()
64
    
65
    def send(self, message):
66
        logger.debug('queueing message: \n%s' % message)
67
        self._q.put(message)
68
    
69
    def connect(self):
70
        raise NotImplementedError
71

    
72
    def run(self):
73
        raise NotImplementedError
74
        
75
    def capabilities(self, whose='client'):
76
        if whose == 'client':
77
            return self._client_capabilities
78
        elif whose == 'server':
79
            return self._server_capabilities
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