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:
@@ -4,10 +4,10 @@ package seng302;
|
||||
import seng302.Networking.BinaryMessageEncoder;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
import seng302.Networking.Utils.RaceStatus;
|
||||
import seng302.Networking.Utils.XMLMessage;
|
||||
import seng302.Networking.Messages.BoatLocation;
|
||||
import seng302.Networking.Messages.Enums.MessageType;
|
||||
import seng302.Networking.Messages.RaceStatus;
|
||||
import seng302.Networking.Messages.XMLMessage;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@@ -110,12 +110,12 @@ public class MockOutput implements Runnable
|
||||
*/
|
||||
public synchronized void parseBoatLocation(int sourceID, double lat, double lon, double heading, double speed){
|
||||
|
||||
BoatLocationMessage boatLocationMessage = new BoatLocationMessage(sourceID, lat, lon, boatLocationSequenceNumber, heading, speed);
|
||||
BoatLocation boatLocation = new BoatLocation(sourceID, lat, lon, boatLocationSequenceNumber, heading, speed);
|
||||
//iterates the sequence number
|
||||
boatLocationSequenceNumber++;
|
||||
|
||||
//encodeds the messages
|
||||
byte[] encodedBoatLoc = RaceVisionByteEncoder.boatLocation(boatLocationMessage);
|
||||
byte[] encodedBoatLoc = RaceVisionByteEncoder.boatLocation(boatLocation);
|
||||
|
||||
//encodeds the full message with header
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(MessageType.BOATLOCATION, System.currentTimeMillis(), messageNumber, (short)encodedBoatLoc.length,
|
||||
|
||||
@@ -7,13 +7,12 @@ import javafx.collections.ObservableList;
|
||||
import org.geotools.referencing.GeodeticCalculator;
|
||||
|
||||
|
||||
import org.joda.time.tz.UTCProvider;
|
||||
import seng302.Constants;
|
||||
import seng302.DataInput.RaceDataSource;
|
||||
import seng302.MockOutput;
|
||||
import seng302.Networking.Utils.BoatStatusMessage;
|
||||
import seng302.Networking.Utils.Enums.BoatStatus;
|
||||
import seng302.Networking.Utils.RaceStatus;
|
||||
import seng302.Networking.Messages.BoatStatus;
|
||||
import seng302.Networking.Messages.Enums.BoatStatusEnum;
|
||||
import seng302.Networking.Messages.RaceStatus;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.ArrayList;
|
||||
@@ -122,20 +121,20 @@ public class Race implements Runnable {
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
timeLeft = startTime - currentTime;
|
||||
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<>();
|
||||
ArrayList<BoatStatus> boatStatuses = new ArrayList<>();
|
||||
//For each boat, we update it's position, and generate a BoatLocationMessage.
|
||||
for (int i = 0; i < startingBoats.size(); i++) {
|
||||
Boat boat = startingBoats.get((i + boatOffset) % startingBoats.size());
|
||||
if (boat != null) {
|
||||
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), 0);
|
||||
boatStatusMessages.add(new BoatStatusMessage(boat.getSourceID(),
|
||||
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatus.RACING : BoatStatus.DNF, boat.getCurrentLeg().getLegNumber()));
|
||||
boatStatuses.add(new BoatStatus(boat.getSourceID(),
|
||||
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
|
||||
}
|
||||
}
|
||||
boatOffset = (boatOffset + 1) % (startingBoats.size());
|
||||
|
||||
if (timeLeft <= 60000/scaleFactor && timeLeft > 0) {
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 2, startTime, 0, 2300, 1, boatStatusMessages);
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 2, startTime, 0, 2300, 1, boatStatuses);
|
||||
mockOutput.parseRaceStatus(raceStatus);
|
||||
}
|
||||
else if (timeLeft <= 0) {
|
||||
@@ -146,7 +145,7 @@ public class Race implements Runnable {
|
||||
stop();
|
||||
}
|
||||
else {
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 1, startTime, 0, 2300,1, boatStatusMessages);
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 1, startTime, 0, 2300,1, boatStatuses);
|
||||
mockOutput.parseRaceStatus(raceStatus);
|
||||
}
|
||||
currentTime = System.currentTimeMillis();
|
||||
@@ -179,7 +178,7 @@ public class Race implements Runnable {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
//Update the total elapsed time.
|
||||
totalTimeElapsed = currentTime - timeRaceStarted;
|
||||
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<BoatStatusMessage>();
|
||||
ArrayList<BoatStatus> boatStatuses = new ArrayList<BoatStatus>();
|
||||
finished = 0;
|
||||
//For each boat, we update it's position, and generate a BoatLocationMessage.
|
||||
for (int i = 0; i < startingBoats.size(); i++) {
|
||||
@@ -192,15 +191,15 @@ public class Race implements Runnable {
|
||||
}
|
||||
if (boat.getTimeFinished() > 0) {
|
||||
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getVelocity());
|
||||
boatStatusMessages.add(new BoatStatusMessage(boat.getSourceID(), BoatStatus.FINISHED, boat.getCurrentLeg().getLegNumber()));
|
||||
boatStatuses.add(new BoatStatus(boat.getSourceID(), BoatStatusEnum.FINISHED, boat.getCurrentLeg().getLegNumber()));
|
||||
finished++;
|
||||
} else {
|
||||
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getVelocity());
|
||||
boatStatusMessages.add(new BoatStatusMessage(boat.getSourceID(),
|
||||
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatus.RACING : BoatStatus.DNF, boat.getCurrentLeg().getLegNumber()));
|
||||
boatStatuses.add(new BoatStatus(boat.getSourceID(),
|
||||
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
|
||||
}
|
||||
if (startingBoats.size()==finished){
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, 0, 2300, 2, boatStatusMessages);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, 0, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
|
||||
mockOutput.parseRaceStatus(raceStatus);
|
||||
}
|
||||
} else {
|
||||
@@ -208,7 +207,7 @@ public class Race implements Runnable {
|
||||
}
|
||||
}
|
||||
boatOffset = (boatOffset + 1) % (startingBoats.size());
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, 0, 2300, 2, boatStatusMessages);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, 0, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
|
||||
mockOutput.parseRaceStatus(raceStatus);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user