root / src / rpc.py @ 10d68bc5
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 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 |
|