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;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Oracle;
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.IEdge;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INode;
......@@ -317,6 +318,8 @@ public class DefaultTopologyComponent implements TopologyComponent {
private Location lastLocation = null;
private List<LocationListener> listeners = new LinkedList<LocationListener>();
private int eventTypeSeq = 0;
public LocationRequestImpl() {
// nothing to do
......@@ -324,6 +327,10 @@ public class DefaultTopologyComponent implements TopologyComponent {
protected void cancel(LocationListener listener) {
boolean removed = listeners.remove(listener);
if (listeners.isEmpty()) {
// upcoming event is no longer valid!
eventTypeSeq++;
}
assert removed;
}
......@@ -333,7 +340,7 @@ public class DefaultTopologyComponent implements TopologyComponent {
if (listeners.isEmpty()) {
// Only start once!
lastLocation = null;
Event.scheduleImmediately(this, null, 0);
Event.scheduleImmediately(this, null, eventTypeSeq);
} else {
// Fire each new listener at least once
listener.onLocationChanged(getHost(), getLastLocation());
......@@ -357,13 +364,22 @@ public class DefaultTopologyComponent implements TopologyComponent {
@Override
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()) {
// Only reschedule, if at least one listener is ... listening
Location newLoc = getLastLocation();
listeners.forEach((LocationListener listener) -> listener
.onLocationChanged(getHost(), 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