Revision 10d68bc5
b/src/operations.py | ||
---|---|---|
12 | 12 |
# See the License for the specific language governing permissions and |
13 | 13 |
# limitations under the License. |
14 | 14 |
|
15 |
from listener import Listener |
|
16 | 15 |
|
17 | 16 |
from threading import Event |
18 | 17 |
|
19 | 18 |
class OperationError(NETCONFError): pass |
20 | 19 |
|
21 |
class RPC: |
|
22 |
pass |
|
23 |
|
|
24 |
class RPCRequest(RPC): |
|
25 |
|
|
26 |
cur_msg_id = {} |
|
27 |
|
|
28 |
@cls |
|
29 |
def next_id(cls, session_id): |
|
30 |
cur_msg_id[session_id] = cur_msg_id.get(session_id, 0) + 1 |
|
31 |
return cur_msg_id[session_id] |
|
32 |
|
|
33 |
def __init__(self): |
|
34 |
self._reply = None |
|
35 |
self._event = Event() |
|
36 |
self._async = None |
|
37 |
|
|
38 |
def get_reply(self, timeout=2.0): |
|
39 |
self._event.wait(timeout) |
|
40 |
if self._event.isSet(): |
|
41 |
return self._reply |
|
42 |
|
|
43 |
def do(self, session, async=False): |
|
44 |
self._async = async |
|
45 |
|
|
46 |
@property |
|
47 |
def async(self): |
|
48 |
return self._async |
|
49 |
|
|
50 |
class RPCReply(RPC): |
|
51 |
pass |
|
52 |
|
|
53 |
class RPCError(OperationError): |
|
54 |
pass |
|
55 | 20 |
|
56 | 21 |
class Operation(RPCRequest): |
57 | 22 |
pass |
58 | 23 |
|
59 |
class ReplyListener(Listener): |
|
60 |
|
|
61 |
def __init__(self): |
|
62 |
self._id2op = {} |
|
63 |
|
|
64 |
def reply(self, msg): |
|
65 |
# if all good: |
|
66 |
op = id2op[id] |
|
67 |
op._reply = parsed_msg |
|
68 |
# else: |
|
69 |
self._error = True |
|
70 |
|
|
71 |
op._event.set() |
|
72 |
pass |
|
73 |
|
|
74 |
def error(self, buf): |
|
75 |
pass |
b/src/rpc.py | ||
---|---|---|
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 listener import Listener |
|
16 |
|
|
17 |
class RPC: |
|
18 |
pass |
|
19 |
|
|
20 |
class RPCRequest(RPC): |
|
21 |
|
|
22 |
cur_msg_id = {} |
|
23 |
|
|
24 |
@cls |
|
25 |
def next_id(cls, session_id): |
|
26 |
cur_msg_id[session_id] = cur_msg_id.get(session_id, 0) + 1 |
|
27 |
return cur_msg_id[session_id] |
|
28 |
|
|
29 |
def __init__(self): |
|
30 |
self._reply = None |
|
31 |
self._event = Event() |
|
32 |
self._async = None |
|
33 |
|
|
34 |
def get_reply(self, timeout=2.0): |
|
35 |
self._event.wait(timeout) |
|
36 |
if self._event.isSet(): |
|
37 |
return self._reply |
|
38 |
|
|
39 |
def do(self, session, async=False): |
|
40 |
self._async = async |
|
41 |
|
|
42 |
@property |
|
43 |
def async(self): |
|
44 |
return self._async |
|
45 |
|
|
46 |
class RPCReply(RPC): |
|
47 |
pass |
|
48 |
|
|
49 |
class RPCError(OperationError): |
|
50 |
pass |
|
51 |
|
|
52 |
class ReplyListener(Listener): |
|
53 |
|
|
54 |
def __init__(self): |
|
55 |
self._id2rpc = {} |
|
56 |
|
|
57 |
def reply(self, msg): |
|
58 |
# if all good: |
|
59 |
op = id2op[id] |
|
60 |
op._reply = parsed_msg |
|
61 |
# else: |
|
62 |
self._error = True |
|
63 |
|
|
64 |
op._event.set() |
|
65 |
pass |
|
66 |
|
|
67 |
def error(self, buf): |
|
68 |
pass |
|
69 |
|
Also available in: Unified diff