Did some large tidy ups, refactoring, and documenting.

Network:
Moved the Network message classes from the Utils package to the Messages package.
Renamed BoatLocationMessage to BoatLocation and BoatStatusMessage to BoatStatus to be consistent with other message classes.
Renamed the BoatStatus enumeration to BoatStatusEnum as it conflicted with BoatStatus (the message).
Moved the BoatStatusEnum and MessageType enumerations from the Utils package to the Messages/Enums package.
Changed the BoatStatusEnum and MessageType enumerations to use map look ups in the enum.fromByte(b) method - this means that there's less copy and pasted values, and the fromByte function doesn't need to be modified if new enumerations are added.
Added a sequenceNumber member to the Heartbeat class.
Added an InvalidMessageException in the package Networking/Exceptions. This is thrown when a message is read, but it is invalid in some way.
Refactored/tidied up the Networking/BinaryMessageEncoder and Decoder classes. The decoder throws InvalidMessageExceptions instead of returning null.

Visualiser:
VisualiserInput now wraps a DataInputStream around the socket. This provides the stream.readFully(buffer) function, which is a blocking read, removing the need for busy wait loops. Replaced the getBytes() function with getNextMessage() and getNextMessageBytes(). These read the next message from the socket, and return it as a message object and a byte array, respectively.
Changed the current heartbeat timeout to 10 seconds. Added some work-in-progress code to attempt to reconnect when connection is lost. It currently doesn't work. I think Fan-Wu was doing a proper implementation, however.
VisualiserInput also has a queue of received events. Currently not really used, but could be useful in the future. Events get added as they are read.
Changed VisualiserInputs main loop to use instanceOf instead of switch+case. Feedback wanted - are there any downsides to using this instanceOf method?

