converted prototype to be reading the stream byte by byte rather than by lines and characters which was very confusing and unreliable. currently extracting message type and payload length. #story[817]

This commit is contained in:
Peter
2017-04-22 16:39:03 +12:00
parent 50e7ece477
commit 247560ee43
@@ -1,65 +1,69 @@
package seng302.models.parsers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.Socket;
public class InputStreamParser {
//changed the currentline variable from sring to long in order to check it's value
// private static String currentLine;
private static long currentLine;
private static BufferedReader buffer = null;
private static InputStream stream = null;
private static boolean reading = true;
private static void readLine() {
private static void skipBytes(int n){
for (int i=0; i < n; i++){
readByte();
}
}
private static int readByte() {
int currentByte = -1;
try {
//Rather than read strings it reads a long which is used for checking the head
// currentLine = buffer.readline();
currentLine = buffer.read();
currentByte = stream.read();
if (currentByte == -1){
reading = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return currentByte;
}
private static void runTest() {
Socket host = null;
String hostAddress = "csse-s302staff.canterbury.ac.nz";
String hostAddress = "livedata.americascup.com";
int hostPort = 4941;
try {
host = new Socket(hostAddress, hostPort);
} catch (IOException e) {
e.printStackTrace();
}
try {
//buffer = new DataInputStream(host.getInputStream());
if (host != null) {
buffer = new BufferedReader(new InputStreamReader(host.getInputStream()));
stream = host.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
readLine();
boolean reading = true;
long prev = 0;
long len = 0;
int sync1;
int sync2;
//currently "reading" will not break the program nicely (because there are multiple readBytes within the while loop)
while(reading) {
// System.out.println(currentLine);
readLine();
sync1 = readByte();
sync2 = readByte();
//checking if it is the start of the packet
if(prev == 71 && currentLine == 65533) {
System.out.println("PACKET LENGTH: " + (len));
len = 0;
}
len += 1;
prev = currentLine;
if (currentLine == -1) {
reading = false;
if(sync1 == 0x47 && sync2 == 0x83) {
System.out.println("message type: " + readByte());
skipBytes(10);
byte[] b = new byte[2];
try {
stream.read(b);
} catch (IOException e){
e.printStackTrace();
}
int payloadLength = bytesToInt(b);
System.out.println("payload length: " + payloadLength);
skipBytes(payloadLength);
skipBytes(4);
}
}
@@ -74,6 +78,23 @@ public class InputStreamParser {
}
/**
* takes an array of up to 4 bytes and returns and int
* @return an int if there is less than 4 bytes -1 otherwise
*/
private static int bytesToInt(byte[] bytes){
int partialInt = 0;
int index = 0;
for (byte b: bytes){
if (index > 3){
return -1;
}
partialInt = partialInt | (b & 0xFF) << (index * 8);
index++;
}
return partialInt;
}
public static void main(String[] args) {
runTest();