Repository navigation
Expand file tree
/
Copy pathRpcServer.java
More file actions
153 lines (135 loc) · 4.6 KB
/
Copy pathRpcServer.java
File metadata and controls
153 lines (135 loc) · 4.6 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package rpc.framework.server;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import rpc.exception.RpcException;
import rpc.exception.RpcInternalError;
import rpc.exception.RpcInvalidRquest;
import rpc.framework.INotificationListener;
import rpc.framework.RpcInvoker;
import rpc.framework.RpcServerInvokerHandler;
import rpc.framework.connection.IRpcChannel;
import rpc.framework.connection.RpcConnector;
import rpc.json.message.RpcErrorResponse;
import rpc.json.message.RpcNotification;
import rpc.json.message.RpcRequest;
import rpc.json.message.RpcResponse;
import rpc.util.RpcLog;
public class RpcServer implements INotificationListener{
private static final String TAG = "RpcServer";
private boolean mIsRunning = false;
private RpcServerInvokerHandler mHandler = null;
ServiceRegistry mServiceRegister = new ServiceRegistry();
ExecutorService mThreadPool = Executors.newCachedThreadPool();
class RequestProcessor implements Callable<RpcResponse> {
private RpcServiceInterface mService = null;
private RpcRequest mRequest = null;
public RequestProcessor(RpcServiceInterface service,
RpcRequest request) {
super();
this.mService = service;
this.mRequest = request;
}
@Override
public RpcResponse call() throws Exception {
if (mService == null) { return null; } // FIXME:: if properly
RpcLog.d(TAG, "RequestProcessor is callings");
try {
if (mRequest.getMethod() == null || mRequest.getMethod().isEmpty()) {
throw new RpcInvalidRquest("method is empty in request of " + mRequest.toString());
}
return mService.execute(mRequest);
} catch (RpcException e) {
return new RpcErrorResponse(mRequest.getId(), e.toError());
}
}
}
/**注册服务
* @param service
*/
public void registerService(RpcServiceInterface service) {
mServiceRegister.addService(service);
}
public void registerSubject(String servicename, Object subject) {
RpcSubscriber.BaseRpcSubscriber.registerSubject(servicename, subject);
}
/** 启动服务程序,会一直阻塞
* @param ip
* @param port
*/
public void serve(String ip, int port) {
while(true) {
IRpcChannel channel = RpcConnector.acceptChannel(ip, port);
if (channel == null) {
RpcLog.e(TAG, "aquire rpc channel failed");
continue;
}
mIsRunning = true;
mHandler = new RpcServerInvokerHandler();
mHandler.bindChannel(channel);
// TODO:: 做一些与回调相关的处理
RpcSubscriber s = null;
try {
s = (RpcSubscriber)RpcSubscriber.BaseRpcSubscriber.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return;
}
s.onConnect(this);
mServiceRegister.removeService(s.list());
mServiceRegister.addService(s);
RpcLog.i(TAG, mServiceRegister.listServices());
run();
s.onDisconnect();
}
}
public boolean onNotify(RpcNotification message) {
if (mIsRunning) {
if (message == null) { return false; }
try {
RpcLog.i(TAG, "notify message");
mHandler.invoke(new RpcInvoker(message, null, null));
return true;
} catch (InterruptedException e) {
RpcLog.e(RpcServer.TAG, "server is stopped because of exception:" + e);
}
}
return false;
}
public void run() {
mHandler.startup();
while (mIsRunning) {
try {
RpcInvoker invoker = mHandler.retrieve();
RpcRequest request = (RpcRequest)invoker.getRequest();
RpcServiceInterface service = (RpcServiceInterface)mServiceRegister.getService(request.getMethod());
// RpcLog.d(TAG, "ask request: " + request.toString());
RpcResponse response = null;
if (service == null) {
RpcLog.e(TAG, "NO service can handle this method:" + request.getMethod());
response = null;
} else {
try {
response = mThreadPool.submit(new RequestProcessor(service, request)).get();
} catch (ExecutionException | InterruptedException | CancellationException e) {
RpcLog.e(TAG, e);
response = new RpcResponse(request.getId(), (new RpcInternalError(e.toString())).toError(), false);
}
}
mHandler.invoke(new RpcInvoker(response, null, null));
} catch (InterruptedException e) {
RpcLog.e(RpcServer.TAG, "server is stopped because of exception:" + e);
RpcServer.this.stop();
}
}
onDestroy();
}
public void stop() {
mIsRunning = false;
}
public void onDestroy() {
mHandler.shutdown();
}
}