/
ClientTestRestServer.java
275 lines (227 loc) · 7.38 KB
/
ClientTestRestServer.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.ignite.client;
import org.apache.ignite.*;
import org.apache.ignite.internal.client.marshaller.*;
import org.apache.ignite.internal.client.marshaller.optimized.*;
import org.apache.ignite.internal.processors.rest.client.message.*;
import org.apache.ignite.internal.processors.rest.protocols.tcp.*;
import org.apache.ignite.internal.util.nio.*;
import org.jetbrains.annotations.*;
import java.net.*;
import java.nio.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
/**
*
*/
public class ClientTestRestServer {
/** */
public static final int FIRST_SERVER_PORT = 11000;
/** */
public static final int SERVERS_CNT = 5;
/** */
private static final byte[] EMPTY_SES_TOKEN = new byte[] {};
/** */
private static final Collection<GridClientNodeBean> top = new ArrayList<>();
/**
*
*/
static {
for (int port = FIRST_SERVER_PORT; port < FIRST_SERVER_PORT + SERVERS_CNT; port++) {
GridClientNodeBean node = new GridClientNodeBean();
node.setNodeId(UUID.randomUUID());
node.setConsistentId("127.0.0.1:" + port);
node.setTcpPort(port);
node.setTcpAddresses(Arrays.asList("127.0.0.1"));
top.add(node);
}
}
/** */
private final int port;
/** */
private volatile boolean failOnConnect;
/** */
private final IgniteLogger log;
/** */
private final AtomicInteger connCnt = new AtomicInteger();
/** */
private final AtomicInteger succConnCnt = new AtomicInteger();
/** */
private final AtomicInteger disconnCnt = new AtomicInteger();
/** */
private GridNioServer<GridClientMessage> srv;
/** */
private volatile GridNioSession lastSes;
/**
* @param port Port to listen on.
* @param failOnConnect If {@code true} than server will close connection immediately after connect.
* @param log Log.
*/
public ClientTestRestServer(int port, boolean failOnConnect, IgniteLogger log) {
this.port = port;
this.failOnConnect = failOnConnect;
this.log = log;
}
/**
* @return Port number.
*/
public int getPort() {
return port;
}
/**
* Starts the server.
*
* @throws IgniteCheckedException If failed.
*/
public void start() throws IgniteCheckedException {
try {
String gridName = "test";
srv = GridNioServer.<GridClientMessage>builder()
.address(InetAddress.getByName("127.0.0.1"))
.port(port)
.listener(new TestListener())
.logger(log)
.selectorCount(2)
.gridName(gridName)
.byteOrder(ByteOrder.nativeOrder())
.tcpNoDelay(true)
.directBuffer(false)
.filters(
new GridNioAsyncNotifyFilter(gridName, Executors.newFixedThreadPool(2), log),
new GridNioCodecFilter(new TestParser(), log, false)
)
.build();
}
catch (UnknownHostException e) {
throw new IgniteCheckedException("Failed to determine localhost address.", e);
}
srv.start();
}
/**
* Stops the server.
*/
public void stop() {
assert srv != null;
srv.stop();
}
/**
* @return Number of connections opened to this server.
*/
public int getConnectCount() {
return connCnt.get();
}
/**
* @return Number of successful connections opened to this server.
*/
public int getSuccessfulConnectCount() {
return succConnCnt.get();
}
/**
* @return Number of connections with this server closed by clients.
*/
public int getDisconnectCount() {
return disconnCnt.get();
}
/**
* Closes all opened connections.
*/
public void fail() {
assert lastSes != null;
lastSes.close();
failOnConnect = true;
resetCounters();
}
/**
*
*/
public void repair() {
failOnConnect = false;
}
/**
* Resets all counters.
*/
public void resetCounters() {
connCnt.set(0);
succConnCnt.set(0);
disconnCnt.set(0);
}
/**
* Prepares response stub.
* @param msg Mesage to respond to.
* @return Response.
*/
private static GridClientResponse makeResponseFor(GridClientMessage msg) {
GridClientResponse res = new GridClientResponse();
res.clientId(msg.clientId());
res.requestId(msg.requestId());
res.successStatus(GridClientResponse.STATUS_SUCCESS);
res.sessionToken(EMPTY_SES_TOKEN);
return res;
}
/**
* Test listener.
*/
private class TestListener extends GridNioServerListenerAdapter<GridClientMessage> {
/** {@inheritDoc} */
@Override public void onConnected(GridNioSession ses) {
lastSes = ses;
connCnt.incrementAndGet();
if (failOnConnect)
ses.close();
else
succConnCnt.incrementAndGet();
}
/** {@inheritDoc} */
@Override public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
disconnCnt.incrementAndGet();
}
/** {@inheritDoc} */
@Override public void onMessage(GridNioSession ses, GridClientMessage msg) {
if (msg == GridClientPingPacket.PING_MESSAGE)
ses.send(GridClientPingPacket.PING_MESSAGE);
else if (msg instanceof GridClientAuthenticationRequest)
ses.send(makeResponseFor(msg));
else if (msg instanceof GridClientTopologyRequest) {
GridClientResponse res = makeResponseFor(msg);
res.result(top);
ses.send(res);
}
else if (msg instanceof GridClientHandshakeRequest)
ses.send(GridClientHandshakeResponse.OK);
}
/** {@inheritDoc} */
@Override public void onSessionWriteTimeout(GridNioSession ses) {
ses.close();
}
/** {@inheritDoc} */
@Override public void onSessionIdleTimeout(GridNioSession ses) {
ses.close();
}
}
/**
*/
private static class TestParser extends GridTcpRestParser {
/** */
private final GridClientMarshaller marsh = new GridClientOptimizedMarshaller();
/** {@inheritDoc} */
@Override protected GridClientMarshaller marshaller(GridNioSession ses) {
return marsh;
}
}
}