forked from marianobarrios/tls-channel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsynchronousChannelServer.java
96 lines (81 loc) · 3.88 KB
/
AsynchronousChannelServer.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
package tlschannel.example;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLContext;
import tlschannel.ServerTlsChannel;
import tlschannel.TlsChannel;
import tlschannel.async.AsynchronousTlsChannel;
import tlschannel.async.AsynchronousTlsChannelGroup;
/**
* Server asynchronous example. Accepts any number of connections and echos bytes sent by the
* clients into standard output.
*
* <p>To test, use: <code> openssl s_client -connect localhost:10000 </code>
*
* <p>This class exemplifies the use of {@link AsynchronousTlsChannel}. It implements a blocking
* select loop, that processes new connections asynchronously using asynchronous channel and
* callbacks, hiding the complexity of a select loop.
*/
public class AsynchronousChannelServer {
private static final Charset utf8 = StandardCharsets.UTF_8;
public static void main(String[] args) throws IOException, GeneralSecurityException {
// initialize the SSLContext, a configuration holder, reusable object
SSLContext sslContext = ContextFactory.authenticatedContext("TLSv1.2");
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
// connect server socket channel and register it in the selector
try (ServerSocketChannel serverSocket = ServerSocketChannel.open()) {
serverSocket.socket().bind(new InetSocketAddress(10000));
// accept raw connections normally
System.out.println("Waiting for connection...");
while (true) {
SocketChannel rawChannel = serverSocket.accept();
rawChannel.configureBlocking(false);
// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal
// options
ServerTlsChannel.Builder builder = ServerTlsChannel.newBuilder(rawChannel, sslContext);
// instantiate TlsChannel
TlsChannel tlsChannel = builder.build();
// build asynchronous channel, based in the TLS channel and associated with the global
// group.
AsynchronousTlsChannel asyncTlsChannel =
new AsynchronousTlsChannel(channelGroup, tlsChannel, rawChannel);
// write to stdout all data sent by the client
ByteBuffer res = ByteBuffer.allocate(10000);
asyncTlsChannel.read(res, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
if (result != -1) {
res.flip();
System.out.print(utf8.decode(res).toString());
res.compact();
// repeat
asyncTlsChannel.read(res, null, this);
} else {
try {
asyncTlsChannel.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void failed(Throwable exc, Object attachment) {
try {
asyncTlsChannel.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
throw new RuntimeException(exc);
}
});
}
}
}
}