This commit is contained in:
Mark Nellemann 2023-06-21 16:39:37 +02:00
parent 9b974eb22c
commit 8795bd7d0f
5 changed files with 35 additions and 32 deletions

View file

@ -16,14 +16,22 @@ public class App implements Callable<Integer> {
final Logger log = LoggerFactory.getLogger(App.class); final Logger log = LoggerFactory.getLogger(App.class);
@CommandLine.Option(names = { "-s", "--size" }, paramLabel = "SIZE", description = "the datagram size") @CommandLine.Option(names = { "-s", "--pkt-size" }, paramLabel = "SIZE", description = "datagram size")
int size = 1500; 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 @Override
public Integer call() throws Exception { // your business logic goes here... public Integer call() throws Exception { // your business logic goes here...
// Start server // Start server
UdpServer udpServer = new UdpServer(); UdpServer udpServer = new UdpServer(port);
udpServer.start(); udpServer.start();
@ -31,10 +39,10 @@ public class App implements Callable<Integer> {
// Start client and send some messages // Start client and send some messages
UdpClient udpClient = new UdpClient(); UdpClient udpClient = new UdpClient("localhost", port);
// Start datagram // Start datagram
Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), size, sequence++); Datagram datagram = new Datagram(DataType.HANDSHAKE.getValue(), packetSize, sequence++);
udpClient.send(datagram); udpClient.send(datagram);
Thread.sleep(100); Thread.sleep(100);
@ -47,19 +55,19 @@ public class App implements Callable<Integer> {
// Data datagrams ... // Data datagrams ...
for(int i = 0; i < 100; i++) { for(int i = 0; i < packetCount; i++) {
datagram = new Datagram(DataType.DATA.getValue(), size, sequence++); datagram = new Datagram(DataType.DATA.getValue(), packetSize, sequence++);
udpClient.send(datagram); udpClient.send(datagram);
//Thread.sleep(50); //Thread.sleep(50);
} }
// End datagram // End datagram
Thread.sleep(500); Thread.sleep(500);
datagram = new Datagram(DataType.END.getValue(), size, sequence++); datagram = new Datagram(DataType.END.getValue(), packetSize, sequence++);
udpClient.send(datagram); udpClient.send(datagram);
udpClient.close(); udpClient.close();
Thread.sleep(1500);
return 0; return 0;
} }

View file

@ -2,7 +2,6 @@ package biz.nellemann.jperf;
public enum DataType { public enum DataType {
HANDSHAKE(1), DATA(2), ACK(4), END(9); HANDSHAKE(1), DATA(2), ACK(4), END(9);
private final int value; private final int value;

View file

@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
* *
* Datagram consists of the following * Datagram consists of the following
* <p> * <p>
* <------------------------- HEADER 32 bytes -------------------> <---------- DATA min 32 bytes --------> * <------------------------- HEADER 32 bytes --------------> <---------- DATA min 32 bytes -------->
* _long _int _int _long _long * _long _int _int _long _long
* 8_bytes 4_bytes 4_bytes 8_bytes 8_bytes * 8_bytes 4_bytes 4_bytes 8_bytes 8_bytes
* MAGIC-ID TYPE LENGTH SEQUENCE TIMESTAMP * MAGIC-ID TYPE LENGTH SEQUENCE TIMESTAMP

View file

@ -14,39 +14,40 @@ public class UdpClient {
final Logger log = LoggerFactory.getLogger(UdpClient.class); final Logger log = LoggerFactory.getLogger(UdpClient.class);
private final int port;
private final InetAddress address;
private DatagramSocket socket; private DatagramSocket socket;
private InetAddress address;
private byte[] buf = new byte[256]; private byte[] buf = new byte[256];
public UdpClient() throws UnknownHostException, SocketException { public UdpClient(String hostname, int port) throws UnknownHostException, SocketException {
log.info("UdpClient()"); log.info("UdpClient() - target: {}, port: {}", hostname, port);
this.port = port;
socket = new DatagramSocket(); socket = new DatagramSocket();
address = InetAddress.getByName("localhost"); address = InetAddress.getByName(hostname);
} }
public void send(Datagram datagram) throws IOException { 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); socket.send(packet);
} }
public Datagram receive() throws IOException { public Datagram receive() throws IOException {
DatagramPacket packet = new DatagramPacket(buf, buf.length); DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); socket.receive(packet);
Datagram datagram = new Datagram(buf); return new Datagram(buf);
return datagram;
} }
public String sendEcho(String msg) throws IOException { public String sendEcho(String msg) throws IOException {
log.info("send() - msg: {}", msg); log.info("send() - msg: {}", msg);
buf = msg.getBytes(); buf = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet); socket.send(packet);
packet = new DatagramPacket(buf, buf.length); packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); socket.receive(packet);
String received = new String( packet.getData(), 0, packet.getLength() ); return new String( packet.getData(), 0, packet.getLength() );
return received;
} }
public void close() { public void close() {

View file

@ -18,9 +18,9 @@ public class UdpServer extends Thread {
private boolean running; private boolean running;
private byte[] buf = new byte[256]; private byte[] buf = new byte[256];
public UdpServer() throws SocketException { public UdpServer(int port) throws SocketException {
log.info("UdpServer()"); log.info("UdpServer() - port: {}", port);
socket = new DatagramSocket(4445); socket = new DatagramSocket(port);
} }
public void run() { public void run() {
@ -45,7 +45,7 @@ public class UdpServer extends Thread {
thisSequence = datagram.getSequence(); thisSequence = datagram.getSequence();
if(datagram.getType() == DataType.HANDSHAKE.getValue()) { if(datagram.getType() == DataType.HANDSHAKE.getValue()) {
log.info("Handshake from ... {}, length: {}", address, datagram.getLength()); log.info("Handshake from ... {}", address);
// Setup to receive larger datagrams // Setup to receive larger datagrams
buf = new byte[datagram.getLength()]; buf = new byte[datagram.getLength()];
@ -59,8 +59,6 @@ public class UdpServer extends Thread {
if(datagram.getType() == DataType.END.getValue()) { if(datagram.getType() == DataType.END.getValue()) {
running = false; running = false;
log.info("Stopping ....");
// TODO: Reset ?
} }
if(datagram.getType() == DataType.DATA.getValue()) { if(datagram.getType() == DataType.DATA.getValue()) {
@ -71,10 +69,7 @@ public class UdpServer extends Thread {
} }
} }
lastSequence = thisSequence; lastSequence = thisSequence;
} }
socket.close(); socket.close();