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
.gradle
.vscode
.idea
# Ignore Gradle build output directory
build

View file

@ -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<Integer> {
@ -29,8 +29,12 @@ public class App implements Callable<Integer> {
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<Integer> {
udpClient.send(datagram);
udpClient.close();
Thread.sleep(1000);
return 0;

View file

@ -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);

View file

@ -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");
}