Revision 8b4b9936 ncclient/operations/rpc.py

b/ncclient/operations/rpc.py
17 17
from threading import Event, Lock
18 18
from uuid import uuid1
19 19

  
20
_listeners = WeakValueDictionary()
21

  
22
def get_listener(session):
23
    try:
24
        return _listeners[session]
25
    except KeyError:
26
        _listeners[session] = MessageListener()
27
        return _listeners[session]
20
from listener import get_listener
21
from ncclient.content.builders import RPCBuilder
28 22

  
29 23
class RPC:
30 24
    
......
36 30
        self._reply_event = Event()
37 31
        self.listener.register(self._id, self)
38 32
        session.add_listener(self.listener)
39

  
33
    
40 34
    def _response_cb(self, reply):
41 35
        self._reply = reply
42 36
        self._event.set()
43 37
    
44
    def _do_request(self, operation):
45
        'operation is xml string'
46
        self._session.send(content.RPC.make(self._id, operation))
38
    def _do_request(self, op):
39
        self._session.send(RPCBuilder.build(self._id, op))
47 40
        if not self._async:
48 41
            self._reply_event.wait()
49 42
        return self._reply
......
85 78
        pass
86 79
    
87 80

  
88
class MessageListener:
89
    
90
    def __init__(self):
91
        # {message-id: RPC}
92
        self._rpc = WeakValueDictionary()
93
        # if the session gets closed by remote endpoint,
94
        # need to know if it is an error event or was requested through
95
        # a NETCONF operation i.e. CloseSession
96
        self._expecting_close = False
97
        # other recognized names and behavior on receiving them
98
        self._recognized = []
99
    
100
    def __str__(self):
101
        return 'MessageListener'
102
    
103
    def expect_close(self):
104
        self._expecting_close = True
105
    
106
    def register(self, id, op):
107
        self._id2rpc[id] = op
108
    
109
    ### Events
110
    
111
    def reply(self, raw):
112
        pass
113
    
114
    def error(self, err):
115
        from ncclient.session.session import SessionCloseError
116
        if err is SessionCloseError:
117
            logger.debug('session closed by remote endpoint, expecting_close=%s' %
118
                         self._expecting_close)
119
            if not self._expecting_close:
120
                raise err
121

  

Also available in: Unified diff