root / ncclient / operations / listener.py @ 8b4b9936
History | View | Annotate | Download (1.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 |
from weakref import WeakValueDictionary |
16 |
|
17 |
from ncclient.content.parsers import RootParser |
18 |
|
19 |
_listeners = WeakValueDictionary() |
20 |
|
21 |
def get_listener(session): |
22 |
try:
|
23 |
return _listeners[session]
|
24 |
except KeyError: |
25 |
_listeners[session] = MessageListener() |
26 |
return _listeners[session]
|
27 |
|
28 |
class MessageListener: |
29 |
|
30 |
def __init__(self): |
31 |
# {message-id: RPC}
|
32 |
self._rpc = WeakValueDictionary()
|
33 |
# if the session gets closed by remote endpoint,
|
34 |
# need to know if it is an error event or was requested through
|
35 |
# a NETCONF operation i.e. CloseSession
|
36 |
self._expecting_close = False |
37 |
# other recognized names and behavior on receiving them
|
38 |
self._recognized = []
|
39 |
|
40 |
def __str__(self): |
41 |
return 'MessageListener' |
42 |
|
43 |
def expect_close(self): |
44 |
self._expecting_close = True |
45 |
|
46 |
def register(self, id, op): |
47 |
self._id2rpc[id] = op |
48 |
|
49 |
### Events
|
50 |
|
51 |
def reply(self, raw): |
52 |
pass
|
53 |
|
54 |
def error(self, err): |
55 |
from ncclient.session.session import SessionCloseError |
56 |
if err is SessionCloseError: |
57 |
logger.debug('session closed by remote endpoint, expecting_close=%s' %
|
58 |
self._expecting_close)
|
59 |
if not self._expecting_close: |
60 |
raise err
|