StreamPacket class created so that we can store all packets generically. The timestamp has also been extracted and stored with the packet so that in the future we may turn the current ArrayList into a priority que.

#story[817]
This commit is contained in:
Kusal Ekanayake
2017-04-23 20:14:41 +12:00
parent ba352183bf
commit 3dc1a7f9c0
3 changed files with 93 additions and 13 deletions
@@ -0,0 +1,21 @@
package seng302.models.parsers;
/**
* Created by kre39 on 23/04/17.
*/
public class StreamPacket {
//Change int to an ENUM for the type
private int type;
private long messageLength;
private long timeStamp;
private byte[] payload;
public StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) {
this.type = type;
this.messageLength = messageLength;
this.timeStamp = timeStamp;
this.payload = payload;
}
}
@@ -0,0 +1,44 @@
package seng302.models.parsers;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* Created by kre39 on 23/04/17.
*/
public class StreamParser {
private static boolean isWithinTag;
static void parseLine(byte[] bytes) {
//TODO overhaul all of this to treat packets as appropriate
String line = new String(bytes);
if (line.startsWith("<")){
isWithinTag = true;
}
// System.out.println("line = ---------------------------------------------\n" + line);
if (isWithinTag) {
// try {
// Element node = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(line.getBytes())).getDocumentElement();
// if (node.getAttributes().getNamedItem("Type") != null) {
// System.out.println(node.getAttributes().getNamedItem("Type") );
// System.out.println(line);
// }
// } catch (Throwable e){
//// e.printStackTrace();
// }
}
if (line.startsWith("</")) {
isWithinTag = false;
}
}
}
@@ -3,16 +3,21 @@ package seng302.models.parsers;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger;
import java.net.Socket; import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import java.util.zip.Checksum; import java.util.zip.Checksum;
public class InputStreamParser { public class StreamReceiver {
private static ByteArrayOutputStream buffer; private static ByteArrayOutputStream buffer;
private static InputStream stream = null; private static InputStream stream = null;
private static boolean reading = true; private static boolean reading = true;
private static Collection<StreamPacket> priorityQue = new ArrayList<>();
private static void skipBytes(long n){ private static void skipBytes(long n){
for (int i=0; i < n; i++){ for (int i=0; i < n; i++){
@@ -37,14 +42,13 @@ public class InputStreamParser {
private static void runTest() { private static void runTest() {
Socket host = null; Socket host = null;
String hostAddress = "livedata.americascup.com"; // String hostAddress = "livedata.americascup.com";
String hostAddress = "csse-s302staff.canterbury.ac.nz";
int hostPort = 4941; int hostPort = 4941;
try { try {
host = new Socket(hostAddress, hostPort); host = new Socket(hostAddress, hostPort);
if (host != null) {
stream = host.getInputStream(); stream = host.getInputStream();
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -55,12 +59,15 @@ public class InputStreamParser {
while(reading) { while(reading) {
buffer = new ByteArrayOutputStream(); buffer = new ByteArrayOutputStream();
sync1 = readByte(); sync1 = readByte();
System.out.println("sync1 = " + Integer.toBinaryString(sync1)); // System.out.println("sync1 = " + Integer.toBinaryString(sync1));
sync2 = readByte(); sync2 = readByte();
//checking if it is the start of the packet //checking if it is the start of the packet
if(sync1 == 0x47 && sync2 == 0x83) { if(sync1 == 0x47 && sync2 == 0x83) {
System.out.println("message type: " + readByte()); int type = readByte();
skipBytes(10); // System.out.println("message type: " + type);
byte[] timeStampBytes = getBytes(6);
skipBytes(4);
// byte[] b = new byte[2]; // byte[] b = new byte[2];
// try { // try {
// stream.read(b); // stream.read(b);
@@ -68,15 +75,23 @@ public class InputStreamParser {
// e.printStackTrace(); // e.printStackTrace();
// } // }
// System.out.println("b = " + Integer.toBinaryString(b[0])); // System.out.println("b = " + Integer.toBinaryString(b[0]));
// System.out.println(timeStamp);
long timeStamp = 0;
long multiplier=1;
for(int i = 0;i < 6;i++) {
timeStamp += timeStampBytes[i]*multiplier;
multiplier *= 256;
}
long payloadLength = bytesToLong(getBytes(2)); long payloadLength = bytesToLong(getBytes(2));
System.out.println("payload length: " + payloadLength); //No. of milliseconds since Jan 1st 1970
skipBytes(payloadLength); 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(); Checksum checksum = new CRC32();
checksum.update(buffer.toByteArray(), 0, buffer.size()); checksum.update(buffer.toByteArray(), 0, buffer.size());
System.out.println(checksum.getValue()); // System.out.println(checksum.getValue());
long crc = bytesToLong(getBytes(4)); long crc = bytesToLong(getBytes(4));
System.out.println(crc); // System.out.println(crc);
} }
} }