root / ncclient / rpc.py @ efc6b445
History | View | Annotate | Download (1.6 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 SessionListener |
18 |
|
19 |
class RPC: |
20 |
|
21 |
current_id = {} |
22 |
listeners = {} |
23 |
|
24 |
def __init__(self, session=None, async=False): |
25 |
self._session = None |
26 |
self._async = None |
27 |
self._reply = None |
28 |
self._event = Event()
|
29 |
|
30 |
def get_reply(self, timeout=2.0): |
31 |
self._event.wait(timeout)
|
32 |
if self._event.isSet(): |
33 |
return self._reply |
34 |
|
35 |
def do(self, async=False): |
36 |
self._async = async
|
37 |
|
38 |
def _deliver(self, reply): |
39 |
self._reply = reply
|
40 |
self._event.set()
|
41 |
|
42 |
@property
|
43 |
def has_reply(self): |
44 |
return self._event.isSet() |
45 |
|
46 |
@property
|
47 |
def is_async(self): |
48 |
return self._async |
49 |
|
50 |
@property
|
51 |
def listener(self): |
52 |
if RPC.listeners[self._sid] is None: |
53 |
RPC.listeners[self.sid] = SessionListener()
|
54 |
return RPC.listeners[self._sid] |
55 |
|
56 |
def _next_id(self): |
57 |
RPC.current_id[self._session.id] = RPC.current_id.get(self._session.id, 0) + 1 |
58 |
return RPC.current_id[self._sid] |