Switching to use networking modules

#story[778]
This commit is contained in:
Erika Savell
2017-04-28 12:43:14 +12:00
parent c95f5377f6
commit b1bbef814b
6 changed files with 192 additions and 318 deletions
+1 -2
View File
@@ -29,11 +29,10 @@ public class App extends Application {
@Override
public void start(Stage primaryStage) {
try {
OutputStream outputStream = System.out;//TEMP currently using System.out, but should replace this with tcp socket we are sending over.
RaceDataSource raceData = new RaceXMLReader("raceXML/bermuda_AC35.xml");
RegattaDataSource regattaData = new RegattaXMLReader("mockXML/regattaTest.xml");
Event raceEvent = new Event(raceData, regattaData, outputStream);
Event raceEvent = new Event(raceData, regattaData);
raceEvent.start();
} catch (IOException e) {
e.printStackTrace();
+18 -89
View File
@@ -9,6 +9,7 @@ import seng302.Exceptions.InvalidBoatDataException;
import seng302.Exceptions.InvalidRaceDataException;
import seng302.Exceptions.InvalidRegattaDataException;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
import seng302.Networking.MockOutput;
import seng302.Networking.Utils.XMLMessage;
import java.io.IOException;
@@ -25,20 +26,21 @@ public class Event {
RaceDataSource raceDataSource;
RegattaDataSource regattaDataSource;
///The stream to which we send all data.
private OutputStream outputStream;
//Sequence numbers for XML messages.
private short regattaXMLSequenceNumber = 0;
private short raceXMLSequenceNumber = 0;
private short boatXMLSequenceNumber = 0;
MockOutput mockOutput;
public Event(RaceDataSource raceData, RegattaDataSource regattaData, OutputStream outputStream) {
public Event(RaceDataSource raceData, RegattaDataSource regattaData) {
this.raceDataSource = raceData;
this.regattaDataSource = regattaData;
this.outputStream = outputStream;
try {
mockOutput = new MockOutput();
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
@@ -51,7 +53,7 @@ public class Event {
//System.out.println("RACE STARTING!!\n\n");//TEMP REMOVE debug
Race newRace = new Race(raceDataSource, 15, this.outputStream);
Race newRace = new Race(raceDataSource, 15, mockOutput);
new Thread((newRace)).start();
}
@@ -60,34 +62,9 @@ public class Event {
RegattaData regattaData = new RegattaData(regattaDataSource);
String xmlString = regattaData.createXML();
byte[] xmlStringUTF8 = new byte[0];
try
{
xmlStringUTF8 = xmlString.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new InvalidRegattaDataException();
}
//Create XML message object and serialize.
short ackNumber = 1;//TEMP need a more sensible way of getting ack number. Is it per packet type, or per packet?
XMLMessageEncoder xmlMessageEncoder = new XMLMessageEncoder(ackNumber, System.currentTimeMillis(), XMLMessage.XMLTypeRegatta, getNextRegattaXMLSequenceNumber(), (short) xmlStringUTF8.length, xmlString);
byte[] serializedMessage = xmlMessageEncoder.encode();
//Write it.
try
{
this.outputStream.write(serializedMessage);
}
catch (IOException e)
{
throw new InvalidRegattaDataException();
}
System.out.println("working");
mockOutput.parseXMLString(xmlString, 26);
System.out.println("working");
}
@@ -96,66 +73,18 @@ public class Event {
RaceData raceData = new RaceData(raceDataSource);
//Serialize race data to an XML as a string.
String xmlString = raceData.createXML();
byte[] xmlStringUTF8 = new byte[0];
try
{
xmlStringUTF8 = xmlString.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new InvalidRaceDataException();
}
//Create XML message object and serialize.
short ackNumber = 1;//TEMP need a more sensible way of getting ack number. Is it per packet type, or per packet?
XMLMessageEncoder xmlMessageEncoder = new XMLMessageEncoder(ackNumber, System.currentTimeMillis(), XMLMessage.XMLTypeRace, getNextRaceXMLSequenceNumber(), (short) xmlStringUTF8.length, xmlString);
byte[] serializedMessage = xmlMessageEncoder.encode();
//Write it.
try
{
this.outputStream.write(serializedMessage);
}
catch (IOException e)
{
throw new InvalidRaceDataException();
}
System.out.println("working");
mockOutput.parseXMLString(xmlString, 26);
System.out.println("working");
}
public void sendBoatData() throws InvalidBoatDataException {
BoatData boatData = new BoatData(raceDataSource.getBoats());
//Serialize race data to an XML as a string.
String xmlString = boatData.createXML();
byte[] xmlStringUTF8 = new byte[0];
try
{
xmlStringUTF8 = xmlString.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new InvalidBoatDataException();
}
//Create XML message object and serialize.
short ackNumber = 1;//TEMP need a more sensible way of getting ack number. Is it per packet type, or per packet?
XMLMessageEncoder xmlMessageEncoder = new XMLMessageEncoder(ackNumber, System.currentTimeMillis(), XMLMessage.XMLTypeBoat, getNextBoatXMLSequenceNumber(), (short) xmlStringUTF8.length, xmlString);
byte[] serializedMessage = xmlMessageEncoder.encode();
//Write it.
try
{
this.outputStream.write(serializedMessage);
}
catch (IOException e)
{
throw new InvalidBoatDataException();
}
System.out.println("working");
mockOutput.parseXMLString(xmlString, 26);
System.out.println("working");
}
+7 -60
View File
@@ -9,6 +9,7 @@ import seng302.DataInput.RaceDataSource;
import SharedModel.*;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import seng302.Networking.MockOutput;
import seng302.Networking.Utils.BoatLocationMessage;
import java.awt.geom.Point2D;
@@ -34,9 +35,7 @@ public class Race implements Runnable {
protected int PRERACE_TIME = 120000; //time in milliseconds to pause during pre-race
private int lastFPS = 20;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
//Outputstream to write messages to.
private OutputStream outputStream;
private MockOutput mockOutput;
/**
@@ -46,13 +45,13 @@ public class Race implements Runnable {
* @param legs Number of marks in order that the boats pass in order to complete the race.
* @param scaleFactor for race
*/
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor, OutputStream outputStream) {
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor, MockOutput mockOutput) {
this.startingBoats = FXCollections.observableArrayList(boats);
this.legs = legs;
this.legs.add(new Leg("Finish", this.legs.size()));
this.scaleFactor = scaleFactor;
this.outputStream = outputStream;
this.mockOutput = mockOutput;
if (startingBoats != null && startingBoats.size() > 0) {
@@ -61,8 +60,8 @@ public class Race implements Runnable {
}
public Race(RaceDataSource raceData, int scaleFactor, OutputStream outputStream) {
this(raceData.getBoats(), raceData.getLegs(), scaleFactor, outputStream);
public Race(RaceDataSource raceData, int scaleFactor, MockOutput mockOutput) {
this(raceData.getBoats(), raceData.getLegs(), scaleFactor, mockOutput);
}
/**
@@ -94,53 +93,12 @@ public class Race implements Runnable {
*/
public void run() {
initialiseBoats();
outputHeartbeat();
countdownTimer();
}
/**
* Starts the heartbeat timer, which sends a heartbeat message every so often (i.e., 5 seconds).
*/
public void outputHeartbeat() {
long heartbeatPeriod = 5000;
AnimationTimer heartbeatTimer = new AnimationTimer() {
long currentHeartbeatTime = System.currentTimeMillis();
long endHeartbeatTime = System.currentTimeMillis() + heartbeatPeriod;
@Override
public void handle(long now) {
if (currentHeartbeatTime >= endHeartbeatTime) {
endHeartbeatTime = System.currentTimeMillis() + heartbeatPeriod;
//Update heartbeat value.
heartbeat++;
//Serialize heartbeat.
byte[] heartBeatMessage = RaceVisionByteEncoder.heartBeat(heartbeat);
//Write it to stream.
try
{
outputStream.write(heartBeatMessage);
}
catch (IOException e)
{//TODO should probably handle this in a more sensible manner.
e.printStackTrace();
}
}
//TODO stop the animation at some point.
/*if (raceFinish) {
System.out.println("Heartbeat stopping");
stop();
}*/
currentHeartbeatTime = System.currentTimeMillis();
}
};
heartbeatTimer.start();
}
/**
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
*/
@@ -256,18 +214,7 @@ public class Race implements Runnable {
//We have finished creating the message.
//Serialize.
byte[] boatLocationMessageSerialized = RaceVisionByteEncoder.boatLocation(boatLocationMessage);
//Write to stream.
try
{
outputStream.write(boatLocationMessageSerialized);
}
catch (IOException e)
{//TODO should probably handle this in a more sensible manner.
e.printStackTrace();
}
mockOutput.parseBoatLocation();
} else {