Reading relevant information (boats and race related info) from the stream so it can be moved to a parse and turned into objects for the actual race.

#story[30a]
This commit is contained in:
kre39
2017-04-23 14:42:10 +12:00
parent 247560ee43
commit a649b11bbf
@@ -1,7 +1,9 @@
package seng302.models.parsers; package seng302.models.parsers;
import java.io.BufferedReader;
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;
@@ -9,6 +11,9 @@ public class InputStreamParser {
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(int n){
for (int i=0; i < n; i++){ for (int i=0; i < n; i++){
@@ -16,6 +21,15 @@ public class InputStreamParser {
} }
} }
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 {
@@ -29,10 +43,11 @@ public class InputStreamParser {
return currentByte; return currentByte;
} }
private static void runTest() { private static void runPacketLengthTest() {
Socket host = null; Socket host = null;
String hostAddress = "livedata.americascup.com"; String hostAddress = "csse-s302staff.canterbury.ac.nz";
// String hostAddress = "livedata.americascup.com";
int hostPort = 4941; int hostPort = 4941;
try { try {
@@ -78,6 +93,61 @@ public class InputStreamParser {
} }
private static void runParserTest() {
Socket host = null;
String hostAddress = "csse-s302staff.canterbury.ac.nz";
int hostPort = 4941;
try {
host = new Socket(hostAddress, hostPort);
} catch (IOException e) {
e.printStackTrace();
}
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 and int
* @return an int if there is less than 4 bytes -1 otherwise * @return an int if there is less than 4 bytes -1 otherwise
@@ -97,8 +167,9 @@ public class InputStreamParser {
public static void main(String[] args) { public static void main(String[] args) {
runTest(); runPacketLengthTest();
runParserTest();
} }
} }