Statistics
| Branch: | Tag: | Revision:

root / ncclient / rpc.py @ ee4bb099

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, Lock
16
from uuid import uuid1
17

    
18
import content
19
from listeners import session_listener_factory
20

    
21
class RPC:
22
    
23
    def __init__(self, session, async=False, parse=True):
24
        self._session = session
25
        self._async = async
26
        self._id = uuid1().urn
27
        self._listener = session_listener_factory(self._session)
28
        listener.register(self._id, self)
29
        session.add_listener(self._listener)
30
        self._reply = None
31
        self._reply_event = Event()
32

    
33
    def _response_cb(self, reply):
34
        self._reply = reply
35
        self._event.set()
36
    
37
    def _do_request(self, op):
38
        self._session.send(content.make_rpc(self._id, op))
39
        if not self._async:
40
            self._reply_event.wait()
41
        return self._reply
42
    
43
    def request(self):
44
        raise NotImplementedError
45
    
46
    def wait_for_reply(self, timeout=None):
47
        self._reply_event.wait(timeout)
48
    
49
    @property
50
    def has_reply(self):
51
        return self._reply_event.isSet()
52
    
53
    @property
54
    def is_async(self):
55
        return self._async
56
    
57
    @property
58
    def reply(self):
59
        return self._reply
60
    
61
    @property
62
    def id(self):
63
        return self._id
64
    
65
    @property
66
    def session(self):
67
        return self._session
68

    
69

    
70
class RPCReply:
71
    pass
72

    
73
class RPCError:
74
    pass