refactored constructor and data structures in ExpeditionMessageParser

Change-Id: If5410c112ff2f8409588b1be9b6d3398c73b715e
This commit is contained in:
Axel Uhl
2017-10-26 11:55:00 +02:00
parent cf4a8975be
commit 5aa7de1c48
3 changed files with 18 additions and 20 deletions
@@ -25,7 +25,7 @@ public class UDPExpeditionReceiver extends UDPReceiver<ExpeditionMessage, Expedi
* Remembers, per boat ID, the milliseconds difference between the time the message was received
* and the GPS time stamp provided by the message.
*/
private final Map<Integer, Long> timeStampOfLastMessageReceived;
private final Map<Integer, Long> delayBetweenMessageTimestampAndTimepointReceived;
private final ExpeditionMessageParser parser;
@@ -75,7 +75,7 @@ public class UDPExpeditionReceiver extends UDPReceiver<ExpeditionMessage, Expedi
public UDPExpeditionReceiver(int listeningOnPort, DeviceRegistry deviceRegistry) throws SocketException {
super(listeningOnPort);
this.deviceRegistry = deviceRegistry;
this.timeStampOfLastMessageReceived = new HashMap<Integer, Long>();
this.delayBetweenMessageTimestampAndTimepointReceived = new HashMap<Integer, Long>();
parser = new ExpeditionMessageParser(this);
addListener(msg->produceAndStoreOptionalFixes(msg), /* validMessagesOnly */ true);
}
@@ -113,8 +113,12 @@ public class UDPExpeditionReceiver extends UDPReceiver<ExpeditionMessage, Expedi
}
}
public Map<Integer, Long> getTimeStampOfLastMessageReceived() {
return timeStampOfLastMessageReceived;
public Long getLastKnownMessageDelayInMillis(int boatID) {
return delayBetweenMessageTimestampAndTimepointReceived.get(boatID);
}
public void updateLastKnownMessageDelay(int boatID, long timestampAsMillis) {
delayBetweenMessageTimestampAndTimepointReceived.put(boatID, timestampAsMillis);
}
protected ExpeditionMessageParser getParser() {
@@ -42,19 +42,7 @@ public class ExpeditionMessageImpl implements ExpeditionMessage {
* {@link #getTimePoint() time point}.
*/
public ExpeditionMessageImpl(int boatID, Map<Integer, Double> values, boolean valid, String originalMessage) {
this.boatID = boatID;
// ensure that nobody can manipulate the map used by this message object from outside
this.values = new HashMap<Integer, Double>(values);
this.valid = valid;
this.originalMessage = originalMessage;
this.createdAtMillis = System.currentTimeMillis();
if (hasValue(ID_GPS_TIME)) {
timePoint = new MillisecondsTimePoint((long)
(getValue(ID_GPS_TIME)*24*3600*1000) + // this is the milliseconds since 31.12.1899 0:00:00 UTC
cal.getTimeInMillis());
} else {
timePoint = new MillisecondsTimePoint(createdAtMillis);
}
this(boatID, values, valid, /* defaultTimePoint */ null, originalMessage, /* unused */ true);
}
/**
@@ -63,9 +51,13 @@ public class ExpeditionMessageImpl implements ExpeditionMessage {
* stamp
*/
public ExpeditionMessageImpl(int boatID, Map<Integer, Double> values, boolean valid, TimePoint defaultTimePoint, String originalMessage) {
this(boatID, values, valid, defaultTimePoint, originalMessage, /* unused */ true);
if (defaultTimePoint == null) {
throw new IllegalArgumentException("defaultTimePoint for ExpeditionMessageImpl constructor must not be null");
throw new IllegalArgumentException("defaultTimePoint for this ExpeditionMessageImpl constructor must not be null");
}
}
private ExpeditionMessageImpl(int boatID, Map<Integer, Double> values, boolean valid, TimePoint defaultTimePoint, String originalMessage, boolean unused) {
this.boatID = boatID;
// ensure that nobody can manipulate the map used by this message object from outside
this.values = new HashMap<Integer, Double>(values);
@@ -76,6 +68,8 @@ public class ExpeditionMessageImpl implements ExpeditionMessage {
timePoint = new MillisecondsTimePoint((long)
(getValue(ID_GPS_TIME)*24*3600*1000) + // this is the milliseconds since 31.12.1899 0:00:00 UTC
cal.getTimeInMillis());
} else if (defaultTimePoint == null) {
timePoint = new MillisecondsTimePoint(createdAtMillis);
} else {
timePoint = defaultTimePoint;
}
@@ -36,7 +36,7 @@ public class ExpeditionMessageParser implements UDPMessageParser<ExpeditionMessa
Map<Integer, Double> values = new HashMap<Integer, Double>();
String[] variablesAndValuesInterleaved = variableValuePairs.split(",");
long now = System.currentTimeMillis();
Long diff = receiver.getTimeStampOfLastMessageReceived().get(boatID);
Long diff = receiver.getLastKnownMessageDelayInMillis(boatID);
TimePoint defaultForMessageTimePoint;
if (diff != null) {
// compute a reasonable default for a time stamp in case message doesn't provide one
@@ -60,7 +60,7 @@ public class ExpeditionMessageParser implements UDPMessageParser<ExpeditionMessa
}
if (result.hasValue(ExpeditionMessage.ID_GPS_TIME)) {
// an original GPS time stamp; then remember the difference between now and the time stamp
receiver.getTimeStampOfLastMessageReceived().put(boatID, now - result.getTimePoint().asMillis());
receiver.updateLastKnownMessageDelay(boatID, now - result.getTimePoint().asMillis());
}
return result;
} else {