From 8795bd7d0f6fdc5154b6ca3906cc7a9693280dc1 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Wed, 21 Jun 2023 16:39:37 +0200 Subject: [PATCH] more wip --- src/main/java/biz/nellemann/jperf/App.java | 26 ++++++++++++------- .../java/biz/nellemann/jperf/DataType.java | 1 - .../java/biz/nellemann/jperf/Datagram.java | 6 ++--- .../java/biz/nellemann/jperf/UdpClient.java | 21 ++++++++------- .../java/biz/nellemann/jperf/UdpServer.java | 13 +++------- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/main/java/biz/nellemann/jperf/App.java b/src/main/java/biz/nellemann/jperf/App.java index 324e7d9..adbca2f 100644 --- a/src/main/java/biz/nellemann/jperf/App.java +++ b/src/main/java/biz/nellemann/jperf/App.java @@ -16,14 +16,22 @@ public class App implements Callable { final Logger log = LoggerFactory.getLogger(App.class); - @CommandLine.Option(names = { "-s", "--size" }, paramLabel = "SIZE", description = "the datagram size") - int size = 1500; + @CommandLine.Option(names = { "-s", "--pkt-size" }, paramLabel = "SIZE", description = "datagram size") + int packetSize = 1500; + + @CommandLine.Option(names = { "-n", "--pkt-num" }, paramLabel = "NUM", description = "datagrams to send") + int packetCount = 100; + + @CommandLine.Option(names = { "-p", "--port" }, paramLabel = "PORT", description = "network port") + int port = 4445; + + @Override public Integer call() throws Exception { // your business logic goes here... // Start server - UdpServer udpServer = new UdpServer(); + UdpServer udpServer = new UdpServer(port); udpServer.start(); @@ -31,10 +39,10 @@ public class App implements Callable { // Start client and send some messages - UdpClient udpClient = new UdpClient(); + UdpClient udpClient = new UdpClient("localhost", port); // Start datagram - Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), size, sequence++); + Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), packetSize, sequence++); udpClient.send(datagram); Thread.sleep(100); @@ -47,19 +55,19 @@ public class App implements Callable { // Data datagrams ... - for(int i = 0; i < 100; i++) { - datagram = new Datagram(DataType.DATA.getValue(), size, sequence++); + for(int i = 0; i < packetCount; i++) { + datagram = new Datagram(DataType.DATA.getValue(), packetSize, sequence++); udpClient.send(datagram); //Thread.sleep(50); } // End datagram Thread.sleep(500); - datagram = new Datagram(DataType.END.getValue(), size, sequence++); + datagram = new Datagram(DataType.END.getValue(), packetSize, sequence++); udpClient.send(datagram); udpClient.close(); - + Thread.sleep(1500); return 0; } diff --git a/src/main/java/biz/nellemann/jperf/DataType.java b/src/main/java/biz/nellemann/jperf/DataType.java index f6e6c59..b2d6de4 100644 --- a/src/main/java/biz/nellemann/jperf/DataType.java +++ b/src/main/java/biz/nellemann/jperf/DataType.java @@ -2,7 +2,6 @@ package biz.nellemann.jperf; public enum DataType { - HANDSHAKE(1), DATA(2), ACK(4), END(9); private final int value; diff --git a/src/main/java/biz/nellemann/jperf/Datagram.java b/src/main/java/biz/nellemann/jperf/Datagram.java index bf34589..9a88f1d 100644 --- a/src/main/java/biz/nellemann/jperf/Datagram.java +++ b/src/main/java/biz/nellemann/jperf/Datagram.java @@ -12,9 +12,9 @@ import org.slf4j.LoggerFactory; * * Datagram consists of the following *

- * <------------------------- HEADER 32 bytes -------------------> <---------- DATA min 32 bytes --------> - * _long _int _int _long _long - * 8_bytes 4_bytes 4_bytes 8_bytes 8_bytes + * <------------------------- HEADER 32 bytes --------------> <---------- DATA min 32 bytes --------> + * _long _int _int _long _long + * 8_bytes 4_bytes 4_bytes 8_bytes 8_bytes * MAGIC-ID TYPE LENGTH SEQUENCE TIMESTAMP * */ diff --git a/src/main/java/biz/nellemann/jperf/UdpClient.java b/src/main/java/biz/nellemann/jperf/UdpClient.java index a1fd4ee..ba0370c 100644 --- a/src/main/java/biz/nellemann/jperf/UdpClient.java +++ b/src/main/java/biz/nellemann/jperf/UdpClient.java @@ -14,39 +14,40 @@ public class UdpClient { final Logger log = LoggerFactory.getLogger(UdpClient.class); + private final int port; + private final InetAddress address; + private DatagramSocket socket; - private InetAddress address; private byte[] buf = new byte[256]; - public UdpClient() throws UnknownHostException, SocketException { - log.info("UdpClient()"); + public UdpClient(String hostname, int port) throws UnknownHostException, SocketException { + log.info("UdpClient() - target: {}, port: {}", hostname, port); + this.port = port; socket = new DatagramSocket(); - address = InetAddress.getByName("localhost"); + address = InetAddress.getByName(hostname); } public void send(Datagram datagram) throws IOException { - DatagramPacket packet = new DatagramPacket(datagram.getPayload(), datagram.getRealLength(), address, 4445); + DatagramPacket packet = new DatagramPacket(datagram.getPayload(), datagram.getRealLength(), address, port); socket.send(packet); } public Datagram receive() throws IOException { DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); - Datagram datagram = new Datagram(buf); - return datagram; + return new Datagram(buf); } public String sendEcho(String msg) throws IOException { log.info("send() - msg: {}", msg); buf = msg.getBytes(); - DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); + DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); packet = new DatagramPacket(buf, buf.length); socket.receive(packet); - String received = new String( packet.getData(), 0, packet.getLength() ); - return received; + return new String( packet.getData(), 0, packet.getLength() ); } public void close() { diff --git a/src/main/java/biz/nellemann/jperf/UdpServer.java b/src/main/java/biz/nellemann/jperf/UdpServer.java index df4123c..3c7b424 100644 --- a/src/main/java/biz/nellemann/jperf/UdpServer.java +++ b/src/main/java/biz/nellemann/jperf/UdpServer.java @@ -18,9 +18,9 @@ public class UdpServer extends Thread { private boolean running; private byte[] buf = new byte[256]; - public UdpServer() throws SocketException { - log.info("UdpServer()"); - socket = new DatagramSocket(4445); + public UdpServer(int port) throws SocketException { + log.info("UdpServer() - port: {}", port); + socket = new DatagramSocket(port); } public void run() { @@ -45,7 +45,7 @@ public class UdpServer extends Thread { thisSequence = datagram.getSequence(); if(datagram.getType() == DataType.HANDSHAKE.getValue()) { - log.info("Handshake from ... {}, length: {}", address, datagram.getLength()); + log.info("Handshake from ... {}", address); // Setup to receive larger datagrams buf = new byte[datagram.getLength()]; @@ -59,8 +59,6 @@ public class UdpServer extends Thread { if(datagram.getType() == DataType.END.getValue()) { running = false; - log.info("Stopping ...."); - // TODO: Reset ? } if(datagram.getType() == DataType.DATA.getValue()) { @@ -71,10 +69,7 @@ public class UdpServer extends Thread { } } - lastSequence = thisSequence; - - } socket.close();