This commit is contained in:
Mark Nellemann 2023-06-20 15:58:00 +02:00
parent f5948162f2
commit 48f7699192
4 changed files with 21 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
# Ignore Gradle project-specific cache directory # Ignore Gradle project-specific cache directory
.gradle .gradle
.vscode .vscode
.idea
# Ignore Gradle build output directory # Ignore Gradle build output directory
build build

View File

@ -8,8 +8,8 @@ import picocli.CommandLine;
import picocli.CommandLine.Command; import picocli.CommandLine.Command;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@Command(name = "jperf", mixinStandardHelpOptions = true, version = "checksum 4.0", @Command(name = "jperf", mixinStandardHelpOptions = true, version = "0.1",
description = "Java network performance tool.") description = "Network performance measurement tool.")
public class App implements Callable<Integer> { public class App implements Callable<Integer> {
@ -29,8 +29,12 @@ public class App implements Callable<Integer> {
Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), 64, sequence++); Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), 64, sequence++);
udpClient.send(datagram); udpClient.send(datagram);
// TODO: Wait for ACK
// Data datagrams ... // Data datagrams ...
for(int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++) {
Thread.sleep(1000);
datagram = new Datagram(DataType.DATA.getValue(), 64, sequence++); datagram = new Datagram(DataType.DATA.getValue(), 64, sequence++);
udpClient.send(datagram); udpClient.send(datagram);
} }
@ -40,6 +44,7 @@ public class App implements Callable<Integer> {
udpClient.send(datagram); udpClient.send(datagram);
udpClient.close(); udpClient.close();
Thread.sleep(1000);
return 0; return 0;

View File

@ -23,7 +23,7 @@ public class Datagram {
private final int HEADER_LENGTH = 24; private final int HEADER_LENGTH = 24;
private final int type; private final int type;
private final int size; private final int length;
private final long sequence; private final long sequence;
private final long timestamp; private final long timestamp;
@ -33,16 +33,16 @@ public class Datagram {
/** /**
* Create new empty datagram * Create new empty datagram
* @param type * @param type
* @param size * @param lenght
* @param sequence * @param sequence
*/ */
public Datagram(int type, int size, long sequence) { public Datagram(int type, int lenght, long sequence) {
this.type = type; this.type = type;
this.size = size; this.length = lenght;
this.sequence = sequence; this.sequence = sequence;
this.timestamp = System.currentTimeMillis(); 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"); log.info("Datagram() 1");
ByteBuffer buffer = ByteBuffer.allocate(payload.length); ByteBuffer buffer = ByteBuffer.wrap(payload);
// Order is importent when assembling header fields like this // Order is importent when assembling header fields like this
type = buffer.getInt(); type = buffer.getInt();
size = buffer.getInt(); length = buffer.getInt();
sequence = buffer.getLong(); sequence = buffer.getLong();
timestamp = buffer.getLong(); timestamp = buffer.getLong();
log.info("Datagram() 2 "); log.info("Datagram() 2 ");
// Read everything after Header
data = new byte[size];
log.info("Datagram() 3 "); log.info("Datagram() 3 ");
buffer.get(data, HEADER_LENGTH, size); // ERROR data = new byte[length - HEADER_LENGTH];
buffer.get(data);
log.info("Datagram() 4"); log.info("Datagram() 4");
} }
public int getLength() { public int getLength() {
return HEADER_LENGTH + data.length; return length;
} }
public byte[] getPayload() throws IOException { public byte[] getPayload() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(HEADER_LENGTH + data.length + 1); ByteBuffer buffer = ByteBuffer.allocate(length);
// Order is important // Order is important
buffer.putInt(type); buffer.putInt(type);
buffer.putInt(size); buffer.putInt(length);
buffer.putLong(sequence); buffer.putLong(sequence);
buffer.putLong(timestamp); buffer.putLong(timestamp);

View File

@ -20,7 +20,7 @@ public class UdpClient {
private byte[] buf; private byte[] buf;
public UdpClient() throws UnknownHostException, SocketException { public UdpClient() throws UnknownHostException, SocketException {
log.info("UdpClient"); log.info("UdpClient()");
socket = new DatagramSocket(); socket = new DatagramSocket();
address = InetAddress.getByName("localhost"); address = InetAddress.getByName("localhost");
} }