Statistics
| Branch: | Tag: | Revision:

root / ncclient / rpc.py @ 38a9b062

History | View | Annotate | Download (1.7 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

    
17
from listener import SessionListener
18

    
19
from uuid import uuid1
20

    
21
class RPC:
22
    
23
    def __init__(self, session, async=False):
24
        self._session = session
25
        self._async = async
26
        self._reply = None
27
        self._reply_event = Event()
28
        self._id = uuid1().urn
29

    
30
    def _listener(self):
31
        if not RPC.listeners.has_key(self._session.id):
32
            RPC.listeners[self._session.id] = SessionListener()
33
        return RPC.listeners[self._session.id]
34

    
35
    def request(self, async=False):
36
        self._async = async
37
        listener = SessionListener(self._session.id)
38
        session.add_listener(listener)
39
        listener.register(self._id, self)
40
        self._session.send(self.to_xml())
41
    
42
    def response_cb(self, reply):
43
        self._reply = reply # does callback parse??
44
        self._event.set()
45
    
46
    @property
47
    def has_reply(self):
48
        return self._reply_event.isSet()
49
    
50
    def wait_on_reply(self, timeout=None):
51
        self._reply_event.wait(timeout)
52
    
53
    @property
54
    def is_async(self):
55
        return self._async
56
    
57
    @property
58
    def id(self):
59
        return self._id