|
|
|
@ -1,6 +1,12 @@
|
|
|
|
package seng302;
|
|
|
|
package seng302;
|
|
|
|
|
|
|
|
import seng302.Networking.BinaryMessageDecoder;
|
|
|
|
|
|
|
|
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.net.*;
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* TCP server to act as the mock AC35 streaming interface
|
|
|
|
* TCP server to act as the mock AC35 streaming interface
|
|
|
|
@ -9,21 +15,60 @@ public class TCPServer
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public static void main(String argv[]) throws Exception
|
|
|
|
public static void main(String argv[]) throws Exception
|
|
|
|
{
|
|
|
|
{
|
|
|
|
String clientSentence;
|
|
|
|
|
|
|
|
String capitalizedSentence;
|
|
|
|
|
|
|
|
//socket port 4942 as 4940 is ac35 live port and 4941 is ac35 test port
|
|
|
|
//socket port 4942 as 4940 is ac35 live port and 4941 is ac35 test port
|
|
|
|
ServerSocket welcomeSocket = new ServerSocket(4942);
|
|
|
|
ServerSocket visualiserSocket = new ServerSocket(4942);
|
|
|
|
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Socket connectionSocket = welcomeSocket.accept();
|
|
|
|
Socket connectionSocket = visualiserSocket.accept();
|
|
|
|
BufferedReader inFromClient =
|
|
|
|
InputStream inFromClient = connectionSocket.getInputStream();
|
|
|
|
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
|
|
|
|
byte[] binaryMessage = getBytes(inFromClient);
|
|
|
|
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
|
|
|
|
|
|
|
|
clientSentence = inFromClient.readLine();
|
|
|
|
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(binaryMessage);
|
|
|
|
System.out.println("Received: " + clientSentence);
|
|
|
|
testDecoder.decode();
|
|
|
|
capitalizedSentence = clientSentence.toUpperCase() + '\n';
|
|
|
|
System.out.println("--header--");
|
|
|
|
outToClient.writeBytes(capitalizedSentence);
|
|
|
|
System.out.println(testDecoder.getMessageType());
|
|
|
|
|
|
|
|
System.out.println(testDecoder.getTimeStamp());
|
|
|
|
|
|
|
|
System.out.println(testDecoder.getSourceID());
|
|
|
|
|
|
|
|
System.out.println(testDecoder.getMessageLength());
|
|
|
|
|
|
|
|
System.out.println("----");
|
|
|
|
|
|
|
|
XMLMessageDecoder xmlMessageDecoder = new XMLMessageDecoder(testDecoder.getMessage());
|
|
|
|
|
|
|
|
xmlMessageDecoder.decode();
|
|
|
|
|
|
|
|
System.out.println("--message--");
|
|
|
|
|
|
|
|
System.out.println(xmlMessageDecoder.getXmlMessageInputSource());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
System.out.println(i);
|
|
|
|
|
|
|
|
byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15);
|
|
|
|
|
|
|
|
short messageLen = bytesToShort(messageLenBytes);
|
|
|
|
|
|
|
|
System.out.println("m " + messageLen);
|
|
|
|
|
|
|
|
byte[] messageBytesWithCRC = new byte[messageLen+4];
|
|
|
|
|
|
|
|
int j = inStream.read(messageBytesWithCRC);
|
|
|
|
|
|
|
|
System.out.println(j);
|
|
|
|
|
|
|
|
ByteBuffer binaryMessageBytes = ByteBuffer.allocate(i+j);
|
|
|
|
|
|
|
|
binaryMessageBytes.put(headerBytes);
|
|
|
|
|
|
|
|
binaryMessageBytes.put(messageBytesWithCRC);
|
|
|
|
|
|
|
|
return binaryMessageBytes.array();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static short bytesToShort(byte[] bytesShort){
|
|
|
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
|
|
|
|
|
|
|
byteBuffer.put(bytesShort[0]);
|
|
|
|
|
|
|
|
byteBuffer.put(bytesShort[1]);
|
|
|
|
|
|
|
|
short shortVal = byteBuffer.getShort(0);
|
|
|
|
|
|
|
|
return shortVal;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|