Revision 1d540e60 ncclient/glue.py

b/ncclient/glue.py
15 15
"TODO: docstring"
16 16

  
17 17
from cStringIO import StringIO
18
from threading import Thread
18 19
from Queue import Queue
19 20
from threading import Lock
20 21
from xml.etree import cElementTree as ET
21 22

  
23
import logging
24
logger = logging.getLogger('ncclient.glue')
22 25

  
23 26
def parse_root(raw):
24 27
    '''Parse the top-level element from a string representing an XML document.
......
32 35
        return (element.tag, element.attrib)
33 36

  
34 37

  
35
class Subject(object):
38
class Subject(Thread):
36 39
    
37 40
    'Meant for subclassing by transport.Session'
38 41

  
39 42
    def __init__(self):
40 43
        "TODO: docstring"
44
        Thread.__init__(self)
41 45
        self._q = Queue()
42
        self._listeners = set()
46
        self._listeners = set() # TODO(?) weakref
43 47
        self._lock = Lock()
44 48
    
45 49
    def _dispatch_received(self, raw):
......
48 52
        with self._lock:
49 53
            listeners = list(self._listeners)
50 54
        for l in listeners:
55
            logger.debug('[dispatching] message to %s' % l)
51 56
            l.callback(root, raw)
52 57
    
53 58
    def _dispatch_error(self, err):
......
55 60
        with self._lock:
56 61
            listeners = list(self._listeners)
57 62
        for l in listeners:
63
            logger.debug('[dispatching] error to %s' % l)
58 64
            l.errback(err)
59 65
    
60 66
    def add_listener(self, listener):
61 67
        "TODO: docstring"
68
        logger.debug('[installing listener] %r' % listener)
62 69
        with self._lock:
63 70
            self._listeners.add(listener)
64 71
    
65 72
    def remove_listener(self, listener):
66 73
        "TODO: docstring"
74
        logger.debug('[discarding listener] %r' % listener)
67 75
        with self._lock:
68 76
            self._listeners.discard(listener)
69 77
    
......
78 86
    
79 87
    def send(self, message):
80 88
        "TODO: docstring"
81
        logger.debug('queueing:%s' % message)
89
        logger.debug('[queueing] %s' % message)
82 90
        self._q.put(message)
83 91

  
84 92

  

Also available in: Unified diff