Statistics
| Branch: | Tag: | Revision:

root / ncclient / transport / session.py @ 94803aaf

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 threading import Event
16

    
17
from ncclient.capabilities import Capabilities, CAPABILITIES
18
from ncclient.glue import Subject
19

    
20
from hello import HelloHandler
21

    
22
import logging
23
logger = logging.getLogger('ncclient.transport.session')
24

    
25
class Session(Subject):
26
    
27
    "TODO: docstring"
28
    
29
    def __init__(self):
30
        "Subclass constructor should call this"
31
        Subject.__init__(self)
32
        self.setName('session')
33
        self._client_capabilities = CAPABILITIES
34
        self._server_capabilities = None # yet
35
        self._id = None # session-id
36
        self._connected = False # to be set/cleared by subclass implementation
37
        logger.debug('%r created: client_capabilities=%r' %
38
                     (self, self._client_capabilities))
39
    
40
    def _post_connect(self):
41
        "Greeting stuff"
42
        init_event = Event()
43
        error = [None] # so that err_cb can bind error[0]. just how it is.
44
        # callbacks
45
        def ok_cb(id, capabilities):
46
            self._id = id
47
            self._server_capabilities = Capabilities(capabilities)
48
            init_event.set()
49
        def err_cb(err):
50
            error[0] = err
51
            init_event.set()
52
        listener = HelloHandler(ok_cb, err_cb)
53
        self.add_listener(listener)
54
        self.send(HelloHandler.build(self._client_capabilities))
55
        logger.debug('starting main loop')
56
        self.start()
57
        # we expect server's hello message
58
        init_event.wait()
59
        # received hello message or an error happened
60
        self.remove_listener(listener)
61
        if error[0]:
62
            raise error[0]
63
        logger.info('initialized: session-id=%s | server_capabilities=%s' %
64
                     (self._id, self._server_capabilities))
65
    
66
    def connect(self, *args, **kwds):
67
        "Subclass implements"
68
        raise NotImplementedError
69

    
70
    def run(self):
71
        "Subclass implements"
72
        raise NotImplementedError
73
    
74
    ### Properties
75
    
76
    @property
77
    def client_capabilities(self):
78
        return self._client_capabilities
79
    
80
    @property
81
    def server_capabilities(self):
82
        return self._server_capabilities
83
    
84
    @property
85
    def connected(self):
86
        return self._connected
87
    
88
    @property
89
    def id(self):
90
        return self._id
91
    
92
    @property
93
    def can_pipeline(self):
94
        return True