|
|
|
|
@ -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,16 +31,47 @@ 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* calculates the time since last heartbeat
|
|
|
|
|
* @return time since last heartbeat
|
|
|
|
|
*/
|
|
|
|
|
private double timeSinceHeartbeat() {
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
return (now - lastHeartbeatTime) / 1000.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes an inputStream and reads the first 15 bytes (the header) and gets the message length
|
|
|
|
|
* for the whole message then reads that and returns the byte array
|
|
|
|
|
* @param inStream inputStream from socket
|
|
|
|
|
* @return encoded binary messsage bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private static byte[] getBytes(InputStream inStream) throws IOException {
|
|
|
|
|
byte[] headerBytes = new byte[15];
|
|
|
|
|
int i = inStream.read(headerBytes);
|
|
|
|
|
byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15);
|
|
|
|
|
short messageLen = bytesToShort(messageLenBytes);
|
|
|
|
|
byte[] messageBytesWithCRC = new byte[messageLen+4];
|
|
|
|
|
int j = inStream.read(messageBytesWithCRC);
|
|
|
|
|
ByteBuffer binaryMessageBytes = ByteBuffer.allocate(headerBytes.length+messageBytesWithCRC.length);
|
|
|
|
|
binaryMessageBytes.put(headerBytes);
|
|
|
|
|
binaryMessageBytes.put(messageBytesWithCRC);
|
|
|
|
|
return binaryMessageBytes.array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
|
try{
|
|
|
|
|
//receiver loop that gets the input
|
|
|
|
|
boolean receiverLoop = true;
|
|
|
|
|
while(receiverLoop) {
|
|
|
|
|
@ -68,45 +98,45 @@ public class VisualiserInput
|
|
|
|
|
System.out.println("HeartBeat Message! " + heartbeatSeqNum);
|
|
|
|
|
break;
|
|
|
|
|
case RACESTATUS:
|
|
|
|
|
System.out.println("Race Status Message");
|
|
|
|
|
// System.out.println("Race Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case DISPLAYTEXTMESSAGE:
|
|
|
|
|
System.out.println("Display Text Message");
|
|
|
|
|
// System.out.println("Display Text Message");
|
|
|
|
|
//no decoder for this.
|
|
|
|
|
break;
|
|
|
|
|
case XMLMESSAGE:
|
|
|
|
|
System.out.println("XML Message!");
|
|
|
|
|
// System.out.println("XML Message!");
|
|
|
|
|
System.out.println(((XMLMessage)data).getXmlMessage());
|
|
|
|
|
break;
|
|
|
|
|
case RACESTARTSTATUS:
|
|
|
|
|
System.out.println("Race Start Status Message");
|
|
|
|
|
// System.out.println("Race Start Status Message");
|
|
|
|
|
break;
|
|
|
|
|
case YACHTEVENTCODE:
|
|
|
|
|
System.out.println("Yacht Action Code!");
|
|
|
|
|
// System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case YACHTACTIONCODE:
|
|
|
|
|
System.out.println("Yacht Action Code!");
|
|
|
|
|
// System.out.println("Yacht Action Code!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case CHATTERTEXT:
|
|
|
|
|
System.out.println("Chatter Text Message!");
|
|
|
|
|
// System.out.println("Chatter Text Message!");
|
|
|
|
|
//no decoder
|
|
|
|
|
break;
|
|
|
|
|
case BOATLOCATION:
|
|
|
|
|
System.out.println("Boat Location Message!");
|
|
|
|
|
// System.out.println("Boat Location Message!");
|
|
|
|
|
break;
|
|
|
|
|
case MARKROUNDING:
|
|
|
|
|
System.out.println("Mark Rounding Message!");
|
|
|
|
|
// System.out.println("Mark Rounding Message!");
|
|
|
|
|
break;
|
|
|
|
|
case COURSEWIND:
|
|
|
|
|
System.out.println("Course Wind Message!");
|
|
|
|
|
// System.out.println("Course Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
case AVGWIND:
|
|
|
|
|
System.out.println("Average Wind Message!");
|
|
|
|
|
// System.out.println("Average Wind Message!");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("Broken Message!");
|
|
|
|
|
// System.out.println("Broken Message!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -117,40 +147,15 @@ public class VisualiserInput
|
|
|
|
|
receiverLoop = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* calculates the time since last heartbeat
|
|
|
|
|
* @return time since last heartbeat
|
|
|
|
|
*/
|
|
|
|
|
private double timeSinceHeartbeat() {
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
return (now - lastHeartbeatTime) / 1000.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes an inputStream and reads the first 15 bytes (the header) and gets the message length
|
|
|
|
|
* for the whole message then reads that and returns the byte array
|
|
|
|
|
* @param inStream inputStream from socket
|
|
|
|
|
* @return encoded binary messsage bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private static byte[] getBytes(InputStream inStream) throws IOException {
|
|
|
|
|
byte[] headerBytes = new byte[15];
|
|
|
|
|
int i = inStream.read(headerBytes);
|
|
|
|
|
byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15);
|
|
|
|
|
short messageLen = bytesToShort(messageLenBytes);
|
|
|
|
|
byte[] messageBytesWithCRC = new byte[messageLen+4];
|
|
|
|
|
int j = inStream.read(messageBytesWithCRC);
|
|
|
|
|
ByteBuffer binaryMessageBytes = ByteBuffer.allocate(headerBytes.length+messageBytesWithCRC.length);
|
|
|
|
|
binaryMessageBytes.put(headerBytes);
|
|
|
|
|
binaryMessageBytes.put(messageBytesWithCRC);
|
|
|
|
|
return binaryMessageBytes.array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String argv[]) throws Exception
|
|
|
|
|
{
|
|
|
|
|
VisualiserInput reciever = new VisualiserInput();
|
|
|
|
|
reciever.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|