Moved XML message creation to RaceServer instead of ConnectionAcceptor.

RaceServer does not currently generate new XMLMessages - NYI.
Added a sequence number to RaceDataSource - this is used to indicate that the data source has been modified.
#story[1188]
main
fjc40 8 years ago
parent d0eebcdb2f
commit 9156bde8af

@ -61,12 +61,7 @@ public class ConnectionAcceptor implements Runnable {
*/ */
private SourceIdAllocator sourceIdAllocator; private SourceIdAllocator sourceIdAllocator;
//race xml sequence number
private short raceXMLSequenceNumber;
//boat xml sequence number
private short boatXMLSequenceNumber;
//regatta xml sequence number
private short regattaXMLSequenceNumber;
// //
private RaceLogic raceLogic = null; private RaceLogic raceLogic = null;
@ -209,77 +204,6 @@ public class ConnectionAcceptor implements Runnable {
} }
} }
} }
/**
* Sets the Race XML to send.
* @param raceXml XML to send to the Client.
*/
public void setRaceXml(String raceXml) {
//Create the message.
XMLMessage message = this.createXMLMessage(raceXml, XMLMessageType.RACE);
//Place it in LatestMessages.
this.latestMessages.setRaceXMLMessage(message);
}
/**
* Sets the Regatta XMl to send.
* @param regattaXml XML to send to Client.
*/
public void setRegattaXml(String regattaXml) {
//Create the message.
XMLMessage message = this.createXMLMessage(regattaXml, XMLMessageType.REGATTA);
//Place it in LatestMessages.
this.latestMessages.setRegattaXMLMessage(message);
}
/**
* Sets the Boats XML to send.
* @param boatsXml XMl to send to the Client.
*/
public void setBoatsXml(String boatsXml) {
//Create the message.
XMLMessage message = this.createXMLMessage(boatsXml, XMLMessageType.BOAT);
//Place it in LatestMessages.
this.latestMessages.setBoatXMLMessage(message);
}
/**
* Creates an XMLMessage of a specified subtype using the xml contents string.
* @param xmlString The contents of the xml file.
* @param messageType The subtype of xml message (race, regatta, boat).
* @return The created XMLMessage object.
*/
private XMLMessage createXMLMessage(String xmlString, XMLMessageType messageType) {
//Get the correct sequence number to use, and increment it.
short sequenceNumber = 0;
if (messageType == XMLMessageType.RACE) {
sequenceNumber = this.raceXMLSequenceNumber;
this.raceXMLSequenceNumber++;
} else if (messageType == XMLMessageType.BOAT) {
sequenceNumber = this.boatXMLSequenceNumber;
this.boatXMLSequenceNumber++;
} else if (messageType == XMLMessageType.REGATTA) {
sequenceNumber = this.regattaXMLSequenceNumber;
this.regattaXMLSequenceNumber++;
}
//Create the message.
XMLMessage message = new XMLMessage(
XMLMessage.currentVersionNumber,
AckSequencer.getNextAckNum(),
System.currentTimeMillis(),
messageType,
sequenceNumber,
xmlString);
return message;
}
} }

@ -120,8 +120,7 @@ public class Event {
} }
List<Integer> potentialParticipants = new ArrayList<>(boatDataSource.getBoats().keySet());
this.sourceIdAllocator = new SourceIdAllocator(potentialParticipants);
this.compositeCommand = new CompositeCommand(); this.compositeCommand = new CompositeCommand();
this.latestMessages = new LatestMessages(); this.latestMessages = new LatestMessages();
@ -149,6 +148,7 @@ public class Event {
//Create connection acceptor. //Create connection acceptor.
this.sourceIdAllocator = new SourceIdAllocator(newRace.getRace());
try { try {
this.connectionAcceptor = new ConnectionAcceptor(latestMessages, compositeCommand, sourceIdAllocator, newRace); this.connectionAcceptor = new ConnectionAcceptor(latestMessages, compositeCommand, sourceIdAllocator, newRace);

@ -1,7 +1,9 @@
package mock.model; package mock.model;
import network.AckSequencer;
import network.Messages.*; import network.Messages.*;
import network.Messages.Enums.BoatLocationDeviceEnum; import network.Messages.Enums.BoatLocationDeviceEnum;
import network.Messages.Enums.XMLMessageType;
import shared.model.Bearing; import shared.model.Bearing;
import shared.model.CompoundMark; import shared.model.CompoundMark;
import shared.model.Mark; import shared.model.Mark;
@ -22,10 +24,29 @@ public class RaceServer {
*/ */
private int boatLocationSequenceNumber = 1; private int boatLocationSequenceNumber = 1;
/**
* The sequence number of race XML messages.
*/
private int raceXMLSeqNumber;
/**
* The sequence number of boat XML messages.
*/
private int boatXMLSeqNumber;
/**
* The sequence number of regatta XML messages.
*/
private int regattaXMLSeqNumber;
public RaceServer(MockRace race, LatestMessages latestMessages) { public RaceServer(MockRace race, LatestMessages latestMessages) {
this.race = race; this.race = race;
this.latestMessages = latestMessages; this.latestMessages = latestMessages;
this.raceXMLSeqNumber = race.getRaceDataSource().getSequenceNumber();
//this.boatXMLSeqNumber = race.getBoatDataSource().getSequenceNumber();
//this.regattaXMLSeqNumber = race.getRegattaDataSource().getSequenceNumber();
} }
/** /**
@ -45,8 +66,46 @@ public class RaceServer {
snapshotMessages.add(parseRaceStatus()); snapshotMessages.add(parseRaceStatus());
latestMessages.setSnapshot(snapshotMessages); latestMessages.setSnapshot(snapshotMessages);
updateXMLFiles();
}
/**
* Checks if the race/boat/regatta data sources have changed, and if they have, update their xml representations.
*/
private void updateXMLFiles() {
updateRaceXMLFile();
//updateBoatXMLFile();
//updateRegattaXMLFile();
}
/**
* Checks if the race data source has changed, and if it has, updates LatestMessages' race xml message.
*/
private void updateRaceXMLFile() {
if (raceXMLSeqNumber != race.getRaceDataSource().getSequenceNumber()) {
//TODO generate XML string from race data source
String raceXMLString = "test";//TODO
XMLMessage message = createXMLMessage(raceXMLString, XMLMessageType.RACE);
latestMessages.setXMLMessage(message);
raceXMLSeqNumber = race.getRaceDataSource().getSequenceNumber();
}
}
/**
* Checks if the boat data source has changed, and if it has, updates LatestMessages' boat xml message.
*/
private void updateBoatXMLFile() {
} }
/**
* Checks if the regatta data source has changed, and if it has, updates LatestMessages' regatta xml message.
*/
private void updateRegattaXMLFile() {
}
/** /**
* Parses an individual marker boat, and returns it. * Parses an individual marker boat, and returns it.
@ -178,4 +237,41 @@ public class RaceServer {
return raceStatus; return raceStatus;
} }
/**
* Creates an XMLMessage of a specified subtype using the xml contents string.
* @param xmlString The contents of the xml file.
* @param messageType The subtype of xml message (race, regatta, boat).
* @return The created XMLMessage object.
*/
private XMLMessage createXMLMessage(String xmlString, XMLMessageType messageType) {
//Get the correct sequence number to use.
int sequenceNumber = 0;
if (messageType == XMLMessageType.RACE) {
sequenceNumber = this.raceXMLSeqNumber;
} else if (messageType == XMLMessageType.BOAT) {
sequenceNumber = this.boatXMLSeqNumber;
} else if (messageType == XMLMessageType.REGATTA) {
sequenceNumber = this.regattaXMLSeqNumber;
}
//Create the message.
XMLMessage message = new XMLMessage(
XMLMessage.currentVersionNumber,
AckSequencer.getNextAckNum(),
System.currentTimeMillis(),
messageType,
sequenceNumber,
xmlString);
return message;
}
} }

@ -35,6 +35,7 @@ public class LatestMessages extends Observable {
/** /**
* Ctor. * Ctor.
*/ */

@ -76,6 +76,8 @@ public class EmptyRaceDataSource implements RaceDataSource {
private RaceTypeEnum raceType = RaceTypeEnum.NOT_A_RACE_TYPE; private RaceTypeEnum raceType = RaceTypeEnum.NOT_A_RACE_TYPE;
private int sequenceNumber = 0;
public EmptyRaceDataSource() { public EmptyRaceDataSource() {
} }
@ -126,4 +128,14 @@ public class EmptyRaceDataSource implements RaceDataSource {
public List<Integer> getParticipants() { public List<Integer> getParticipants() {
return participants; return participants;
} }
@Override
public int getSequenceNumber() {
return sequenceNumber;
}
@Override
public void incrementSequenceNumber() {
sequenceNumber++;
}
} }

@ -83,4 +83,16 @@ public interface RaceDataSource {
* @return Bottom right GPS coordinate. * @return Bottom right GPS coordinate.
*/ */
GPSCoordinate getMapBottomRight(); GPSCoordinate getMapBottomRight();
/**
* Returns the sequence number associated with this data source. Used to indicate when it has changed.
* @return Sequence number.
*/
int getSequenceNumber();
/**
* Increments the sequence number for this data source. Used to indicate that it has changed.
*/
void incrementSequenceNumber();
} }

@ -84,6 +84,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
private RaceTypeEnum raceType; private RaceTypeEnum raceType;
private int sequenceNumber = 0;
/** /**
@ -491,4 +493,14 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
public List<Corner> getCornersList() { public List<Corner> getCornersList() {
return cornersList; return cornersList;
} }
@Override
public int getSequenceNumber() {
return sequenceNumber;
}
@Override
public void incrementSequenceNumber() {
sequenceNumber++;
}
} }

@ -33,20 +33,23 @@ public class Constants {
* Frame periods are multiplied by this to get the amount of time a single frame represents. * Frame periods are multiplied by this to get the amount of time a single frame represents.
* E.g., frame period = 20ms, scale = 5, frame represents 20 * 5 = 100ms, and so boats are simulated for 100ms, even though only 20ms actually occurred. * E.g., frame period = 20ms, scale = 5, frame represents 20 * 5 = 100ms, and so boats are simulated for 100ms, even though only 20ms actually occurred.
*/ */
public static final int RaceTimeScale = 10; public static final int RaceTimeScale = 1;//10;
/** /**
* The race pre-start time, in milliseconds. 3 minutes. * The race pre-start time, in milliseconds. 3 minutes.
*/ */
public static final long RacePreStartTime = 1 * 10 * 1000; public static final long RacePreStartTime = 3 * 60 * 1000;
/** /**
* The race preparatory time, in milliseconds. 1 minutes. * The race preparatory time, in milliseconds. 1 minute.
*/ */
public static final long RacePreparatoryTime = 1 * 60 * 1000; public static final long RacePreparatoryTime = 1 * 60 * 1000;
/** /**
* The number of milliseconds in one hour. * The number of milliseconds in one hour.
* <br> * <br>

@ -71,8 +71,10 @@ public class FinishController extends Controller {
//Winner label. //Winner label.
raceWinnerLabel.setText("Winner: "+ boatNameColumn.getCellObservableValue(0).getValue()); if (boats.size() > 0) {
raceWinnerLabel.setWrapText(true); raceWinnerLabel.setText("Winner: " + boatNameColumn.getCellObservableValue(0).getValue());
raceWinnerLabel.setWrapText(true);
}
} }

@ -5,12 +5,7 @@
<CreationTimeDate>RACE_CREATION_TIME</CreationTimeDate> <CreationTimeDate>RACE_CREATION_TIME</CreationTimeDate>
<RaceStartTime Postpone="false" Time="RACE_START_TIME"/> <RaceStartTime Postpone="false" Time="RACE_START_TIME"/>
<Participants> <Participants>
<Yacht SourceID="121"/>
<Yacht SourceID="122"/>
<Yacht SourceID="123"/>
<Yacht SourceID="124"/>
<Yacht SourceID="125"/>
<Yacht SourceID="126"/>
</Participants> </Participants>
<CompoundMarkSequence> <CompoundMarkSequence>
<Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" /> <Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" />

@ -15,30 +15,16 @@ import static org.junit.Assert.*;
*/ */
public class SourceIdAllocatorTest { public class SourceIdAllocatorTest {
/** private MockRace mockRace;
* This is the list of source IDs that we start with.
*/
private List<Integer> originalSourceIDs;
/**
* Used to allocate source IDs.
*/
private SourceIdAllocator sourceIdAllocator; private SourceIdAllocator sourceIdAllocator;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
originalSourceIDs = new ArrayList<>(); mockRace = MockRaceTest.createMockRace();
originalSourceIDs.add(120);
originalSourceIDs.add(121);
originalSourceIDs.add(122);
originalSourceIDs.add(123);
originalSourceIDs.add(124);
originalSourceIDs.add(125);
sourceIdAllocator = new SourceIdAllocator(originalSourceIDs); sourceIdAllocator = new SourceIdAllocator(mockRace);
} }
@ -49,11 +35,12 @@ public class SourceIdAllocatorTest {
@Test @Test
public void emptyAllocationTest() { public void emptyAllocationTest() {
SourceIdAllocator allocator = new SourceIdAllocator(new ArrayList<>()); mockRace.getRaceDataSource().getParticipants().removeAll(mockRace.getBoatDataSource().getBoats().keySet());
mockRace.getRaceDataSource().getParticipants().addAll(mockRace.getBoatDataSource().getBoats().keySet());
try { try {
int sourceID = allocator.allocateSourceID(); int sourceID = sourceIdAllocator.allocateSourceID();
fail("Exception should have been thrown, but wasn't."); fail("Exception should have been thrown, but wasn't.");
@ -108,10 +95,6 @@ public class SourceIdAllocatorTest {
@Test @Test
public void reallocationTest() throws Exception { public void reallocationTest() throws Exception {
List<Integer> sourceIDList = new ArrayList<>();
sourceIDList.add(123);
SourceIdAllocator sourceIdAllocator = new SourceIdAllocator(sourceIDList);
//Allocate. //Allocate.
int sourceID = sourceIdAllocator.allocateSourceID(); int sourceID = sourceIdAllocator.allocateSourceID();

@ -5,12 +5,6 @@
<CreationTimeDate>2017-04-19T15:30:00+1200</CreationTimeDate> <CreationTimeDate>2017-04-19T15:30:00+1200</CreationTimeDate>
<RaceStartTime Postpone="false" Time="2017-04-19T15:33:00+1200"/> <RaceStartTime Postpone="false" Time="2017-04-19T15:33:00+1200"/>
<Participants> <Participants>
<Yacht SourceID="121"/>
<Yacht SourceID="122"/>
<Yacht SourceID="123"/>
<Yacht SourceID="124"/>
<Yacht SourceID="125"/>
<Yacht SourceID="126"/>
</Participants> </Participants>
<CompoundMarkSequence> <CompoundMarkSequence>
<Corner CompoundMarkID="1" SeqID="1"/> <Corner CompoundMarkID="1" SeqID="1"/>
@ -54,4 +48,4 @@
<Limit Lat="32.297164" Lon="-64.856394" SeqID="10"/> <Limit Lat="32.297164" Lon="-64.856394" SeqID="10"/>
<Limit Lat="32.296148" Lon="-64.849184" SeqID="11"/> <Limit Lat="32.296148" Lon="-64.849184" SeqID="11"/>
</CourseLimit> </CourseLimit>
</Race> </Race>

Loading…
Cancel
Save