Mock now serializes and sends Boat.xml, Race.xml, Regatta.xml, Heartbeat, BoatLocation messages.
#story[778]
This commit is contained in:
@@ -9,6 +9,8 @@ import seng302.DataInput.RaceXMLReader;
|
||||
import seng302.DataInput.RegattaDataSource;
|
||||
import seng302.DataInput.RegattaXMLReader;
|
||||
import seng302.Model.Event;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
@@ -27,9 +29,11 @@ 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);
|
||||
Event raceEvent = new Event(raceData, regattaData, outputStream);
|
||||
raceEvent.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -8,6 +8,13 @@ import seng302.DataInput.RegattaDataSource;
|
||||
import seng302.Exceptions.InvalidBoatDataException;
|
||||
import seng302.Exceptions.InvalidRaceDataException;
|
||||
import seng302.Exceptions.InvalidRegattaDataException;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
import seng302.Networking.Utils.XMLMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,22 +25,33 @@ public class Event {
|
||||
RaceDataSource raceDataSource;
|
||||
RegattaDataSource regattaDataSource;
|
||||
|
||||
public Event(RaceDataSource raceData, RegattaDataSource regattaData) {
|
||||
///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;
|
||||
|
||||
|
||||
public Event(RaceDataSource raceData, RegattaDataSource regattaData, OutputStream outputStream) {
|
||||
this.raceDataSource = raceData;
|
||||
this.regattaDataSource = regattaData;
|
||||
this.outputStream = outputStream;
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug
|
||||
//System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug
|
||||
sendRegattaData();
|
||||
System.out.println("\nRACE DATA\n");//TEMP REMOVE debug
|
||||
//System.out.println("\nRACE DATA\n");//TEMP REMOVE debug
|
||||
sendRaceData();
|
||||
System.out.println("\nBOAT DATA\n");//TEMP REMOVE debug
|
||||
//System.out.println("\nBOAT DATA\n");//TEMP REMOVE debug
|
||||
sendBoatData();
|
||||
|
||||
System.out.println("RACE STARTING!!\n\n");//TEMP REMOVE debug
|
||||
//System.out.println("RACE STARTING!!\n\n");//TEMP REMOVE debug
|
||||
|
||||
Race newRace = new Race(raceDataSource, 15);
|
||||
Race newRace = new Race(raceDataSource, 15, this.outputStream);
|
||||
new Thread((newRace)).start();
|
||||
}
|
||||
|
||||
@@ -43,7 +61,34 @@ public class Event {
|
||||
RegattaData regattaData = new RegattaData(regattaDataSource);
|
||||
String xmlString = regattaData.createXML();
|
||||
|
||||
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -52,7 +97,32 @@ public class Event {
|
||||
//Serialize race data to an XML as a string.
|
||||
String xmlString = raceData.createXML();
|
||||
|
||||
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendBoatData() throws InvalidBoatDataException {
|
||||
@@ -60,8 +130,65 @@ public class Event {
|
||||
//Serialize race data to an XML as a string.
|
||||
String xmlString = boatData.createXML();
|
||||
|
||||
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next sequence number to be used for regatta XML messages.
|
||||
* @return
|
||||
*/
|
||||
public short getNextRegattaXMLSequenceNumber()
|
||||
{
|
||||
short currentNumber = regattaXMLSequenceNumber;
|
||||
regattaXMLSequenceNumber += 1;
|
||||
return currentNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next sequence number to be used for race XML messages.
|
||||
* @return
|
||||
*/
|
||||
public short getNextRaceXMLSequenceNumber()
|
||||
{
|
||||
short currentNumber = raceXMLSequenceNumber;
|
||||
raceXMLSequenceNumber += 1;
|
||||
return currentNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next sequence number to be used for boat XML messages.
|
||||
* @return
|
||||
*/
|
||||
public short getNextBoatXMLSequenceNumber()
|
||||
{
|
||||
short currentNumber = boatXMLSequenceNumber;
|
||||
boatXMLSequenceNumber += 1;
|
||||
return currentNumber;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
@@ -33,6 +35,10 @@ public class Race implements Runnable {
|
||||
private int lastFPS = 20;
|
||||
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
|
||||
|
||||
//Outputstream to write messages to.
|
||||
private OutputStream outputStream;
|
||||
|
||||
|
||||
/**
|
||||
* Initailiser for Race
|
||||
*
|
||||
@@ -40,12 +46,14 @@ 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) {
|
||||
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor, OutputStream outputStream) {
|
||||
|
||||
this.startingBoats = FXCollections.observableArrayList(boats);
|
||||
this.legs = legs;
|
||||
this.legs.add(new Leg("Finish", this.legs.size()));
|
||||
this.scaleFactor = scaleFactor;
|
||||
this.outputStream = outputStream;
|
||||
|
||||
|
||||
if (startingBoats != null && startingBoats.size() > 0) {
|
||||
initialiseBoats();
|
||||
@@ -53,8 +61,8 @@ public class Race implements Runnable {
|
||||
|
||||
}
|
||||
|
||||
public Race(RaceDataSource raceData, int scaleFactor) {
|
||||
this(raceData.getBoats(), raceData.getLegs(), scaleFactor);
|
||||
public Race(RaceDataSource raceData, int scaleFactor, OutputStream outputStream) {
|
||||
this(raceData.getBoats(), raceData.getLegs(), scaleFactor, outputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,21 +98,38 @@ public class Race implements Runnable {
|
||||
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() + 5000;
|
||||
long endHeartbeatTime = System.currentTimeMillis() + heartbeatPeriod;
|
||||
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
if (currentHeartbeatTime >= endHeartbeatTime) {
|
||||
System.out.println("-------");
|
||||
System.out.println("Heartbeat value: " + heartbeat);
|
||||
System.out.println("-------");
|
||||
endHeartbeatTime = System.currentTimeMillis() + 5000;
|
||||
endHeartbeatTime = System.currentTimeMillis() + heartbeatPeriod;
|
||||
|
||||
//Update heartbeat value.
|
||||
heartbeat++;
|
||||
//TODO: Send heartbeat value
|
||||
|
||||
//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();
|
||||
@@ -230,8 +255,19 @@ public class Race implements Runnable {
|
||||
boatLocationMessage.setRudderAngle((short) 0);//Junk value.
|
||||
|
||||
//We have finished creating the message.
|
||||
//TODO at this point, we need to send the event to the visualiser.
|
||||
//System.out.println(boatLocationMessage);//TEMP debug print
|
||||
|
||||
//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();
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
@@ -251,12 +287,12 @@ public class Race implements Runnable {
|
||||
for (BoatInRace boat : startingBoats) {
|
||||
if (boat != null) {
|
||||
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
|
||||
System.out.println(boat.toString() + " " + boat.getPosition());//TEMP debug print
|
||||
//System.out.println(boat.toString() + " " + boat.getPosition());//TEMP debug print
|
||||
if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0)
|
||||
boat.setPosition("-");
|
||||
}
|
||||
}
|
||||
System.out.println("=====");//TEMP debug print
|
||||
//System.out.println("=====");//TEMP debug print
|
||||
}
|
||||
|
||||
public void initialiseBoats() {
|
||||
|
||||
@@ -3,6 +3,10 @@ package seng302.Model;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import SharedModel.BoatInRace;
|
||||
import SharedModel.GPSCoordinate;
|
||||
import SharedModel.Leg;
|
||||
import SharedModel.Marker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -32,7 +36,7 @@ public class RaceTest {
|
||||
legs.add(START_LEG);
|
||||
legs.add(FINISH_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, 5);
|
||||
Race race = new Race(boats, legs, 5, System.out);
|
||||
race.setDnfChance(0);
|
||||
long timeStarted = System.currentTimeMillis();
|
||||
race.run();
|
||||
@@ -53,7 +57,7 @@ public class RaceTest {
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
legs.add(FINISH_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, 1);
|
||||
Race race = new Race(boats, legs, 1, System.out);
|
||||
race.setDnfChance(0);
|
||||
assertEquals(race.boatsFinished, 0);
|
||||
|
||||
@@ -77,7 +81,7 @@ public class RaceTest {
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
legs.add(FINISH_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, 1);
|
||||
Race race = new Race(boats, legs, 1, System.out);
|
||||
race.setDnfChance(0);
|
||||
assertEquals(race.boatsFinished, 0);
|
||||
|
||||
@@ -95,7 +99,7 @@ public class RaceTest {
|
||||
legs.add(START_LEG);
|
||||
legs.add(FINISH_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, 1);
|
||||
Race race = new Race(boats, legs, 1, System.out);
|
||||
race.setDnfChance(0);
|
||||
|
||||
BoatInRace unFinishedBoat = new BoatInRace("Test", 10, Color.ALICEBLUE, "tt", 1);
|
||||
@@ -152,7 +156,7 @@ public class RaceTest {
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
legs.add(START_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, scaleFactor);
|
||||
Race race = new Race(boats, legs, scaleFactor, System.out);
|
||||
race.setDnfChance(0);
|
||||
|
||||
assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-6);
|
||||
@@ -170,7 +174,7 @@ public class RaceTest {
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
legs.add(START_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, scaleFactor);
|
||||
Race race = new Race(boats, legs, scaleFactor, System.out);
|
||||
race.totalTimeElapsed = 6000; //6 seconds
|
||||
assertTrue(race.calcTimer().equals("Race clock: 00:01:00"));
|
||||
}
|
||||
@@ -184,7 +188,7 @@ public class RaceTest {
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
legs.add(START_LEG);
|
||||
|
||||
Race race = new Race(boats, legs, scaleFactor);
|
||||
Race race = new Race(boats, legs, scaleFactor, System.out);
|
||||
race.totalTimeElapsed = 3213000;
|
||||
assertTrue(race.calcTimer().equals("Race clock: 02:40:39"));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user