Add NIO client example

This commit is contained in:
Stuart Stock
2017-02-07 11:37:36 -06:00
parent 9bfa1e3f37
commit 3231247f81
4 changed files with 127 additions and 12 deletions

View File

@ -10,7 +10,6 @@ created by Adam Langley and Robert Obryk.
## Links
* [Nearenough Github repo](https://github.com/int08h/nearenough)
* [Roughtime project](https://roughtime.googlesource.com/roughtime)
* [Netty project](http://netty.io/)
## Building
Nearenough bundles all required dependencies in the `lib` directory. Add those `.jar` files to
@ -19,9 +18,9 @@ your IDE's project classpath. Building is IDE-only for the moment.
## Client Quickstart
### Client Examples
See [NioClient.java](../master/src/examples/NioClient.java) and
[NettyClient.java](../master/src/examples/NettyClient.java) for examples of sending a request to a
Roughtime server and processing the response.
See [`NioClient.java`](../master/src/examples/NioClient.java) and
[`NettyClient.java`](../master/src/examples/NettyClient.java) for examples of sending a request to
a Roughtime server and processing the response.
### DIY Client
If implementing your own client, the general idea is:
@ -62,7 +61,7 @@ Nearenough is not stable yet. Expect significant changes as the code evolves.
* Server - Not started
## Contributors
* Stuart Stock, original author (stuart {at} int08h.com)
* Stuart Stock, original author and current maintainer (stuart {at} int08h.com)
## Copyright and License
Nearenough is Copyright (c) 2017 int08h LLC. All rights reserved.

View File

@ -79,6 +79,7 @@ public final class NettyClient {
});
}
@SuppressWarnings("Duplicates")
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
// A reply from the server has been received
@ -101,7 +102,7 @@ public final class NettyClient {
// The "midpoint" is the Roughtime server's reported timestamp (in microseconds). And the
// "radius" is a span of uncertainty around that midpoint. A Roughtime server asserts that
// its "true time" lies within the span.
Instant midpoint = Instant.ofEpochMilli(client.midpoint() / 1000L);
Instant midpoint = Instant.ofEpochMilli(client.midpoint() / 1_000L);
int radiusSec = client.radius() / 1_000_000;
System.out.println("midpoint : " + midpoint + " (radius " + radiusSec + " sec)");

View File

@ -15,9 +15,110 @@
package examples;
import static nearenough.util.BytesUtil.hexToBytes;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.time.Instant;
import nearenough.client.RoughtimeClient;
import nearenough.protocol.RtMessage;
import nearenough.protocol.RtWire;
/**
* Use Java NIO to send a request to the given Roughtime server and dump the response (if any)
*/
public final class NioClient {
// TODO(stuart)
// Hostname and port of the public Google Roughtime server
private static final String GOOGLE_SERVER_HOST = "roughtime.sandbox.google.com";
private static final int GOOGLE_SERVER_PORT = 2002;
// Long-term public key of the public Google Roughtime server
private static final byte[] GOOGLE_SERVER_PUBKEY = hexToBytes(
"7ad3da688c5c04c635a14786a70bcf30224cc25455371bf9d4a2bfb64b682534"
);
@SuppressWarnings("Duplicates")
public static void main(String[] args) throws IOException, InterruptedException {
InetSocketAddress addr = new InetSocketAddress(GOOGLE_SERVER_HOST, GOOGLE_SERVER_PORT);
System.out.printf("Sending request to %s\n", addr);
// Nonblocking NIO UDP channel for the remote Roughtime server
DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(GOOGLE_SERVER_PORT));
// Create a new RoughtimeClient instance
RoughtimeClient client = new RoughtimeClient(GOOGLE_SERVER_PUBKEY);
// Create a request message
RtMessage request = client.createRequest();
// Encode for transmission
ByteBuf encodedRequest = RtWire.toWire(request);
// Send the encoded request, converting the Netty ByteBuf to a Java ByteBuffer for NIO use.
int bytesWritten = channel.send(encodedRequest.nioBuffer(), addr);
// Ensure the message was sent
if (bytesWritten != encodedRequest.readableBytes()) {
throw new RuntimeException("failed to fully write request");
}
// Space for receiving the reply
ByteBuffer recvBuf = ByteBuffer.allocate(4096);
int attempts = 50;
// Simple loop to look for the first response. Wait for max 5 seconds.
while (--attempts > 0) {
recvBuf.clear();
channel.receive(recvBuf);
recvBuf.flip();
if (recvBuf.hasRemaining()) {
break;
}
Thread.sleep(100);
}
if (recvBuf.hasRemaining()) {
// A reply from the server has been received
System.out.printf("Read message of %d bytes from %s:\n", recvBuf.remaining(), addr);
// Parse the response
RtMessage response = RtMessage.fromByteBuffer(recvBuf);
System.out.println(response);
// Validate the response. Checks that the message is well-formed, all signatures are valid,
// and our nonce is present in the response.
client.processResponse(response);
if (client.isResponseValid()) {
// Validation passed, the response is good
// The "midpoint" is the Roughtime server's reported timestamp (in microseconds). And the
// "radius" is a span of uncertainty around that midpoint. A Roughtime server asserts that
// its "true time" lies within the span.
Instant midpoint = Instant.ofEpochMilli(client.midpoint() / 1_000L);
int radiusSec = client.radius() / 1_000_000;
System.out.println("midpoint : " + midpoint + " (radius " + radiusSec + " sec)");
// For comparison, also print the local clock. If the midpoint and your local time
// are widely different, check your local machine's time sync!
Instant local = Instant.now();
System.out.println("local clock : " + local);
} else {
// Validation failed. Print out the reason why.
System.out.println("Response INVALID: " + client.invalidResponseCause().getMessage());
}
} else {
// No reply within five seconds
System.out.println("No response from " + addr);
}
}
}

View File

@ -21,6 +21,7 @@ import static nearenough.util.Preconditions.checkState;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
@ -48,13 +49,26 @@ public final class RtMessage {
/**
* @return A new {@code RtMessage} by parsing the contents of the provided {@code byte[]}. The
* contents of {@code bytes} must be a well-formed Roughtime message.
* contents of {@code srcBytes} must be a well-formed Roughtime message.
*/
public static RtMessage fromBytes(byte[] bytes) {
checkNotNull(bytes, "bytes");
public static RtMessage fromBytes(byte[] srcBytes) {
checkNotNull(srcBytes, "srcBytes");
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(bytes.length);
buf.writeBytes(bytes);
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBytes.length);
buf.writeBytes(srcBytes);
return new RtMessage(buf);
}
/**
* @return A new {@code RtMessage} by parsing the contents of the provided {@code ByteBuffer},
* which can be direct or heap based. The contents of {@code srcBuf} must be a well-formed
* Roughtime message.
*/
public static RtMessage fromByteBuffer(ByteBuffer srcBuf) {
checkNotNull(srcBuf, "srcBuf");
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBuf.remaining());
buf.writeBytes(srcBuf);
return new RtMessage(buf);
}