-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathServer.java
More file actions
132 lines (112 loc) · 3.99 KB
/
Server.java
File metadata and controls
132 lines (112 loc) · 3.99 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
//
// MessagePack-RPC for Java
//
// Copyright (C) 2010 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.rpc;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import org.msgpack.rpc.builder.DefaultDispatcherBuilder;
import org.msgpack.rpc.builder.DispatcherBuilder;
import org.msgpack.rpc.reflect.Reflect;
import org.msgpack.type.NilValue;
import org.msgpack.type.Value;
import org.msgpack.rpc.address.IPAddress;
import org.msgpack.rpc.dispatcher.Dispatcher;
import org.msgpack.rpc.dispatcher.MethodDispatcher;
import org.msgpack.rpc.config.ClientConfig;
import org.msgpack.rpc.config.ServerConfig;
import org.msgpack.rpc.config.TcpServerConfig;
import org.msgpack.rpc.transport.ServerTransport;
import org.msgpack.rpc.transport.MessageSendable;
import org.msgpack.rpc.loop.EventLoop;
import org.msgpack.rpc.error.RPCError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Server extends SessionPool {
private final static Logger logger = LoggerFactory.getLogger(Server.class);
private Dispatcher dp;
private ServerTransport stran;
private DispatcherBuilder dispatcherBuilder = new DefaultDispatcherBuilder();
public Server() {
super();
}
public Server(ClientConfig config) {
super(config);
}
public Server(EventLoop loop) {
super(loop);
}
public Server(ClientConfig config, EventLoop loop) {
super(config, loop);
}
public DispatcherBuilder getDispatcherBuilder() {
return dispatcherBuilder;
}
public void setDispatcherBuilder(DispatcherBuilder dispatcherBuilder) {
this.dispatcherBuilder = dispatcherBuilder;
}
public void serve(Dispatcher dp) {
this.dp = dp;
}
public void serve(Object handler) {
this.dp = dispatcherBuilder.build(handler,this.getEventLoop().getMessagePack());
}
public void listen(String host, int port) throws UnknownHostException, IOException {
listen(new TcpServerConfig(new IPAddress(host, port)));
}
public void listen(InetSocketAddress address) throws IOException {
listen(new TcpServerConfig(new IPAddress(address)));
}
public void listen(int port) throws IOException {
listen(new TcpServerConfig(new IPAddress(port)));
}
public void listen(ServerConfig config) throws IOException {
stran = getEventLoop().listenTransport(config, this);
}
public void close() {
if (stran != null) {
stran.close();
}
super.close();
}
public void onRequest(MessageSendable channel, int msgid, String method, Value args) {
Request request = new Request(channel, msgid, method, args);
try {
dp.dispatch(request);
} catch (RPCError e) {
// FIXME
request.sendError(e.getCode(), e);
} catch (Exception e) {
logger.error("Unexpected error occured while calling " + method, e);
// FIXME request.sendError("RemoteError", e.getMessage());
if(e.getMessage() == null)
{
request.sendError("");
}else{
request.sendError(e.getMessage());
}
}
}
public void onNotify(String method, Value args) {
Request request = new Request(method, args);
try {
dp.dispatch(request);
} catch (Exception e) {
// FIXME ignore?
}
}
}