-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from DroidPlanner/fix_optimization
Events dispatching optimization
- Loading branch information
Showing
3 changed files
with
74 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 50 additions & 17 deletions
67
dependencyLibs/Core/src/org/droidplanner/core/drone/DroneEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,67 @@ | ||
package org.droidplanner.core.drone; | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
import org.droidplanner.core.drone.DroneInterfaces.DroneEventsType; | ||
import org.droidplanner.core.drone.DroneInterfaces.OnDroneListener; | ||
import org.droidplanner.core.model.Drone; | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class DroneEvents extends DroneVariable { | ||
|
||
public DroneEvents(Drone myDrone) { | ||
super(myDrone); | ||
} | ||
private static final long EVENT_DISPATCHING_DELAY = 33l; //milliseconds | ||
|
||
private final ConcurrentLinkedQueue<OnDroneListener> droneListeners = new ConcurrentLinkedQueue<OnDroneListener>(); | ||
private final AtomicBoolean isDispatcherRunning = new AtomicBoolean(false); | ||
|
||
public void addDroneListener(OnDroneListener listener) { | ||
if (listener != null & !droneListeners.contains(listener)) | ||
droneListeners.add(listener); | ||
} | ||
private final Runnable eventDispatcher = new Runnable() { | ||
|
||
public void removeDroneListener(OnDroneListener listener) { | ||
if (listener != null && droneListeners.contains(listener)) | ||
droneListeners.remove(listener); | ||
} | ||
@Override | ||
public void run() { | ||
handler.removeCallbacks(this); | ||
|
||
final DroneEventsType event = eventQueue.poll(); | ||
if (event == null) { | ||
isDispatcherRunning.set(false); | ||
return; | ||
} | ||
|
||
public void notifyDroneEvent(DroneEventsType event) { | ||
if (event != null && !droneListeners.isEmpty()) { | ||
for (OnDroneListener listener : droneListeners) { | ||
listener.onDroneEvent(event, myDrone); | ||
} | ||
|
||
handler.removeCallbacks(this); | ||
handler.postDelayed(this, EVENT_DISPATCHING_DELAY); | ||
|
||
isDispatcherRunning.set(true); | ||
} | ||
} | ||
}; | ||
|
||
private final DroneInterfaces.Handler handler; | ||
|
||
public DroneEvents(Drone myDrone, DroneInterfaces.Handler handler) { | ||
super(myDrone); | ||
this.handler = handler; | ||
} | ||
|
||
private final ConcurrentLinkedQueue<OnDroneListener> droneListeners = new ConcurrentLinkedQueue<OnDroneListener>(); | ||
private final ConcurrentLinkedQueue<DroneEventsType> eventQueue = new ConcurrentLinkedQueue<>(); | ||
|
||
public void addDroneListener(OnDroneListener listener) { | ||
if (listener != null & !droneListeners.contains(listener)) | ||
droneListeners.add(listener); | ||
} | ||
|
||
public void removeDroneListener(OnDroneListener listener) { | ||
if (listener != null && droneListeners.contains(listener)) | ||
droneListeners.remove(listener); | ||
} | ||
|
||
public void notifyDroneEvent(DroneEventsType event) { | ||
if (event == null || droneListeners.isEmpty() || eventQueue.contains(event)) | ||
return; | ||
|
||
eventQueue.add(event); | ||
if (isDispatcherRunning.compareAndSet(false, true)) | ||
handler.postDelayed(eventDispatcher, EVENT_DISPATCHING_DELAY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters