mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
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:
@@ -1,65 +1,69 @@
|
|||||||
package seng302.models.parsers;
|
package seng302.models.parsers;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
|
|
||||||
public class InputStreamParser {
|
public class InputStreamParser {
|
||||||
|
|
||||||
//changed the currentline variable from sring to long in order to check it's value
|
private static InputStream stream = null;
|
||||||
// private static String currentLine;
|
private static boolean reading = true;
|
||||||
private static long currentLine;
|
|
||||||
private static BufferedReader buffer = null;
|
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
//Rather than read strings it reads a long which is used for checking the head
|
currentByte = stream.read();
|
||||||
// currentLine = buffer.readline();
|
if (currentByte == -1){
|
||||||
currentLine = buffer.read();
|
reading = false;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return currentByte;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void runTest() {
|
private static void runTest() {
|
||||||
|
|
||||||
Socket host = null;
|
Socket host = null;
|
||||||
String hostAddress = "csse-s302staff.canterbury.ac.nz";
|
String hostAddress = "livedata.americascup.com";
|
||||||
int hostPort = 4941;
|
int hostPort = 4941;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
host = new Socket(hostAddress, hostPort);
|
host = new Socket(hostAddress, hostPort);
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
//buffer = new DataInputStream(host.getInputStream());
|
|
||||||
if (host != null) {
|
if (host != null) {
|
||||||
buffer = new BufferedReader(new InputStreamReader(host.getInputStream()));
|
stream = host.getInputStream();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
readLine();
|
int sync1;
|
||||||
boolean reading = true;
|
int sync2;
|
||||||
long prev = 0;
|
//currently "reading" will not break the program nicely (because there are multiple readBytes within the while loop)
|
||||||
long len = 0;
|
|
||||||
while(reading) {
|
while(reading) {
|
||||||
// System.out.println(currentLine);
|
sync1 = readByte();
|
||||||
readLine();
|
sync2 = readByte();
|
||||||
//checking if it is the start of the packet
|
//checking if it is the start of the packet
|
||||||
if(prev == 71 && currentLine == 65533) {
|
if(sync1 == 0x47 && sync2 == 0x83) {
|
||||||
System.out.println("PACKET LENGTH: " + (len));
|
System.out.println("message type: " + readByte());
|
||||||
len = 0;
|
skipBytes(10);
|
||||||
}
|
byte[] b = new byte[2];
|
||||||
len += 1;
|
try {
|
||||||
prev = currentLine;
|
stream.read(b);
|
||||||
if (currentLine == -1) {
|
} catch (IOException e){
|
||||||
reading = false;
|
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) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
runTest();
|
runTest();
|
||||||
|
|||||||
Reference in New Issue
Block a user