Commit 44d3c530 authored by Björn Richerzhagen's avatar Björn Richerzhagen
Browse files

Fixed bug in LocationRequests

- if a request is re-used within a given time frame (<= the update
interval), this leads to incorrect event duplication and, thus, too
frequent listener calls
parent 5e0cc0a7
...@@ -45,6 +45,7 @@ import de.tudarmstadt.maki.simonstrator.api.Graphs; ...@@ -45,6 +45,7 @@ import de.tudarmstadt.maki.simonstrator.api.Graphs;
import de.tudarmstadt.maki.simonstrator.api.Host; import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Oracle; import de.tudarmstadt.maki.simonstrator.api.Oracle;
import de.tudarmstadt.maki.simonstrator.api.Randoms; import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.graph.Graph; import de.tudarmstadt.maki.simonstrator.api.common.graph.Graph;
import de.tudarmstadt.maki.simonstrator.api.common.graph.IEdge; import de.tudarmstadt.maki.simonstrator.api.common.graph.IEdge;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INode; import de.tudarmstadt.maki.simonstrator.api.common.graph.INode;
...@@ -317,6 +318,8 @@ public class DefaultTopologyComponent implements TopologyComponent { ...@@ -317,6 +318,8 @@ public class DefaultTopologyComponent implements TopologyComponent {
private Location lastLocation = null; private Location lastLocation = null;
private List<LocationListener> listeners = new LinkedList<LocationListener>(); private List<LocationListener> listeners = new LinkedList<LocationListener>();
private int eventTypeSeq = 0;
public LocationRequestImpl() { public LocationRequestImpl() {
// nothing to do // nothing to do
...@@ -324,6 +327,10 @@ public class DefaultTopologyComponent implements TopologyComponent { ...@@ -324,6 +327,10 @@ public class DefaultTopologyComponent implements TopologyComponent {
protected void cancel(LocationListener listener) { protected void cancel(LocationListener listener) {
boolean removed = listeners.remove(listener); boolean removed = listeners.remove(listener);
if (listeners.isEmpty()) {
// upcoming event is no longer valid!
eventTypeSeq++;
}
assert removed; assert removed;
} }
...@@ -333,7 +340,7 @@ public class DefaultTopologyComponent implements TopologyComponent { ...@@ -333,7 +340,7 @@ public class DefaultTopologyComponent implements TopologyComponent {
if (listeners.isEmpty()) { if (listeners.isEmpty()) {
// Only start once! // Only start once!
lastLocation = null; lastLocation = null;
Event.scheduleImmediately(this, null, 0); Event.scheduleImmediately(this, null, eventTypeSeq);
} else { } else {
// Fire each new listener at least once // Fire each new listener at least once
listener.onLocationChanged(getHost(), getLastLocation()); listener.onLocationChanged(getHost(), getLastLocation());
...@@ -357,13 +364,22 @@ public class DefaultTopologyComponent implements TopologyComponent { ...@@ -357,13 +364,22 @@ public class DefaultTopologyComponent implements TopologyComponent {
@Override @Override
public void eventOccurred(Object content, int type) { public void eventOccurred(Object content, int type) {
if (eventTypeSeq != type) {
/*
* Discard invalid events caused when a client cancels updates
* but reactivates the request within the update frequency
* interval. In this case, the old events continue rescheduling
* themselves.
*/
return;
}
if (!listeners.isEmpty()) { if (!listeners.isEmpty()) {
// Only reschedule, if at least one listener is ... listening // Only reschedule, if at least one listener is ... listening
Location newLoc = getLastLocation(); Location newLoc = getLastLocation();
listeners.forEach((LocationListener listener) -> listener listeners.forEach((LocationListener listener) -> listener
.onLocationChanged(getHost(), newLoc)); .onLocationChanged(getHost(), newLoc));
lastLocation = newLoc; lastLocation = newLoc;
Event.scheduleWithDelay(interval, this, null, 0); Event.scheduleWithDelay(interval, this, null, eventTypeSeq);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment