Mock App now passes an OutputStream into the "Event" class. This is where data is written to (i.e., writing data over tcp socket).

Updated Event and Race class to write to this stream, and added some TODO notes which point out where we need to properly serialize messages, when merged with networking code.
main
fjc40 9 years ago
parent 2ce84e3be4
commit bf5ea193af

@ -8,6 +8,7 @@ import seng302.Model.Event;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.OutputStream;
public class App extends Application {
@ -23,8 +24,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");
Event raceEvent = new Event(raceData);
Event raceEvent = new Event(raceData, outputStream);
raceEvent.start();
} catch (IOException e) {
e.printStackTrace();

@ -18,6 +18,7 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.time.OffsetDateTime;
import java.util.List;
@ -47,7 +48,7 @@ public class RaceData {
}
public void createXML() {
public String createXML() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
@ -148,14 +149,13 @@ public class RaceData {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
//Serialize document.
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
transformer.transform(source,result);
transformer.transform(source, result);
System.out.println("File saved!");
return stringWriter.toString();
} catch (ParserConfigurationException pce) {
@ -164,6 +164,7 @@ public class RaceData {
tfe.printStackTrace();
}
return "";//TEMP this is probably bad. This shouldn't really be reached, but seems necessary due to the use of catches above.
}

@ -13,6 +13,9 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.List;
/**
@ -22,16 +25,33 @@ public class Event {
RaceDataSource raceDataSource;
public Event(RaceDataSource raceData) {
///The stream to which we send all data.
private OutputStream outputStream;
/**
* Ctor.
* @param raceData
* @param outputStream OutputStream to write data to.
*/
public Event(RaceDataSource raceData, OutputStream outputStream) {
this.raceDataSource = raceData;
this.outputStream = outputStream;
}
public void start() {
public void start() throws IOException
{
System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug
sendRegattaData();
System.out.println("\nRACE DATA\n");//TEMP REMOVE debug
sendRaceData();
System.out.println("\nBOAT DATA\n");//TEMP REMOVE debug
sendBoatData();
Race newRace = new Race(raceDataSource, 15);
System.out.println("RACE STARTING!!\n\n");//TEMP REMOVE debug
Race newRace = new Race(raceDataSource, 15, this.outputStream);
new Thread((newRace)).start();
}
@ -87,13 +107,21 @@ public class Event {
magneticVariation.appendChild(doc.createTextNode(Double.toString(14.76)));
rootElement.appendChild(magneticVariation);
TransformerFactory trasformerFactory = TransformerFactory.newInstance();
Transformer transformer = trasformerFactory.newTransformer();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//print XML object to check for correctness
StreamResult result = new StreamResult(System.out);
//Serialize document.
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
transformer.transform(source,result);
//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.
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
} catch (Exception e){
e.printStackTrace();
@ -101,9 +129,17 @@ public class Event {
}
public void sendRaceData() {
public void sendRaceData() throws IOException
{
RaceData raceData = new RaceData(raceDataSource);
raceData.createXML();
//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.
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
public void sendBoatData() {
@ -194,9 +230,17 @@ public class Event {
Transformer transformer = trasformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//print XML object to check for correctness
StreamResult result = new StreamResult(System.out);
//Serialize document.
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
transformer.transform(source,result);
//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.
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
} catch (Exception e) {
e.printStackTrace();

@ -15,6 +15,8 @@ import seng302.RaceEventMessages.BoatLocationMessage;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -39,26 +41,33 @@ public class Race implements Runnable {
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) {
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();
}
}
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);
}
/**
@ -209,7 +218,16 @@ 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
//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 {

Loading…
Cancel
Save