WIP. Probably need to cherry pick stuff out of here.
Added ClientConnection and server-side handshake. Added MessageSerialiser and Deserialiser. #story[1095]
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
package network.StreamRelated;
|
||||
|
||||
|
||||
import network.BinaryMessageDecoder;
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.AC35Data;
|
||||
import shared.model.RunnableWithFramePeriod;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static network.Utils.ByteConverter.bytesToShort;
|
||||
|
||||
/**
|
||||
* This class is responsible for converting data from an input stream into a queue of {@link AC35Data} messages.
|
||||
*/
|
||||
public class MessageDeserialiser implements RunnableWithFramePeriod {
|
||||
|
||||
|
||||
/**
|
||||
* The stream we're reading from.
|
||||
*/
|
||||
private DataInputStream inputStream;
|
||||
|
||||
/**
|
||||
* The messages we've read.
|
||||
*/
|
||||
private BlockingQueue<AC35Data> messagesRead;
|
||||
|
||||
|
||||
/**
|
||||
* Ack numbers used in messages.
|
||||
*/
|
||||
private int ackNumber = 1;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new MessageSerialiser to write a queue of messages to a given stream.
|
||||
* @param inputStream The stream to write to.
|
||||
* @param messagesRead The messages to send.
|
||||
*/
|
||||
public MessageDeserialiser(InputStream inputStream, BlockingQueue<AC35Data> messagesRead) {
|
||||
this.inputStream = new DataInputStream(inputStream);
|
||||
this.messagesRead = messagesRead;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increments the ackNumber value, and returns it.
|
||||
* @return Incremented ackNumber.
|
||||
*/
|
||||
private int getNextAckNumber(){
|
||||
this.ackNumber++;
|
||||
|
||||
return this.ackNumber;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Reads and returns the next message as an array of bytes from the input stream. Use getNextMessage() to get the actual message object instead.
|
||||
* @return Encoded binary message bytes.
|
||||
* @throws IOException Thrown when an error occurs while reading from the input stream.
|
||||
*/
|
||||
private byte[] getNextMessageBytes() throws IOException {
|
||||
inputStream.mark(0);
|
||||
short CRCLength = 4;
|
||||
short headerLength = 15;
|
||||
|
||||
//Read the header of the next message.
|
||||
byte[] headerBytes = new byte[headerLength];
|
||||
inputStream.readFully(headerBytes);
|
||||
|
||||
//Read the message body length.
|
||||
byte[] messageBodyLengthBytes = Arrays.copyOfRange(headerBytes, headerLength - 2, headerLength);
|
||||
short messageBodyLength = bytesToShort(messageBodyLengthBytes);
|
||||
|
||||
//Read the message body.
|
||||
byte[] messageBodyBytes = new byte[messageBodyLength];
|
||||
inputStream.readFully(messageBodyBytes);
|
||||
|
||||
//Read the message CRC.
|
||||
byte[] messageCRCBytes = new byte[CRCLength];
|
||||
inputStream.readFully(messageCRCBytes);
|
||||
|
||||
//Put the head + body + crc into one large array.
|
||||
ByteBuffer messageBytes = ByteBuffer.allocate(headerBytes.length + messageBodyBytes.length + messageCRCBytes.length);
|
||||
messageBytes.put(headerBytes);
|
||||
messageBytes.put(messageBodyBytes);
|
||||
messageBytes.put(messageCRCBytes);
|
||||
|
||||
return messageBytes.array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads and returns the next message object from the input stream.
|
||||
* @return The message object.
|
||||
* @throws IOException Thrown when an error occurs while reading from the input stream.
|
||||
* @throws InvalidMessageException Thrown when the message is invalid in some way.
|
||||
*/
|
||||
private AC35Data getNextMessage() throws IOException, InvalidMessageException
|
||||
{
|
||||
//Get the next message from the socket as a block of bytes.
|
||||
byte[] messageBytes = this.getNextMessageBytes();
|
||||
|
||||
//Decode the binary message into an appropriate message object.
|
||||
BinaryMessageDecoder decoder = new BinaryMessageDecoder(messageBytes);
|
||||
|
||||
return decoder.decode();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
|
||||
|
||||
long currentFrameTime = System.currentTimeMillis();
|
||||
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
||||
previousFrameTime = currentFrameTime;
|
||||
|
||||
|
||||
//Reads the next message.
|
||||
try {
|
||||
AC35Data message = this.getNextMessage();
|
||||
messagesRead.add(message);
|
||||
}
|
||||
catch (InvalidMessageException | IOException e) {
|
||||
|
||||
Logger.getGlobal().log(Level.WARNING, "Unable to read message.", e);
|
||||
|
||||
try {
|
||||
inputStream.reset();
|
||||
} catch (IOException e1) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Unable to reset inputStream.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package network.StreamRelated;
|
||||
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.AC35Data;
|
||||
import shared.model.RunnableWithFramePeriod;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This class is responsible for writing a queue of {@link network.Messages.AC35Data} messages to an output stream.
|
||||
*/
|
||||
public class MessageSerialiser implements RunnableWithFramePeriod {
|
||||
|
||||
|
||||
/**
|
||||
* The stream we're writing to.
|
||||
*/
|
||||
private DataOutputStream outputStream;
|
||||
|
||||
/**
|
||||
* The messages we're writing to the stream.
|
||||
*/
|
||||
private BlockingQueue<AC35Data> messagesToSend;
|
||||
|
||||
|
||||
/**
|
||||
* Ack numbers used in messages.
|
||||
*/
|
||||
private int ackNumber = 1;
|
||||
|
||||
/**
|
||||
* Determines whether or not this runnable is currently running.
|
||||
*/
|
||||
private boolean isRunning;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new MessageSerialiser to write a queue of messages to a given stream.
|
||||
* @param outputStream The stream to write to.
|
||||
* @param messagesToSend The messages to send.
|
||||
*/
|
||||
public MessageSerialiser(OutputStream outputStream, BlockingQueue<AC35Data> messagesToSend) {
|
||||
this.outputStream = new DataOutputStream(outputStream);
|
||||
this.messagesToSend = messagesToSend;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increments the ackNumber value, and returns it.
|
||||
* @return Incremented ackNumber.
|
||||
*/
|
||||
private int getNextAckNumber(){
|
||||
this.ackNumber++;
|
||||
|
||||
return this.ackNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not this runnable is running.
|
||||
* @return True means that it is still running, false means that it has stopped.
|
||||
*/
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
|
||||
isRunning = true;
|
||||
|
||||
while (isRunning) {
|
||||
|
||||
|
||||
long currentFrameTime = System.currentTimeMillis();
|
||||
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
||||
previousFrameTime = currentFrameTime;
|
||||
|
||||
|
||||
//Send the messages.
|
||||
List<AC35Data> messages = new ArrayList<>();
|
||||
messagesToSend.drainTo(messages);
|
||||
|
||||
for (AC35Data message : messages) {
|
||||
try {
|
||||
byte[] messageBytes = RaceVisionByteEncoder.encodeBinaryMessage(message, getNextAckNumber());
|
||||
|
||||
outputStream.write(messageBytes);
|
||||
|
||||
|
||||
} catch (InvalidMessageException e) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Could not encode message: " + message, e);
|
||||
|
||||
} catch (IOException e) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Could not write message to outputStream: " + outputStream, e);
|
||||
isRunning = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user