-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEchoServer.java
More file actions
executable file
·416 lines (356 loc) · 16.2 KB
/
Copy pathEchoServer.java
File metadata and controls
executable file
·416 lines (356 loc) · 16.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS info.picocli:picocli:4.7.3
//DEPS io.netty:netty-all:4.1.100.Final
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import picocli.AutoComplete;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help.Ansi;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@SuppressWarnings("unused")
@Command(name = "echo-server", description = "A server which echos requests to the console.", showDefaultValues = true, subcommands = AutoComplete.GenerateCompletion.class)
public class EchoServer implements Callable<Integer> {
@Option(names = {"-H", "--host"}, defaultValue = "127.0.0.1", description = "The host to listen on.")
private String host;
@SuppressWarnings("unused")
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this help message.")
private boolean usageHelpRequested;
@Option(names = {"-p", "--port"}, description = "The port to listen on.", defaultValue = "8080")
private int port;
@SuppressWarnings("unused")
private static class Transport {
@Option(names = "--http", description = "Starts a HTTP server.")
private boolean http;
@Option(names = "--tcp", description = "Starts a TCP server.")
private boolean tcp;
@Option(names = "--udp", description = "Starts a UDP server.")
private boolean udp;
}
@ArgGroup
private Transport transport;
@Option(names = {"-v", "--verbose"}, description = "Prints verbose output.")
private boolean verbose;
@Spec
private CommandSpec spec;
private final PrintStream stdout;
private final PrintStream stderr;
private Ansi ansi;
private EchoServer(final PrintStream stdout, final PrintStream stderr) {
this.stdout = stdout;
this.stderr = stderr;
}
public static void main(final String... args) {
final CommandLine commandLine = new CommandLine(new EchoServer(System.out, System.err));
commandLine.setOut(new PrintWriter(System.out));
final int exitStatus = commandLine.execute(args);
System.exit(exitStatus);
}
@Override
public Integer call() throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread(stdout::println));
try (Server server = createServer()) {
server.start().whenComplete((s, error) -> {
if (error != null) {
error.printStackTrace(stderr);
System.exit(1);
}
print("@|bold Listening on %s:%d|@%n", s.host(), s.port());
});
Thread.currentThread().join();
}
return 0;
}
private void print() {
stdout.println();
stdout.flush();
}
private void print(final String fmt, final Object... args) {
print(stdout, fmt, args);
}
private void printError(final String fmt, final Object... args) {
print(stderr, "@|red " + fmt + "|@", args);
}
private void print(final PrintStream os, final String fmt, final Object... args) {
os.println(format(fmt, args));
os.flush();
}
private String format(final String fmt, final Object... args) {
if (ansi == null) {
ansi = spec.commandLine().getColorScheme().ansi();
}
return ansiFormat(ansi, String.format(fmt, args));
}
private String ansiFormat(final Ansi ansi, final String value) {
return ansi.string(value);
}
private Server createServer() {
if (transport != null && transport.tcp) {
return new TcpServer();
}
if (transport != null && transport.udp) {
return new UdpServer();
}
return new HttpServer();
}
private abstract class Server implements Runnable, AutoCloseable {
final EventLoopGroup mainGroup = new NioEventLoopGroup(1);
final EventLoopGroup workerGroup = new NioEventLoopGroup();
final Lock writeLock = new ReentrantLock();
volatile ChannelFuture channelFuture;
private final AtomicBoolean running = new AtomicBoolean(false);
protected final CompletableFuture<Server> startFuture;
private Server() {
startFuture = new CompletableFuture<>();
}
void stop() {
running.set(false);
final var channelFuture = this.channelFuture;
channelFuture.channel().closeFuture().syncUninterruptibly();
}
CompletionStage<Server> start() {
if (running.compareAndSet(false, true)) {
ForkJoinPool.commonPool().submit(this);
}
return startFuture;
}
boolean isRunning() {
return running.get();
}
String host() {
return host;
}
int port() {
return port;
}
@Override
public void close() throws Exception {
if (!startFuture.isDone()) {
startFuture.cancel(true);
}
stop();
}
}
private class HttpServer extends Server {
@Override
public void run() {
// Create the child handler
final var childHandler = new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
final var pipeline = ch.pipeline();
pipeline.addLast(new HttpRequestDecoder());
pipeline.addLast(new HttpResponseEncoder());
pipeline.addLast(new HttpObjectAggregator(1024 * 1024 * 10));
// Echo back
pipeline.addLast(new SimpleChannelInboundHandler<>() {
private final ByteArrayOutputStream data = new ByteArrayOutputStream();
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) {
if (msg instanceof HttpRequest) {
if (HttpUtil.is100ContinueExpected((HttpRequest) msg)) {
final var response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER);
ctx.write(response);
}
}
if (msg instanceof HttpContent) {
final var content = (HttpContent) msg;
final var buffer = content.content();
try {
writeLock.lock();
try {
buffer.readBytes(data, buffer.readableBytes());
} finally {
writeLock.unlock();
}
if (msg instanceof LastHttpContent) {
final String responseBody;
writeLock.lock();
try {
responseBody = data.toString(StandardCharsets.UTF_8);
data.reset();
} finally {
writeLock.unlock();
}
final LastHttpContent trailer = (LastHttpContent) msg;
final HttpVersion version;
final HttpHeaders headers;
if (msg instanceof HttpRequest) {
version = ((HttpRequest) msg).protocolVersion();
headers = ((HttpRequest) msg).headers();
} else {
version = HttpVersion.HTTP_1_1;
headers = new DefaultHttpHeaders();
}
if (headers != null) {
headers.forEach(entry -> print("%s: %s", entry.getKey(), entry.getValue()));
}
trailer.trailingHeaders()
.forEach(entry -> print("%s: %s", entry.getKey(), entry.getValue()));
// Write a response back
print(responseBody);
final var response = new DefaultFullHttpResponse(version, HttpResponseStatus.OK, Unpooled.copiedBuffer(responseBody + "\r\n", StandardCharsets.UTF_8));
ctx.writeAndFlush(response)
// Not efficient, but this is for testing only
.addListener(ChannelFutureListener.CLOSE);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace(stderr);
ctx.close();
}
});
}
};
final var bootstrap = new ServerBootstrap();
bootstrap.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(childHandler);
channelFuture = bootstrap.bind(host, port)
.addListener(future -> startFuture.complete(HttpServer.this));
}
}
private class TcpServer extends Server {
@Override
public void run() {
// Create the child handler
final var childHandler = new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
final var pipeline = ch.pipeline();
// Echo back
pipeline.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) {
stdout.print(((ByteBuf) msg).toString(StandardCharsets.UTF_8));
}
ctx.write(msg);
}
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace(stderr);
ctx.close();
}
});
}
};
final var bootstrap = new ServerBootstrap();
bootstrap.group(mainGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(childHandler);
channelFuture = bootstrap.bind(host, port)
.addListener(future -> startFuture.complete(TcpServer.this));
}
}
private class UdpServer extends Server {
@Override
public void run() {
// Create the child handler
final var handler = new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(final DatagramChannel ch) {
final var pipeline = ch.pipeline();
// Echo back
pipeline.addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final DatagramPacket msg) throws Exception {
msg.content().readBytes(stdout, msg.content().readableBytes());
ctx.write(msg);
}
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
ctx.flush();
super.channelReadComplete(ctx);
}
});
}
};
final var bootstrap = new Bootstrap();
bootstrap.group(workerGroup)
.channel(NioDatagramChannel.class)
.handler(handler);
channelFuture = bootstrap.bind(host, port)
.addListener(future -> startFuture.complete(UdpServer.this));
}
}
}