mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
added functionality to check the CRC for the packet. I ran into a lot of trouble with this regarding everything in java being signed by twos compliment #story[817]
This commit is contained in:
@@ -1,39 +1,30 @@
|
|||||||
package seng302.models.parsers;
|
package seng302.models.parsers;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.zip.CRC32;
|
||||||
|
import java.util.zip.Checksum;
|
||||||
|
|
||||||
|
|
||||||
public class StreamReceiver {
|
public class InputStreamParser {
|
||||||
|
|
||||||
|
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 BufferedReader buffer = null;
|
|
||||||
private static String currentLine;
|
|
||||||
private static boolean isWithinTag = false;
|
|
||||||
|
|
||||||
private static void skipBytes(int n){
|
private static void skipBytes(long n){
|
||||||
for (int i=0; i < n; i++){
|
for (int i=0; i < n; i++){
|
||||||
readByte();
|
readByte();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void readLine() {
|
|
||||||
try {
|
|
||||||
//Rather than read strings it reads a long which is used for checking the head
|
|
||||||
currentLine = buffer.readLine();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int readByte() {
|
private static int readByte() {
|
||||||
int currentByte = -1;
|
int currentByte = -1;
|
||||||
try {
|
try {
|
||||||
currentByte = stream.read();
|
currentByte = stream.read();
|
||||||
|
buffer.write(currentByte);
|
||||||
if (currentByte == -1){
|
if (currentByte == -1){
|
||||||
reading = false;
|
reading = false;
|
||||||
}
|
}
|
||||||
@@ -43,11 +34,10 @@ public class StreamReceiver {
|
|||||||
return currentByte;
|
return currentByte;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void runPacketLengthTest() {
|
private static void runTest() {
|
||||||
|
|
||||||
Socket host = null;
|
Socket host = null;
|
||||||
String hostAddress = "csse-s302staff.canterbury.ac.nz";
|
String hostAddress = "livedata.americascup.com";
|
||||||
// String hostAddress = "livedata.americascup.com";
|
|
||||||
int hostPort = 4941;
|
int hostPort = 4941;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -63,22 +53,30 @@ public class StreamReceiver {
|
|||||||
int sync2;
|
int sync2;
|
||||||
//currently "reading" will not break the program nicely (because there are multiple readBytes within the while loop)
|
//currently "reading" will not break the program nicely (because there are multiple readBytes within the while loop)
|
||||||
while(reading) {
|
while(reading) {
|
||||||
|
buffer = new ByteArrayOutputStream();
|
||||||
sync1 = readByte();
|
sync1 = readByte();
|
||||||
|
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());
|
System.out.println("message type: " + readByte());
|
||||||
skipBytes(10);
|
skipBytes(10);
|
||||||
byte[] b = new byte[2];
|
// byte[] b = new byte[2];
|
||||||
try {
|
// try {
|
||||||
stream.read(b);
|
// stream.read(b);
|
||||||
} catch (IOException e){
|
// } catch (IOException e){
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
int payloadLength = bytesToInt(b);
|
// System.out.println("b = " + Integer.toBinaryString(b[0]));
|
||||||
|
long payloadLength = bytesToLong(getBytes(2));
|
||||||
System.out.println("payload length: " + payloadLength);
|
System.out.println("payload length: " + payloadLength);
|
||||||
skipBytes(payloadLength);
|
skipBytes(payloadLength);
|
||||||
skipBytes(4);
|
|
||||||
|
Checksum checksum = new CRC32();
|
||||||
|
checksum.update(buffer.toByteArray(), 0, buffer.size());
|
||||||
|
System.out.println(checksum.getValue());
|
||||||
|
long crc = bytesToLong(getBytes(4));
|
||||||
|
System.out.println(crc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,83 +91,42 @@ public class StreamReceiver {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static byte[] getBytes(int n){
|
||||||
private static void runParserTest() {
|
byte[] bytes = new byte[n];
|
||||||
Socket host = null;
|
for (int i = 0; i < n; i++){
|
||||||
String hostAddress = "csse-s302staff.canterbury.ac.nz";
|
bytes[i] = (byte) readByte();
|
||||||
int hostPort = 4941;
|
// System.out.println(Integer.toBinaryString(bytes[i]));
|
||||||
|
// System.out.println(bytes[i]);
|
||||||
try {
|
|
||||||
host = new Socket(hostAddress, hostPort);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
return bytes;
|
||||||
try {
|
|
||||||
if (host != null) {
|
|
||||||
buffer = new BufferedReader(new InputStreamReader(host.getInputStream()));
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
readLine();
|
|
||||||
boolean reading = true;
|
|
||||||
|
|
||||||
while(reading) {
|
|
||||||
parseLine(currentLine);
|
|
||||||
readLine();
|
|
||||||
|
|
||||||
if (currentLine == null) {
|
|
||||||
reading = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (host != null) {
|
|
||||||
host.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void parseLine(String line){
|
|
||||||
if (line.startsWith("<Boat") || line.startsWith("<Race")){
|
|
||||||
isWithinTag = true;
|
|
||||||
}
|
|
||||||
if (isWithinTag) {
|
|
||||||
// System.out.println(line);
|
|
||||||
}
|
|
||||||
if (line.startsWith("</Boat") || line.startsWith("</Race")) {
|
|
||||||
isWithinTag = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* takes an array of up to 4 bytes and returns and int
|
* takes an array of up to 4 bytes and returns a positive
|
||||||
* @return an int if there is less than 4 bytes -1 otherwise
|
* long constructed from the input bytes
|
||||||
|
*
|
||||||
|
* (note it is assumed the bytes coming in need to be reversed like those from a stream)
|
||||||
|
*
|
||||||
|
* @return a positive long if there is less than 4 bytes -1 otherwise
|
||||||
*/
|
*/
|
||||||
private static int bytesToInt(byte[] bytes){
|
private static long bytesToLong(byte[] bytes){
|
||||||
int partialInt = 0;
|
long partialLong = 0;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (byte b: bytes){
|
for (byte b: bytes){
|
||||||
if (index > 3){
|
if (index > 3){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
partialInt = partialInt | (b & 0xFF) << (index * 8);
|
partialLong = partialLong | (b & 0xFFL) << (index * 8);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
return partialInt;
|
return partialLong;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
runPacketLengthTest();
|
runTest();
|
||||||
runParserTest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user