Revision 35ad9d81 ncclient/listener.py
b/ncclient/listener.py | ||
---|---|---|
12 | 12 |
# See the License for the specific language governing permissions and |
13 | 13 |
# limitations under the License. |
14 | 14 |
|
15 |
from threading import Lock
|
|
15 |
from content import rpc
|
|
16 | 16 |
|
17 |
import logging
|
|
17 |
class SessionListener:
|
|
18 | 18 |
|
19 |
logger = logging.getLogger('ncclient.listener') |
|
20 |
|
|
21 |
class Subject: |
|
22 |
|
|
23 |
def __init__(self, listeners=[]): |
|
24 |
self._listeners = listeners |
|
25 |
self._lock = Lock() |
|
26 |
|
|
27 |
def has_listener(self, listener): |
|
28 |
with self._lock: |
|
29 |
return (listener in self._listeners) |
|
19 |
def __init__(self): |
|
20 |
self._id2rpc = {} |
|
21 |
self._subscription_id = None # notifications are delivered to the rpc |
|
22 |
# that created the subscription |
|
30 | 23 |
|
31 |
def add_listener(self, listener): |
|
32 |
with self._lock: |
|
33 |
self._listeners.append(listener) |
|
24 |
def set_subscription(self, id): |
|
25 |
self._subscription = id |
|
34 | 26 |
|
35 |
def remove_listener(self, listener): |
|
36 |
with self._lock: |
|
37 |
try: |
|
38 |
self._listeners.remove(listener) |
|
39 |
except ValueError: |
|
40 |
pass |
|
27 |
def reply(self, raw): |
|
28 |
id, is_notification = rpc.parse(raw) |
|
29 |
if is_notification: |
|
30 |
self._id2rpc[self._subscription_id].event(raw) |
|
31 |
else: |
|
32 |
self._id2rpc[id]._deliver(raw) |
|
33 |
del self._id2rpc[id] |
|
41 | 34 |
|
42 |
def dispatch(self, event, *args, **kwds): |
|
43 |
with self._lock: |
|
44 |
listeners = list(self._listeners) |
|
45 |
for l in listeners: |
|
46 |
logger.debug('dispatching [%s] to [%s]' % (event, l.__class__)) |
|
47 |
try: |
|
48 |
getattr(l, event)(*args, **kwds) |
|
49 |
except Exception as e: |
|
50 |
logger.warning(e) |
|
35 |
def error(self, buf): |
|
36 |
pass |
Also available in: Unified diff