diff --git a/.gitignore b/.gitignore index 196d4ef..9c1bfc6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Ignore Gradle project-specific cache directory .gradle .vscode +.idea # Ignore Gradle build output directory build diff --git a/src/main/java/biz/nellemann/jperf/App.java b/src/main/java/biz/nellemann/jperf/App.java index c203369..8dfa504 100644 --- a/src/main/java/biz/nellemann/jperf/App.java +++ b/src/main/java/biz/nellemann/jperf/App.java @@ -8,8 +8,8 @@ import picocli.CommandLine; import picocli.CommandLine.Command; import java.util.concurrent.Callable; -@Command(name = "jperf", mixinStandardHelpOptions = true, version = "checksum 4.0", - description = "Java network performance tool.") +@Command(name = "jperf", mixinStandardHelpOptions = true, version = "0.1", + description = "Network performance measurement tool.") public class App implements Callable { @@ -29,8 +29,12 @@ public class App implements Callable { Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), 64, sequence++); udpClient.send(datagram); + // TODO: Wait for ACK + + // Data datagrams ... for(int i = 0; i < 10; i++) { + Thread.sleep(1000); datagram = new Datagram(DataType.DATA.getValue(), 64, sequence++); udpClient.send(datagram); } @@ -40,6 +44,7 @@ public class App implements Callable { udpClient.send(datagram); udpClient.close(); + Thread.sleep(1000); return 0; diff --git a/src/main/java/biz/nellemann/jperf/Datagram.java b/src/main/java/biz/nellemann/jperf/Datagram.java index 23338f4..ccbb466 100644 --- a/src/main/java/biz/nellemann/jperf/Datagram.java +++ b/src/main/java/biz/nellemann/jperf/Datagram.java @@ -23,7 +23,7 @@ public class Datagram { private final int HEADER_LENGTH = 24; private final int type; - private final int size; + private final int length; private final long sequence; private final long timestamp; @@ -33,16 +33,16 @@ public class Datagram { /** * Create new empty datagram * @param type - * @param size + * @param lenght * @param sequence */ - public Datagram(int type, int size, long sequence) { + public Datagram(int type, int lenght, long sequence) { this.type = type; - this.size = size; + this.length = lenght; this.sequence = sequence; this.timestamp = System.currentTimeMillis(); - this.data = new byte[size]; + this.data = new byte[lenght - HEADER_LENGTH]; } @@ -55,39 +55,36 @@ public class Datagram { log.info("Datagram() 1"); - ByteBuffer buffer = ByteBuffer.allocate(payload.length); + ByteBuffer buffer = ByteBuffer.wrap(payload); // Order is importent when assembling header fields like this type = buffer.getInt(); - size = buffer.getInt(); + length = buffer.getInt(); sequence = buffer.getLong(); timestamp = buffer.getLong(); log.info("Datagram() 2 "); - - // Read everything after Header - data = new byte[size]; - log.info("Datagram() 3 "); - buffer.get(data, HEADER_LENGTH, size); // ERROR + data = new byte[length - HEADER_LENGTH]; + buffer.get(data); log.info("Datagram() 4"); } public int getLength() { - return HEADER_LENGTH + data.length; + return length; } public byte[] getPayload() throws IOException { - ByteBuffer buffer = ByteBuffer.allocate(HEADER_LENGTH + data.length + 1); + ByteBuffer buffer = ByteBuffer.allocate(length); // Order is important buffer.putInt(type); - buffer.putInt(size); + buffer.putInt(length); buffer.putLong(sequence); buffer.putLong(timestamp); diff --git a/src/main/java/biz/nellemann/jperf/UdpClient.java b/src/main/java/biz/nellemann/jperf/UdpClient.java index 61b0756..b5f0189 100644 --- a/src/main/java/biz/nellemann/jperf/UdpClient.java +++ b/src/main/java/biz/nellemann/jperf/UdpClient.java @@ -20,7 +20,7 @@ public class UdpClient { private byte[] buf; public UdpClient() throws UnknownHostException, SocketException { - log.info("UdpClient"); + log.info("UdpClient()"); socket = new DatagramSocket(); address = InetAddress.getByName("localhost"); }