Statistics
| Branch: | Tag: | Revision:

root / ncclient / listeners.py @ 2acc860a

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
import logging
16
from weakref import WeakValueDictionary
17

    
18
import content
19

    
20
logger = logging.getLogger('ncclient.listeners')
21

    
22
session_listeners = {}
23
def session_listener_factory(session):
24
    try:
25
        return session_listeners[session]
26
    except KeyError:
27
        session_listeners[session] = SessionListener()
28
        return session_listeners[session]
29

    
30
class SessionListener(object):
31
    
32
    def __init__(self):
33
        self._id2rpc = WeakValueDictionary()
34
        self._expecting_close = False
35
        sself._notification_rpc_id = None
36
    
37
    def __str__(self):
38
        return 'SessionListener'
39
    
40
    
41
    
42
    def expect_close(self):
43
        self._expecting_close = True
44
    
45
    def register(self, id, op):
46
        self._id2rpc[id] = op
47
    
48
    ### Events
49
    
50
    def reply(self, raw):
51
        try:
52
            id = content.parse_message_root(raw)
53
            if id is None:
54
                pass
55
            elif id == 'notification':
56
                self._id2rpc[self._sub_id]._notify(raw)
57
            else:
58
                self._id2rpc[id]._response_cb(raw)
59
        except Exception as e:
60
            logger.warning(e)
61
    
62
    def error(self, err):
63
        from ssh import SessionCloseError
64
        if err is SessionCloseError:
65
            logger.debug('received session close, expecting_close=%s' %
66
                         self._expecting_close)
67
            if not self._expecting_close:
68
                raise err
69

    
70

    
71
class HelloListener:
72
    
73
    def __str__(self):
74
        return 'HelloListener'
75
        
76
    def __init__(self, session):
77
        self._session = session
78
    
79
    def reply(self, data):
80
        try:
81
            id, capabilities = content.Hello.parse(data)
82
            logger.debug('HelloListener: session_id: %s; capabilities: %s', id, capabilities)
83
            self._session.initialize(id, capabilities)
84
        except Exception as e:
85
            self._session.initialize_error(e)
86
    
87
    def error(self, err):
88
        self._session.initialize_error(err)
89

    
90

    
91
class DebugListener:
92
    
93
    def __str__(self):
94
        return 'DebugListener'
95
    
96
    def reply(self, raw):
97
        logger.debug('DebugListener:reply:\n%s' % raw)
98
    
99
    def error(self, err):
100
        logger.debug('DebugListener:error:\n%s' % err)