Created packet enum to class packets and started progress on how the packets are read and parsed according to the type of packet.

#story[820]
This commit is contained in:
Kusal Ekanayake
2017-04-24 15:50:21 +12:00
parent 3dc1a7f9c0
commit 403dc480c4
4 changed files with 85 additions and 3 deletions
@@ -0,0 +1,53 @@
package seng302.models.parsers;
/**
* Created by Kusal on 4/24/2017.
*/
public enum PacketType {
HEARTBEAT,
RACE_STATUS,
DISPLAY_TEXT_MESSAGE,
XML_MESSAGE,
RACE_START_STATUS,
YACHT_EVENT_CODE,
YACHT_ACTION_CODE,
CHATTER_TEXT,
BOAT_LOCATION,
MARK_ROUNDING,
COURSE_WIND,
AVG_WIND,
OTHER;
static PacketType assignPacketType(int packetType){
switch(packetType){
case 1:
return HEARTBEAT;
case 12:
return RACE_STATUS;
case 20:
return DISPLAY_TEXT_MESSAGE;
case 26:
return XML_MESSAGE;
case 27:
return RACE_START_STATUS;
case 29:
return YACHT_EVENT_CODE;
case 31:
return YACHT_ACTION_CODE;
case 36:
return CHATTER_TEXT;
case 37:
return BOAT_LOCATION;
case 38:
return MARK_ROUNDING;
case 44:
return COURSE_WIND;
case 47:
return AVG_WIND;
default:
}
return OTHER;
}
}
@@ -6,16 +6,21 @@ package seng302.models.parsers;
public class StreamPacket {
//Change int to an ENUM for the type
private int type;
private PacketType type;
private long messageLength;
private long timeStamp;
private byte[] payload;
public StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) {
this.type = type;
this.type = PacketType.assignPacketType(type);
this.messageLength = messageLength;
this.timeStamp = timeStamp;
this.payload = payload;
// System.out.println("type = " + type);
if (this.type == PacketType.BOAT_LOCATION){
System.out.println(this.type.toString());
StreamParser.extractBoatLocation(payload);
}
}
}
@@ -8,6 +8,8 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Created by kre39 on 23/04/17.
@@ -40,5 +42,27 @@ public class StreamParser {
}
}
static void extractBoatLocation(byte[] payload){
byte[] latBytes = Arrays.copyOfRange(payload,16,20);
byte[] lonBytes = Arrays.copyOfRange(payload,20,24);
byte[] boatIdBytes = Arrays.copyOfRange(payload,8,12);
int boatId = ByteBuffer.wrap(boatIdBytes).getInt();
int lat = ByteBuffer.wrap(latBytes).getInt();
int lon = ByteBuffer.wrap(lonBytes).getInt();
// System.out.println("boatId = " + boatId);
// System.out.println("lon = " + 180 * (lon/Math.pow(2,31)));
// System.out.println("lat = " + 180 * (lat/Math.pow(2,31)));
}
public static int toInt(byte[] bytes, int offset) {
int ret = 0;
for (int i=0; i<4 && i+offset<bytes.length; i++) {
ret <<= 8;
ret |= (int)bytes[i] & 0xFF;
}
return ret;
}
}
@@ -84,7 +84,7 @@ public class StreamReceiver {
}
long payloadLength = bytesToLong(getBytes(2));
//No. of milliseconds since Jan 1st 1970
System.out.println("timeStamp = " + timeStamp);
// System.out.println("timeStamp = " + timeStamp);
// System.out.println("payload length: " + payloadLength);
priorityQue.add(new StreamPacket(type, payloadLength, timeStamp, getBytes((int)payloadLength)));
Checksum checksum = new CRC32();