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);
@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<Integer> {
// 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<Integer> {
// 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;
}

View file

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

View file

@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
*
* Datagram consists of the following
* <p>
* <------------------------- HEADER 32 bytes -------------------> <---------- DATA min 32 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

View file

@ -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() {

View file

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