#story[778,782]
This commit is contained in:
fjc40
2017-05-08 03:29:33 +12:00
parent 466e22437b
commit b247244665
38 changed files with 1067 additions and 600 deletions
@@ -2,9 +2,12 @@ package seng302.Networking;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.Exceptions.InvalidMessageException;
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
import seng302.Networking.Utils.MessageType;
import seng302.Networking.Messages.AC35Data;
import seng302.Networking.Messages.Enums.MessageType;
import seng302.Networking.Messages.XMLMessage;
import java.io.*;
@@ -30,19 +33,32 @@ public class BinaryMessageDecoderTest {
BinaryMessageEncoder testMessage = new BinaryMessageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
testDecoder.decode();
BinaryMessageDecoder decoder = new BinaryMessageDecoder(testMessage.getFullMessage());
AC35Data message = null;
try {
message = decoder.decode();
}
catch (InvalidMessageException e) {
Assert.assertFalse(e.getMessage(), true);
}
if (!(message instanceof XMLMessage)){
Assert.assertFalse(true);
}
XMLMessage xmlMessage = (XMLMessage) message;
//message length
Assert.assertEquals((short) encodedMessage.length,testDecoder.getMessageLength());
Assert.assertEquals((short) encodedMessage.length, decoder.getMessageBodyLength());
//time stamp
Assert.assertEquals(time,testDecoder.getTimeStamp());
Assert.assertEquals(time, decoder.getHeaderTimeStamp());
//source ID
Assert.assertEquals((short) 1, testDecoder.getSourceID());
Assert.assertEquals((short) 1, decoder.getHeaderSourceID());
//message type
Assert.assertEquals(26, testDecoder.getMessageType());
Assert.assertEquals(26, decoder.getHeaderMessageType());
XMLMessageDecoder decoderXML = new XMLMessageDecoder(testDecoder.getMessage());
XMLMessageDecoder decoderXML = new XMLMessageDecoder(decoder.getMessageBody());
decoderXML.decode();
//tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
@@ -62,7 +78,7 @@ public class BinaryMessageDecoderTest {
// Assert.assertEquals(xmlString.toString(), contents);
}catch (IOException e){
e.printStackTrace();
Assert.assertFalse(e.getMessage(), true);
}
}
}
@@ -2,7 +2,7 @@ package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.Utils.BoatLocationMessage;
import seng302.Networking.Messages.BoatLocation;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
@@ -13,7 +13,7 @@ public class BoatLocationDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
BoatLocationMessage testMessage = new BoatLocationMessage((byte) 1, time, 2,
BoatLocation testMessage = new BoatLocation((byte) 1, time, 2,
3, (byte) 1, 180, -180, 4, 5,
(short) 6, (short) 7, 8, 9, 10, 11,
(short) 12, 13, 14 , (short) 15,
@@ -22,7 +22,7 @@ public class BoatLocationDecoderTest {
byte [] testEncodedMessage = raceVisionByteEncoder.boatLocation(testMessage);
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
BoatLocationMessage decodedTest = testDecoder.getMessage();
BoatLocation decodedTest = testDecoder.getMessage();
Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber());
Assert.assertEquals(testMessage.getTime(), decodedTest.getTime());
@@ -2,7 +2,7 @@ package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.Utils.CourseWind;
import seng302.Networking.Messages.CourseWind;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import java.util.ArrayList;
@@ -3,8 +3,8 @@ package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import seng302.Networking.Utils.BoatStatusMessage;
import seng302.Networking.Utils.RaceStatus;
import seng302.Networking.Messages.BoatStatus;
import seng302.Networking.Messages.RaceStatus;
import java.util.ArrayList;
@@ -32,8 +32,8 @@ public class RaceStatusDecoderTest {
long boat1TimeAtFinish = boat1TimeAtNextMark + (1000 * 15);
long boat2TimeAtFinish = boat2TimeAtNextMark + (1000 * 7);
BoatStatusMessage boatStatusMessage1 = new BoatStatusMessage(boat1SourceID, boat1Status, boat1LegNumber, boat1PenaltiesAwarded, boat1PenaltiesServed, boat1TimeAtNextMark, boat1TimeAtFinish);
BoatStatusMessage boatStatusMessage2 = new BoatStatusMessage(boat2SourceID, boat2Status, boat2LegNumber, boat2PenaltiesAwarded, boat2PenaltiesServed, boat2TimeAtNextMark, boat2TimeAtFinish);
BoatStatus boatStatus1 = new BoatStatus(boat1SourceID, boat1Status, boat1LegNumber, boat1PenaltiesAwarded, boat1PenaltiesServed, boat1TimeAtNextMark, boat1TimeAtFinish);
BoatStatus boatStatus2 = new BoatStatus(boat2SourceID, boat2Status, boat2LegNumber, boat2PenaltiesAwarded, boat2PenaltiesServed, boat2TimeAtNextMark, boat2TimeAtFinish);
int raceID = 585;
int raceStatus = 3;
@@ -41,11 +41,11 @@ public class RaceStatusDecoderTest {
int windDirection = 2341;
int windSpeed = 10201;
int raceType = 1;
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<>(2);
boatStatusMessages.add(boatStatusMessage1);
boatStatusMessages.add(boatStatusMessage2);
ArrayList<BoatStatus> boatStatuses = new ArrayList<>(2);
boatStatuses.add(boatStatus1);
boatStatuses.add(boatStatus2);
RaceStatus raceStatusObject = new RaceStatus(time, raceID, raceStatus, raceStartTime, windDirection, windSpeed, raceType, boatStatusMessages);
RaceStatus raceStatusObject = new RaceStatus(time, raceID, raceStatus, raceStartTime, windDirection, windSpeed, raceType, boatStatuses);
byte[] encodedRaceStatus = RaceVisionByteEncoder.raceStatus(raceStatusObject);
@@ -61,7 +61,7 @@ public class RaceStatusDecoderTest {
Assert.assertEquals(windSpeed, decoderTest.getRaceWindSpeed());
BoatStatusMessage boat1 = decoderTest.getBoats().get(0);
BoatStatus boat1 = decoderTest.getBoats().get(0);
Assert.assertEquals(boat1SourceID, boat1.getSourceID());
Assert.assertEquals(boat1Status, boat1.getBoatStatus());