Skip to content

Commit

Permalink
Hongyi: add seq number checking, create a wrapper for serializing and…
Browse files Browse the repository at this point in the history
… deserializing UDP packet, fix bugs
  • Loading branch information
laoyaosniper committed Sep 13, 2014
1 parent 8104950 commit c39ae2d
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 171 deletions.
3 changes: 1 addition & 2 deletions UDP_Measurement_Server/src/com/udpmeasurement/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ public class Config {
public static final int MIN_PACKETSIZE = 36;
// Leave enough margin for min MTU in the link and IP options
public static final int MAX_PACKETSIZE = 512;
// Todo(Hongyi): Arbitrary value, need further discuss

public static final int DEFAULT_UDP_PACKET_SIZE = 100;
// Todo(Hongyi): Arbitrary timeout value, need further discuss
public static final int DEFAULT_TIMEOUT = 3000;
// Todo(Hongyi): Arbitrary value, need further discuss
public static final int MAX_BURSTCOUNT = 100;

public static final int PKT_ERROR = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MeasurementPacket {
public int type;
public int burstCount;
public int packetNum;
public int intervalNum;
public int inversionNum;
public long timestamp;
public int packetSize;
public int seq;
Expand Down Expand Up @@ -57,7 +57,7 @@ public MeasurementPacket(ClientIdentifier cliId, byte[] rawdata)
type = dataIn.readInt();
burstCount = dataIn.readInt();
packetNum = dataIn.readInt();
intervalNum = dataIn.readInt();
inversionNum = dataIn.readInt();
timestamp = dataIn.readLong();
packetSize = dataIn.readInt();
seq = dataIn.readInt();
Expand Down Expand Up @@ -87,7 +87,7 @@ public byte[] getByteArray() throws MeasurementError {
dataOut.writeInt(type);
dataOut.writeInt(burstCount);
dataOut.writeInt(packetNum);
dataOut.writeInt(intervalNum);
dataOut.writeInt(inversionNum);
dataOut.writeLong(timestamp);
dataOut.writeInt(packetSize);
dataOut.writeInt(seq);
Expand Down
16 changes: 13 additions & 3 deletions UDP_Measurement_Server/src/com/udpmeasurement/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* @author Hongyi Yao (hyyao@umich.edu)
Expand All @@ -37,7 +38,7 @@ public class RequestHandler implements Runnable {
*/
public RequestHandler(DatagramSocket socket, ClientIdentifier clientId,
ClientRecord clientRecord) {
this.socket = socket;
//this.socket = socket;
this.clientId = clientId;
this.clientRecord = clientRecord;
}
Expand All @@ -54,6 +55,7 @@ private void sendPacket() throws MeasurementError {
dataPacket.packetNum = clientRecord.packetReceived;
dataPacket.timestamp = System.currentTimeMillis();
dataPacket.packetSize = clientRecord.packetSize;
dataPacket.seq = clientRecord.seq;

byte[] sendBuffer = packet.getByteArray();
DatagramPacket sendPacket = new DatagramPacket(
Expand All @@ -68,8 +70,8 @@ private void sendPacket() throws MeasurementError {

Config.logmsg("Sent response to " + clientId.toString() + " type: PKT_DATA b:" +
packet.burstCount + " p:" + packet.packetNum + " i:" +
packet.intervalNum + " j:" + packet.timestamp + " s:" +
packet.packetSize);
packet.inversionNum + " j:" + packet.timestamp + " s:" +
packet.packetSize + " seq:" + packet.seq);
}

/* (non-Javadoc)
Expand All @@ -78,6 +80,12 @@ private void sendPacket() throws MeasurementError {
*/
@Override
public void run() {
try {
socket = new DatagramSocket();
} catch (SocketException e1) {
Config.logmsg("Socket creation failed when sending data packets");
}

for ( int i = 0; i < clientRecord.burstCount; i++ ) {
clientRecord.packetReceived = i;
try {
Expand All @@ -93,6 +101,8 @@ public void run() {
Config.logmsg("sleep is interrupted: " + e.getMessage());
}
}
// Recycle socket resource
socket.close();
}

}
24 changes: 14 additions & 10 deletions UDP_Measurement_Server/src/com/udpmeasurement/UDPReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,12 @@ public void run() {
*/
private void processPacket(MeasurementPacket packet)
throws MeasurementError {
if ( packet.type != Config.PKT_REQUEST && packet.type != Config.PKT_DATA ) {
// Send error packet back
Config.logmsg("Received malformed packet! Type " + packet.type);
sendPacket(Config.PKT_ERROR, packet.clientId, null);
}
else if ( packet.type == Config.PKT_REQUEST ) {
if ( packet.type == Config.PKT_REQUEST ) {
// Create a new thread to burst udp packets
Config.logmsg("Receive packet request");

ClientRecord clientRecord = new ClientRecord();
clientRecord.seq = packet.seq;
clientRecord.burstCount = packet.burstCount;
clientRecord.packetSize = packet.packetSize;
clientRecord.udpInterval = packet.udpInterval;
Expand Down Expand Up @@ -141,7 +137,7 @@ else if ( packet.type == Config.PKT_REQUEST ) {
socket, packet.clientId, clientRecord);
new Thread(respHandle).start();
}
else { // packetType == PKT_DATA
else if ( packet.type == Config.PKT_DATA ) {
// Look up the client map to find the corresponding recorder
// , or create a new one. Then record the packet's content
// After received all the packets in a burst or timeout,
Expand Down Expand Up @@ -170,7 +166,7 @@ else if ( packet.type == Config.PKT_REQUEST ) {
clientRecord.seq);
}
}
else {
else { // Receive UDP burst from a new client
clientRecord = new ClientRecord();
clientRecord.burstCount = packet.burstCount;
clientRecord.receivedNumberList.add(packet.packetNum);
Expand Down Expand Up @@ -198,6 +194,11 @@ else if ( packet.type == Config.PKT_REQUEST ) {
}
}
}
else {
// Not data or request packet, send error packet back
Config.logmsg("Received malformed packet! Type " + packet.type);
sendPacket(Config.PKT_ERROR, packet.clientId, null);
}
}

/**
Expand All @@ -222,15 +223,18 @@ else if ( type == Config.PKT_DATA ) {
dataPacket.packetNum = clientRecord.packetReceived;
dataPacket.timestamp = System.currentTimeMillis();
dataPacket.packetSize = clientRecord.packetSize;
dataPacket.seq = clientRecord.seq;
}
else if ( type == Config.PKT_RESPONSE ) {
MeasurementPacket responsePacket = packet;
responsePacket.type = Config.PKT_RESPONSE;
responsePacket.burstCount = clientRecord.burstCount;
responsePacket.intervalNum = clientRecord.calculateInversionNumber();
responsePacket.inversionNum = clientRecord.calculateInversionNumber();
// Store jitter in the field timestamp
responsePacket.timestamp = clientRecord.calculateJitter();
responsePacket.packetNum = clientRecord.receivedNumberList.size();
responsePacket.packetSize = clientRecord.packetSize;
responsePacket.seq = clientRecord.seq;
}

byte[] sendBuffer = packet.getByteArray();
Expand All @@ -245,7 +249,7 @@ else if ( type == Config.PKT_RESPONSE ) {
}

Config.logmsg("Sent response to " + clientId.toString() + " type:" + type + " b:" +
packet.burstCount + " p:" + packet.packetNum + " i:" + packet.intervalNum +
packet.burstCount + " p:" + packet.packetNum + " i:" + packet.inversionNum +
" j:" + packet.timestamp + " s:" + packet.packetSize);
}

Expand Down
3 changes: 2 additions & 1 deletion UDP_Measurement_Server/src/com/udpmeasurement/UDPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class UDPServer {
* Check the port and create the receiver thread
* @param args port used by server
*/
private static final String VERSION = "2.1";
public static void main(String[] args) {
UDPReceiver deamon;
int port = 0;
Expand All @@ -38,7 +39,7 @@ public static void main(String[] args) {
else {
port = Config.DEFAULT_PORT;
}
System.out.println("UDP Burst server(Ver 2.0) runs on port " + port);
System.out.println("UDP Burst server(Ver " + VERSION + ") runs on port " + port);
try {
deamon = new UDPReceiver(port);
new Thread(deamon).start();
Expand Down
2 changes: 1 addition & 1 deletion android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<string name="UDPBurstDirLabel">Direction:</string>
<string name="UDPBurstIntervalLabel">Interval(ms): </string>
<string name="UDPBurstTargetHint">www.google.com</string>
<string name="UDPBurstIntervalHint">256</string>
<string name="UDPBurstIntervalHint">1</string>
<string name="UDPBurstPacketSizeLabel">Packet Size(byte): </string>
<string name="UDPBurstPacketSizeHint">100</string>
<string name="UDPBurstPacketCountLabel">Packet Number: </string>
Expand Down
6 changes: 6 additions & 0 deletions android/src/com/mobiperf/MeasurementCreationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ private void setupEditTextFocusChangeListener() {
text.setOnFocusChangeListener(textFocusChangeListener);
text = (EditText) findViewById(R.id.dnsLookupText);
text.setOnFocusChangeListener(textFocusChangeListener);
text = (EditText) findViewById(R.id.UDPBurstIntervalText);
text.setOnFocusChangeListener(textFocusChangeListener);
text = (EditText) findViewById(R.id.UDPBurstPacketCountText);
text.setOnFocusChangeListener(textFocusChangeListener);
text = (EditText) findViewById(R.id.UDPBurstPacketSizeText);
text.setOnFocusChangeListener(textFocusChangeListener);
}

@Override
Expand Down
Loading

0 comments on commit c39ae2d

Please sign in to comment.