|
|
|
|
@ -17,13 +17,12 @@ import static seng302.Networking.Utils.MessageType.*;
|
|
|
|
|
/**
|
|
|
|
|
* TCP server to act as the mock AC35 streaming interface
|
|
|
|
|
*/
|
|
|
|
|
public class VisualiserInput
|
|
|
|
|
public class VisualiserInput implements Runnable
|
|
|
|
|
{
|
|
|
|
|
//time since last heartbeat
|
|
|
|
|
private long lastHeartbeatTime;
|
|
|
|
|
|
|
|
|
|
//socket port 4945 as 4940 is ac35 live port and 4941 is ac35 test port
|
|
|
|
|
private ServerSocket visualiserSocket;
|
|
|
|
|
private Socket connectionSocket;
|
|
|
|
|
|
|
|
|
|
long heartbeatSeqNum;
|
|
|
|
|
@ -32,91 +31,14 @@ public class VisualiserInput
|
|
|
|
|
|
|
|
|
|
VisualiserInput() throws IOException{
|
|
|
|
|
|
|
|
|
|
Socket connectionSocket = new Socket(InetAddress.getLocalHost(), 4942);
|
|
|
|
|
// connectionSocket = new Socket(InetAddress.getLocalHost(), 4942);
|
|
|
|
|
|
|
|
|
|
//this is the test data that streams form the AC35 website
|
|
|
|
|
// Socket connectionSocket = new Socket("livedata.americascup.com",4941);
|
|
|
|
|
connectionSocket = new Socket("livedata.americascup.com",4941);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//start Time
|
|
|
|
|
lastHeartbeatTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//receiver loop that gets the input
|
|
|
|
|
boolean receiverLoop = true;
|
|
|
|
|
while(receiverLoop) {
|
|
|
|
|
//gets the input from the socket
|
|
|
|
|
InputStream inFromClient = connectionSocket.getInputStream();
|
|
|
|
|
|
|
|
|
|
//converts the input into a byte array that can be read by the decoder
|
|
|
|
|
byte[] binaryMessage = getBytes(inFromClient);
|
|
|
|
|
|
|
|
|
|
//decode the binary message into readable date
|
|
|
|
|
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(binaryMessage);
|
|
|
|
|
AC35Data data = testDecoder.decode();
|
|
|
|
|
if (data == null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//checks which message is being received and does what is needed for that message
|
|
|
|
|
MessageType mType = data.getType();
|
|
|
|
|
switch (mType) {
|
|
|
|
|
case HEARTBEAT:
|
|
|
|
|
lastHeartbeatTime = System.currentTimeMillis();
|
|
|
|
|
//note: if the program runs for over 340 years, this will crash.
|
|
|
|
|
heartbeatSeqNum = ByteConverter.bytesToLong(testDecoder.getMessage());
|
|
|
|
|
System.out.println("HeartBeat Message! " + heartbeatSeqNum);
|
|
|
|
|
break;
|
|
|
|
|
case RACESTATUS:
|
|
|
|
|
System.out.println("Race Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case DISPLAYTEXTMESSAGE:
|
|
|
|
|
System.out.println("Display Text Message");
|
|
|
|
|
//no decoder for this.
|
|
|
|
|
break;
|
|
|
|
|
case XMLMESSAGE:
|
|
|
|
|
System.out.println("XML Message!");
|
|
|
|
|
System.out.println(((XMLMessage)data).getXmlMessage());
|
|
|
|
|
break;
|
|
|
|
|
case RACESTARTSTATUS:
|
|
|
|
|
System.out.println("Race Start Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case YACHTEVENTCODE:
|
|
|
|
|
System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case YACHTACTIONCODE:
|
|
|
|
|
System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case CHATTERTEXT:
|
|
|
|
|
System.out.println("Chatter Text Message!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case BOATLOCATION:
|
|
|
|
|
System.out.println("Boat Location Message!");
|
|
|
|
|
break;
|
|
|
|
|
case MARKROUNDING:
|
|
|
|
|
System.out.println("Mark Rounding Message!");
|
|
|
|
|
break;
|
|
|
|
|
case COURSEWIND:
|
|
|
|
|
System.out.println("Course Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
case AVGWIND:
|
|
|
|
|
System.out.println("Average Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("Broken Message!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if no heartbeat has been received in more than 6 seconds
|
|
|
|
|
//the connection will need to be restarted
|
|
|
|
|
if (timeSinceHeartbeat() > 6){
|
|
|
|
|
System.out.println("Connection has stopped, trying to reconnect");
|
|
|
|
|
receiverLoop = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -148,9 +70,92 @@ public class VisualiserInput
|
|
|
|
|
return binaryMessageBytes.array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
|
try{
|
|
|
|
|
//receiver loop that gets the input
|
|
|
|
|
boolean receiverLoop = true;
|
|
|
|
|
while(receiverLoop) {
|
|
|
|
|
//gets the input from the socket
|
|
|
|
|
InputStream inFromClient = connectionSocket.getInputStream();
|
|
|
|
|
|
|
|
|
|
//converts the input into a byte array that can be read by the decoder
|
|
|
|
|
byte[] binaryMessage = getBytes(inFromClient);
|
|
|
|
|
|
|
|
|
|
//decode the binary message into readable date
|
|
|
|
|
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(binaryMessage);
|
|
|
|
|
AC35Data data = testDecoder.decode();
|
|
|
|
|
if (data == null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//checks which message is being received and does what is needed for that message
|
|
|
|
|
MessageType mType = data.getType();
|
|
|
|
|
switch (mType) {
|
|
|
|
|
case HEARTBEAT:
|
|
|
|
|
lastHeartbeatTime = System.currentTimeMillis();
|
|
|
|
|
//note: if the program runs for over 340 years, this will crash.
|
|
|
|
|
heartbeatSeqNum = ByteConverter.bytesToLong(testDecoder.getMessage());
|
|
|
|
|
System.out.println("HeartBeat Message! " + heartbeatSeqNum);
|
|
|
|
|
break;
|
|
|
|
|
case RACESTATUS:
|
|
|
|
|
// System.out.println("Race Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case DISPLAYTEXTMESSAGE:
|
|
|
|
|
// System.out.println("Display Text Message");
|
|
|
|
|
//no decoder for this.
|
|
|
|
|
break;
|
|
|
|
|
case XMLMESSAGE:
|
|
|
|
|
// System.out.println("XML Message!");
|
|
|
|
|
System.out.println(((XMLMessage)data).getXmlMessage());
|
|
|
|
|
break;
|
|
|
|
|
case RACESTARTSTATUS:
|
|
|
|
|
// System.out.println("Race Start Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case YACHTEVENTCODE:
|
|
|
|
|
// System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case YACHTACTIONCODE:
|
|
|
|
|
// System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case CHATTERTEXT:
|
|
|
|
|
// System.out.println("Chatter Text Message!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case BOATLOCATION:
|
|
|
|
|
// System.out.println("Boat Location Message!");
|
|
|
|
|
break;
|
|
|
|
|
case MARKROUNDING:
|
|
|
|
|
// System.out.println("Mark Rounding Message!");
|
|
|
|
|
break;
|
|
|
|
|
case COURSEWIND:
|
|
|
|
|
// System.out.println("Course Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
case AVGWIND:
|
|
|
|
|
// System.out.println("Average Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// System.out.println("Broken Message!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if no heartbeat has been received in more than 6 seconds
|
|
|
|
|
//the connection will need to be restarted
|
|
|
|
|
if (timeSinceHeartbeat() > 6){
|
|
|
|
|
System.out.println("Connection has stopped, trying to reconnect");
|
|
|
|
|
receiverLoop = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String argv[]) throws Exception
|
|
|
|
|
{
|
|
|
|
|
VisualiserInput reciever = new VisualiserInput();
|
|
|
|
|
reciever.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|