Removed output stream, started refactoring BoatDataClass

#story[778]
main
Erika Savell 9 years ago
parent 6a35c40f77
commit d05d7271fc

@ -11,7 +11,6 @@ import seng302.Model.Event;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.OutputStream;
public class App extends Application {
@ -27,10 +26,9 @@ 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();

@ -26,8 +26,9 @@ import java.util.List;
public class RaceData {
private RaceDataSource dataSource;
private String raceID;
private String raceType;
Document doc;
Element raceElement;
private OffsetDateTime creationTimeDate;
private OffsetDateTime raceStartTime;
private List<BoatInRace> participants;
@ -36,8 +37,6 @@ public class RaceData {
public RaceData(RaceDataSource dataSource) {
this.dataSource = dataSource;
this.raceID = dataSource.getRaceId();
this.raceType = "Fleet";
creationTimeDate = OffsetDateTime.now();
raceStartTime = OffsetDateTime.now().plusMinutes(3);
participants = dataSource.getBoats();
@ -51,19 +50,15 @@ public class RaceData {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
// root elements
Document doc = docBuilder.newDocument();
Element raceElement = doc.createElement("Race");
//root element;
raceElement = doc.createElement("Race");
doc.appendChild(raceElement);
Element raceIdElement = doc.createElement("RaceID");
raceIdElement.appendChild(doc.createTextNode(raceID));
raceElement.appendChild(raceIdElement);
Element raceTypeElement = doc.createElement("RaceType");
raceTypeElement.appendChild(doc.createTextNode(raceType));
raceElement.appendChild(raceTypeElement);
appendRaceId();
appendRaceType();
Element creationTimeElement = doc.createElement("CreationTimeDate");
creationTimeElement.appendChild(doc.createTextNode(creationTimeDate.toString()));
@ -164,4 +159,19 @@ public class RaceData {
throw new InvalidRaceDataException();
}
}
private void appendRaceId() {
Element raceIdElement = doc.createElement("RaceID");
raceIdElement.appendChild(doc.createTextNode(dataSource.getRaceId()));
raceElement.appendChild(raceIdElement);
}
private void appendRaceType() {
Element raceTypeElement = doc.createElement("RaceType");
raceTypeElement.appendChild(doc.createTextNode(dataSource.getRaceType()));
raceElement.appendChild(raceTypeElement);
}
}

@ -17,6 +17,7 @@ public interface RaceDataSource {
List<Marker> getMarkers();
String getRaceId();
String getRaceType();
GPSCoordinate getMark();
GPSCoordinate getMapTopLeft();

@ -11,10 +11,6 @@ import seng302.Exceptions.InvalidRegattaDataException;
import seng302.Mock.RaceDataSource;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by esa46 on 21/04/17.
*/
@ -22,13 +18,10 @@ public class Event {
RaceDataSource raceDataSource;
RegattaDataSource regattaDataSource;
///The stream to which we send all data.
private OutputStream outputStream;
public Event(RaceDataSource raceData, RegattaDataSource regattaData, OutputStream outputStream) {
public Event(RaceDataSource raceData, RegattaDataSource regattaData) {
this.raceDataSource = raceData;
this.regattaDataSource = regattaData;
this.outputStream = outputStream;
}
public void start()
@ -42,7 +35,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);
new Thread((newRace)).start();
}
@ -52,19 +45,7 @@ public class Event {
RegattaData regattaData = new RegattaData(regattaDataSource);
String xmlString = regattaData.createXML();
//TODO now we should place in XML message object.
//TODO now we should serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
try
{
this.outputStream.write(xmlString.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
catch (IOException e)
{
throw new InvalidRegattaDataException();
}
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
}
@ -74,18 +55,7 @@ public class Event {
//Serialize race data to an XML as a string.
String xmlString = raceData.createXML();
//TODO now we should place in XML message object.
//TODO now we should serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
try
{
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
catch (IOException e)
{
throw new InvalidRaceDataException();
}
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
}
public void sendBoatData() throws InvalidBoatDataException
@ -94,19 +64,7 @@ public class Event {
//Serialize race data to an XML as a string.
String xmlString = boatData.createXML();
//TODO now we should place in XML message object.
//TODO now we should serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
try
{
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
catch (IOException e)
{
throw new InvalidBoatDataException();
}
System.out.println(xmlString); // to be replaced by TCPClient.send(xmlString) type function call
}

@ -14,8 +14,6 @@ import seng302.RaceEventMessages.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;
@ -34,29 +32,23 @@ public class Race implements Runnable {
private int lastFPS = 20;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
protected int heartbeat = 0;
protected boolean raceFinish = false;
protected int scaleFactor;
protected int PRERACE_TIME = 120000; //time in milliseconds to pause during pre-race
//Outputstream to write messages to.
private OutputStream outputStream;
/**
* Initailiser for Race
*
* @param boats Takes in an array of boats that are participating in the race.
* @param legs Number of marks in order that the boats pass in order to complete the race.
* @param scaleFactor for race
* @param outputStream Outputstream to write messages to.
*/
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor, OutputStream outputStream) {
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor) {
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();
@ -64,13 +56,8 @@ public class Race implements Runnable {
}
//Constructor used in testing
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor) {
this(boats, legs, scaleFactor, null);
}
public Race(RaceDataSource raceData, int scaleFactor, OutputStream outputStream) {
this(raceData.getBoats(), raceData.getLegs(), scaleFactor, outputStream);
public Race(RaceDataSource raceData, int scaleFactor) {
this(raceData.getBoats(), raceData.getLegs(), scaleFactor);
}
/**
@ -222,15 +209,6 @@ public class Race implements Runnable {
//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
try
{
//TODO we should actually serialize the boat message before writing to output.
outputStream.write(boatLocationMessage.toString().getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
} else {

@ -351,4 +351,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
public String getRaceId() { return raceID; }
public List<Marker> getMarkers() { return markers; }
public String getRaceType() {
return "FLEET";
}
}

Loading…
Cancel
Save