Skip to content

Commit

Permalink
Minor improvements in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Oct 13, 2024
1 parent 8c93022 commit 4b68d5d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 55 deletions.
11 changes: 3 additions & 8 deletions src/test/java/tlschannel/example/AsynchronousChannelServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand All @@ -27,8 +26,6 @@
*/
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
Expand All @@ -48,15 +45,13 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
SocketChannel rawChannel = serverSocket.accept();
rawChannel.configureBlocking(false);

// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal
// options
// 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.
// build asynchronous channel, based in the TLS channel and associated with the global group.
AsynchronousTlsChannel asyncTlsChannel =
new AsynchronousTlsChannel(channelGroup, tlsChannel, rawChannel);

Expand All @@ -67,7 +62,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
public void completed(Integer result, Object attachment) {
if (result != -1) {
res.flip();
System.out.print(utf8.decode(res));
System.out.print(StandardCharsets.UTF_8.decode(res));
res.compact();
// repeat
asyncTlsChannel.read(res, null, this);
Expand Down
19 changes: 3 additions & 16 deletions src/test/java/tlschannel/example/NonBlockingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Iterator;
Expand All @@ -19,10 +18,7 @@
/** Client non-blocking example. Connects to a public TLS reporting service. */
public class NonBlockingClient {

private static final Charset utf8 = StandardCharsets.UTF_8;

public static void main(String[] args) throws IOException, GeneralSecurityException {

ByteBuffer requestBuffer = ByteBuffer.wrap(SimpleBlockingClient.httpLine.getBytes(StandardCharsets.US_ASCII));
ByteBuffer responseBuffer = ByteBuffer.allocate(1000); // use small buffer to cause selector loops
boolean requestSent = false;
Expand All @@ -40,13 +36,11 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
// Note that the raw channel (and not the wrapped one) is registered in the selector
rawChannel.register(selector, SelectionKey.OP_CONNECT);

// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal
// options
// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options
ClientTlsChannel.Builder builder = ClientTlsChannel.newBuilder(rawChannel, sslContext);

// instantiate TlsChannel
TlsChannel tlsChannel = builder.build();
try {
try (TlsChannel tlsChannel = builder.build()) {
mainLoop:
while (true) {

Expand All @@ -55,18 +49,14 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept

Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {

SelectionKey key = iterator.next();
iterator.remove();

if (key.isConnectable()) {

if (rawChannel.finishConnect()) {
// the channel is registered for writing, because TLS connections are initiated by
// clients.
rawChannel.register(selector, SelectionKey.OP_WRITE);
}

} else if (key.isReadable() || key.isWritable()) {
try {
if (!requestSent) {
Expand All @@ -80,7 +70,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
int c = tlsChannel.read(responseBuffer);
if (c > 0) {
responseBuffer.flip();
System.out.print(utf8.decode(responseBuffer));
System.out.print(StandardCharsets.UTF_8.decode(responseBuffer));
responseBuffer.compact();
}
if (c < 0) {
Expand All @@ -93,14 +83,11 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
} catch (NeedsWriteException e) {
key.interestOps(SelectionKey.OP_WRITE); // overwrites previous value
}

} else {
throw new IllegalStateException();
}
}
}
} finally {
tlsChannel.close();
}
}
}
Expand Down
11 changes: 1 addition & 10 deletions src/test/java/tlschannel/example/NonBlockingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.channels.Selector;
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 java.util.Iterator;
Expand Down Expand Up @@ -35,8 +34,6 @@
*/
public class NonBlockingServer {

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
Expand All @@ -48,20 +45,16 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
serverSocket.configureBlocking(false);
Selector selector = Selector.open();
serverSocket.register(selector, SelectionKey.OP_ACCEPT);

while (true) {

// loop blocks here
selector.select();

Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {

SelectionKey key = iterator.next();
iterator.remove();

if (key.isAcceptable()) {

// we have a new connection

ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
Expand All @@ -79,9 +72,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
// reading, because TLS connections are initiated by clients.
SelectionKey newKey = rawChannel.register(selector, SelectionKey.OP_READ);
newKey.attach(tlsChannel);

} else if (key.isReadable() || key.isWritable()) {

// we have data (or buffer space) in existing connections

ByteBuffer buffer = ByteBuffer.allocate(10000);
Expand All @@ -93,7 +84,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
int c = tlsChannel.read(buffer);
if (c > 0) {
buffer.flip();
System.out.print(utf8.decode(buffer));
System.out.print(StandardCharsets.UTF_8.decode(buffer));
}
if (c < 0) {
tlsChannel.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.channels.Selector;
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 java.util.Iterator;
Expand Down Expand Up @@ -37,8 +36,6 @@
*/
public class NonBlockingServerWithOffLoopTasks {

private static final Charset utf8 = StandardCharsets.UTF_8;

private static final Executor taskExecutor =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
private static final Set<SelectionKey> taskReadyKeys = ConcurrentHashMap.newKeySet();
Expand All @@ -54,7 +51,6 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
serverSocket.configureBlocking(false);
Selector selector = Selector.open();
serverSocket.register(selector, SelectionKey.OP_ACCEPT);

while (true) {

// loop blocks here
Expand Down Expand Up @@ -119,7 +115,7 @@ private static void handleReadyChannel(Selector selector, SelectionKey key) thro
int c = tlsChannel.read(buffer);
if (c > 0) {
buffer.flip();
System.out.print(utf8.decode(buffer));
System.out.print(StandardCharsets.UTF_8.decode(buffer));
}
if (c < 0) {
tlsChannel.close();
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/tlschannel/example/SimpleBlockingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
Expand All @@ -14,8 +13,6 @@
/** Client example. Connects to a public TLS reporting service. */
public class SimpleBlockingClient {

private static final Charset utf8 = StandardCharsets.UTF_8;

public static final String domain = "www.howsmyssl.com";
public static final String httpLine = "GET https://www.howsmyssl.com/a/check HTTP/1.0\nHost: www.howsmyssl.com\n\n";

Expand All @@ -28,8 +25,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
try (SocketChannel rawChannel = SocketChannel.open()) {
rawChannel.connect(new InetSocketAddress(domain, 443));

// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal
// options
// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options
ClientTlsChannel.Builder builder = ClientTlsChannel.newBuilder(rawChannel, sslContext);

// instantiate TlsChannel
Expand All @@ -44,7 +40,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
// empty
}
res.flip();
System.out.println(utf8.decode(res));
System.out.println(StandardCharsets.UTF_8.decode(res));
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/tlschannel/example/SimpleBlockingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.nio.ByteBuffer;
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;
Expand All @@ -21,8 +20,6 @@
*/
public class SimpleBlockingServer {

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
Expand All @@ -36,8 +33,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
System.out.println("Waiting for connection...");
try (SocketChannel rawChannel = serverSocket.accept()) {

// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal
// options
// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options
ServerTlsChannel.Builder builder = ServerTlsChannel.newBuilder(rawChannel, sslContext);

// instantiate TlsChannel
Expand All @@ -47,7 +43,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
ByteBuffer res = ByteBuffer.allocate(10000);
while (tlsChannel.read(res) != -1) {
res.flip();
System.out.print(utf8.decode(res));
System.out.print(StandardCharsets.UTF_8.decode(res));
res.compact();
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/tlschannel/example/SniBlockingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.nio.ByteBuffer;
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 java.util.Optional;
Expand All @@ -25,8 +24,6 @@
*/
public class SniBlockingServer {

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
Expand Down Expand Up @@ -71,7 +68,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
ByteBuffer res = ByteBuffer.allocate(10000);
while (tlsChannel.read(res) != -1) {
res.flip();
System.out.print(utf8.decode(res));
System.out.print(StandardCharsets.UTF_8.decode(res));
res.compact();
}
}
Expand Down

0 comments on commit 4b68d5d

Please sign in to comment.