Statistics
| Branch: | Tag: | Revision:

root / ncclient / rpc.py @ 589b23e4

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 threading import Event
16

    
17
from listener import Listener
18

    
19
from content import MessageIDParser
20

    
21
class RPC:
22
    
23
    cur_id = {}
24

    
25
    def __init__(self, session=None, async=False):
26
        self._session = None
27
        self._async = None
28
        self._reply = None
29
        self._event = Event()
30
    
31
    def get_reply(self, timeout=2.0):
32
        self._event.wait(timeout)
33
        if self._event.isSet():
34
            return self._reply
35
    
36
    def do(self, session, async=False):
37
        self._async = async
38
    
39
    def deliver(self, reply):
40
        self._reply = reply
41
        self._event.set()
42

    
43
    @property
44
    def has_reply(self): return self._event.isSet()
45
    
46
    @property
47
    def async(self): return self._async
48
    
49
    @property
50
    def listener(self): return self._listener
51
    
52
    def _next_id(self):
53
        cur_id[self._sid] = cur_id.get(self._sid, 0) + 1
54
        return cur_id[self._sid]
55
    
56
class RPCReply:
57
    
58
    def __init__(self, raw):
59
        self._raw = raw
60
    
61
    def get_id(self):
62
        return content.rpc.parse_msg_id(raw)
63

    
64
class RPCError(NETCONFError):
65
    
66
    pass
67

    
68
class ReplyListener(Listener):
69
    
70
    def __init__(self):
71
        self._id2rpc = {}
72
    
73
    def reply(self, msg):
74
        reply = RPCReply(msg)
75
        id2rpc[reply.get_id()].deliver(reply)
76
    
77
    def error(self, buf):
78
        pass