| 1 | ### Eclipse Workspace Patch 1.0
|
|---|
| 2 | #P livegps
|
|---|
| 3 | Index: src/livegps/LiveGpsSuppressor.java
|
|---|
| 4 | ===================================================================
|
|---|
| 5 | --- src/livegps/LiveGpsSuppressor.java (revision 0)
|
|---|
| 6 | +++ src/livegps/LiveGpsSuppressor.java (revision 0)
|
|---|
| 7 | @@ -0,0 +1,123 @@
|
|---|
| 8 | +package livegps;
|
|---|
| 9 | +
|
|---|
| 10 | +import java.util.logging.Logger;
|
|---|
| 11 | +
|
|---|
| 12 | +import org.openstreetmap.josm.Main;
|
|---|
| 13 | +
|
|---|
| 14 | +/**
|
|---|
| 15 | + * The LiveGpsSuppressor permits update events only once within a given timespan.
|
|---|
| 16 | + * This is useful, when too frequent updates consume large parts of the CPU resources (esp.
|
|---|
| 17 | + * on low-end devices, such as netbooks).
|
|---|
| 18 | + *
|
|---|
| 19 | + * Its own thread wakes up after the sleepTime and enables the allowUpdate flag.
|
|---|
| 20 | + * When another thread (the LiveGpsAcquirere) "asks for permission",
|
|---|
| 21 | + * the first call is permitted, but it also disables the updates for the following calls,
|
|---|
| 22 | + * until the sleepTime has elapsed.
|
|---|
| 23 | + *
|
|---|
| 24 | + * @author casualwalker
|
|---|
| 25 | + *
|
|---|
| 26 | + */
|
|---|
| 27 | +public class LiveGpsSuppressor implements Runnable {
|
|---|
| 28 | +
|
|---|
| 29 | + /**
|
|---|
| 30 | + * Default sleep time is 5 seconds.
|
|---|
| 31 | + */
|
|---|
| 32 | + private static final int DEFAULT_SLEEP_TIME = 5000;
|
|---|
| 33 | +
|
|---|
| 34 | + /**
|
|---|
| 35 | + * The currently used sleepTime.
|
|---|
| 36 | + */
|
|---|
| 37 | + private int sleepTime = DEFAULT_SLEEP_TIME;
|
|---|
| 38 | +
|
|---|
| 39 | + /**
|
|---|
| 40 | + * The flag allowUpdate is enabled once during the sleepTime.
|
|---|
| 41 | + */
|
|---|
| 42 | + private boolean allowUpdate = false;
|
|---|
| 43 | +
|
|---|
| 44 | + /**
|
|---|
| 45 | + * Controls if this thread is still in used.
|
|---|
| 46 | + */
|
|---|
| 47 | + boolean shutdownFlag = false;
|
|---|
| 48 | +
|
|---|
| 49 | + protected static final Logger log = Logger.getLogger(LiveGpsLayer.class
|
|---|
| 50 | + .getName());
|
|---|
| 51 | +
|
|---|
| 52 | + /**
|
|---|
| 53 | + * Run thread enables the allowUpdate flag once during its cycle.
|
|---|
| 54 | + * @see java.lang.Runnable#run()
|
|---|
| 55 | + */
|
|---|
| 56 | + public void run() {
|
|---|
| 57 | + initSleepTime();
|
|---|
| 58 | +
|
|---|
| 59 | + shutdownFlag = false;
|
|---|
| 60 | + while (!shutdownFlag) {
|
|---|
| 61 | + log.fine("Suppressor Thread running");
|
|---|
| 62 | + setAllowUpdate(true);
|
|---|
| 63 | +
|
|---|
| 64 | + try {
|
|---|
| 65 | + Thread.sleep(getSleepTime());
|
|---|
| 66 | + } catch (InterruptedException e) {
|
|---|
| 67 | + // TODO I never knew, how to handle this??? Probably just log
|
|---|
| 68 | + // and
|
|---|
| 69 | + // carry on
|
|---|
| 70 | + log
|
|---|
| 71 | + .warning("LiveGPSSuppressor thread was interrupted during sleep cycle.");
|
|---|
| 72 | + }
|
|---|
| 73 | + }
|
|---|
| 74 | +
|
|---|
| 75 | + }
|
|---|
| 76 | +
|
|---|
| 77 | + /**
|
|---|
| 78 | + * Retrieve the sleepTime from the configuration.
|
|---|
| 79 | + * If no such configuration key exists, it will be initialized here.
|
|---|
| 80 | + */
|
|---|
| 81 | + private void initSleepTime() {
|
|---|
| 82 | + // fetch it from the user setting, or use the default value.
|
|---|
| 83 | + sleepTime = Main.pref.getInteger("livegps.refreshinterval",
|
|---|
| 84 | + DEFAULT_SLEEP_TIME);
|
|---|
| 85 | + // creates the setting, if none present.
|
|---|
| 86 | + Main.pref.putInteger("livegps.refreshinterval", sleepTime);
|
|---|
| 87 | + }
|
|---|
| 88 | +
|
|---|
| 89 | + /**
|
|---|
| 90 | + * Set the allowUpdate flag. May only privately accessible!
|
|---|
| 91 | + * @param allowUpdate the allowUpdate to set
|
|---|
| 92 | + */
|
|---|
| 93 | + private synchronized void setAllowUpdate(boolean allowUpdate) {
|
|---|
| 94 | + this.allowUpdate = allowUpdate;
|
|---|
| 95 | + }
|
|---|
| 96 | +
|
|---|
| 97 | + /**
|
|---|
| 98 | + * Query, if an update is currently allowed.
|
|---|
| 99 | + * When it is allowed, it will disable the allowUpdate flag as a side effect.
|
|---|
| 100 | + * (this means, one thread got to issue an update event)
|
|---|
| 101 | + *
|
|---|
| 102 | + * @return true, if an update is currently allowed; false, if the update shall be suppressed.
|
|---|
| 103 | + */
|
|---|
| 104 | + public synchronized boolean isAllowUpdate() {
|
|---|
| 105 | +
|
|---|
| 106 | + if (allowUpdate) {
|
|---|
| 107 | + log.fine("Update PERMITTED");
|
|---|
| 108 | + allowUpdate = false;
|
|---|
| 109 | + return true;
|
|---|
| 110 | + } else {
|
|---|
| 111 | + log.fine("Update DENIED");
|
|---|
| 112 | + return false;
|
|---|
| 113 | + }
|
|---|
| 114 | + }
|
|---|
| 115 | +
|
|---|
| 116 | + /**
|
|---|
| 117 | + * Shut this thread down.
|
|---|
| 118 | + */
|
|---|
| 119 | + public void shutdown() {
|
|---|
| 120 | + shutdownFlag = true;
|
|---|
| 121 | + }
|
|---|
| 122 | +
|
|---|
| 123 | + /**
|
|---|
| 124 | + * @return the defaultSleepTime
|
|---|
| 125 | + */
|
|---|
| 126 | + private int getSleepTime() {
|
|---|
| 127 | + return this.sleepTime;
|
|---|
| 128 | + }
|
|---|
| 129 | +
|
|---|
| 130 | +}
|
|---|