-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcInvoker.java
More file actions
77 lines (65 loc) · 2 KB
/
Copy pathRpcInvoker.java
File metadata and controls
77 lines (65 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package rpc.framework;
import rpc.component.RpcComponent;
import rpc.framework.message.RpcMessage;
public class RpcInvoker {
private int Id = 0;
private RpcMessage request = null;
private RpcMessage response = null;
private IResultListener resultlistener = null;
private Object mResultLocker = new Object();
public RpcInvoker(RpcMessage request, RpcComponent component, IResultListener listener) {
Id = request!=null ? request.getId() : -1;
this.request = request;
resultlistener = listener;
}
/**
*
* @param timeout
* @return false 说明是超时了
* @throws InterruptedException
*/
public boolean waitingResponse(int timeout) throws InterruptedException {
synchronized (mResultLocker) {
long start = System.currentTimeMillis ();
while (response == null) {
mResultLocker.wait(timeout);
if (System.currentTimeMillis() - start >= timeout) {
// 如果超时,抛出TimeoutException异常
// FIXME: 要不要禁掉listener
resultlistener = null; // 防止后面又收到回调,引起混乱
return false;
}
}
}
return true;
}
public void notifyResponse(RpcMessage response) {
synchronized (mResultLocker) {
mResultLocker.notify();
this.response = response;
}
if (resultlistener!=null) {
// FIXME:: 为了兼容过去的框架,这里统一不处理,由客户端的setResponse统一处理。
//resultlistener.onResult(response);
}
}
public RpcMessage getRequest() {
return request;
}
public IResultListener getListener() {
return resultlistener;
}
public boolean needWaitingResponse() {
//return resultlistener != null;
// FIXME:暂时总是异步,这是为了兼容过去的代码框架,每次收到服务器的回复后,都必须调用测一次setResponse。
return true;
}
public RpcMessage getResponse() {
synchronized (mResultLocker) {
return response;
}
}
public Integer getId() {
return Id;
}
}