From 79f3a77e1a145e48d73c1e1d9cc35f1fa86b33e8 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Mon, 10 Aug 2020 17:23:45 +0100 Subject: [PATCH] Annotate methods that should be called with a wake lock. --- .../api/lifecycle/LifecycleManager.java | 4 ++++ .../bramble/api/lifecycle/Service.java | 6 +++++- .../bramble/api/plugin/Plugin.java | 4 ++++ .../api/plugin/duplex/DuplexPlugin.java | 2 ++ .../api/plugin/simplex/SimplexPlugin.java | 3 +++ .../bramble/api/system/TaskScheduler.java | 6 ++++++ .../bramble/api/system/Wakeful.java | 19 +++++++++++++++++++ .../bramble/io/TimeoutMonitorImpl.java | 2 ++ .../bramble/plugin/PollerImpl.java | 1 + .../rendezvous/RendezvousPollerImpl.java | 3 +++ .../transport/TransportKeyManagerImpl.java | 4 ++++ .../briar/feed/FeedManagerImpl.java | 4 +++- 12 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 bramble-api/src/main/java/org/briarproject/bramble/api/system/Wakeful.java diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java index b5980e4ba..ab14b669f 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/LifecycleManager.java @@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; +import org.briarproject.bramble.api.system.Wakeful; import java.util.concurrent.ExecutorService; @@ -65,6 +66,7 @@ public interface LifecycleManager { * Opens the {@link DatabaseComponent} using the given key and starts any * registered {@link Service Services}. */ + @Wakeful StartResult startServices(SecretKey dbKey); /** @@ -72,6 +74,7 @@ public interface LifecycleManager { * registered {@link ExecutorService ExecutorServices}, and closes the * {@link DatabaseComponent}. */ + @Wakeful void stopServices(); /** @@ -104,6 +107,7 @@ public interface LifecycleManager { * * @param txn A read-write transaction */ + @Wakeful void onDatabaseOpened(Transaction txn) throws DbException; } } \ No newline at end of file diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/Service.java b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/Service.java index fcdcab912..849ebdd17 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/Service.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/lifecycle/Service.java @@ -1,16 +1,20 @@ package org.briarproject.bramble.api.lifecycle; +import org.briarproject.bramble.api.system.Wakeful; + public interface Service { /** - * Starts the service.This method must not be called concurrently with + * Starts the service. This method must not be called concurrently with * {@link #stopService()}. */ + @Wakeful void startService() throws ServiceException; /** * Stops the service. This method must not be called concurrently with * {@link #startService()}. */ + @Wakeful void stopService() throws ServiceException; } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/Plugin.java b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/Plugin.java index 9d2d50600..1318bcca4 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/Plugin.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/Plugin.java @@ -4,6 +4,7 @@ import org.briarproject.bramble.api.Pair; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.settings.SettingsManager; +import org.briarproject.bramble.api.system.Wakeful; import java.util.Collection; @@ -70,11 +71,13 @@ public interface Plugin { /** * Starts the plugin. */ + @Wakeful void start() throws PluginException; /** * Stops the plugin. */ + @Wakeful void stop() throws PluginException; /** @@ -106,6 +109,7 @@ public interface Plugin { * Attempts to create connections using the given transport properties, * passing any created connections to the corresponding handlers. */ + @Wakeful void poll(Collection> properties); } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/duplex/DuplexPlugin.java b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/duplex/DuplexPlugin.java index ce0e5787e..9e14e7ab5 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/duplex/DuplexPlugin.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/duplex/DuplexPlugin.java @@ -8,6 +8,7 @@ import org.briarproject.bramble.api.plugin.Plugin; import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.rendezvous.KeyMaterialSource; import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint; +import org.briarproject.bramble.api.system.Wakeful; import javax.annotation.Nullable; @@ -21,6 +22,7 @@ public interface DuplexPlugin extends Plugin { * Attempts to create and return a connection using the given transport * properties. Returns null if a connection cannot be created. */ + @Wakeful @Nullable DuplexTransportConnection createConnection(TransportProperties p); diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/simplex/SimplexPlugin.java b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/simplex/SimplexPlugin.java index 7f0ab0141..9df61968d 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/simplex/SimplexPlugin.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/simplex/SimplexPlugin.java @@ -5,6 +5,7 @@ import org.briarproject.bramble.api.plugin.Plugin; import org.briarproject.bramble.api.plugin.TransportConnectionReader; import org.briarproject.bramble.api.plugin.TransportConnectionWriter; import org.briarproject.bramble.api.properties.TransportProperties; +import org.briarproject.bramble.api.system.Wakeful; import javax.annotation.Nullable; @@ -18,6 +19,7 @@ public interface SimplexPlugin extends Plugin { * Attempts to create and return a reader for the given transport * properties. Returns null if a reader cannot be created. */ + @Wakeful @Nullable TransportConnectionReader createReader(TransportProperties p); @@ -25,6 +27,7 @@ public interface SimplexPlugin extends Plugin { * Attempts to create and return a writer for the given transport * properties. Returns null if a writer cannot be created. */ + @Wakeful @Nullable TransportConnectionWriter createWriter(TransportProperties p); } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/system/TaskScheduler.java b/bramble-api/src/main/java/org/briarproject/bramble/api/system/TaskScheduler.java index 9d72c4cfb..7e1490cfa 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/system/TaskScheduler.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/system/TaskScheduler.java @@ -14,6 +14,9 @@ public interface TaskScheduler { /** * Submits the given task to the given executor after the given delay. + *

+ * If the platform supports wake locks, a wake lock will be held while + * submitting and running the task. */ Future schedule(Runnable task, Executor executor, long delay, TimeUnit unit); @@ -22,6 +25,9 @@ public interface TaskScheduler { * Submits the given task to the given executor after the given delay, * and then repeatedly with the given interval between executions * (measured from the end of one execution to the beginning of the next). + *

+ * If the platform supports wake locks, a wake lock will be held while + * submitting and running the task. */ Future scheduleWithFixedDelay(Runnable task, Executor executor, long delay, long interval, TimeUnit unit); diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/system/Wakeful.java b/bramble-api/src/main/java/org/briarproject/bramble/api/system/Wakeful.java new file mode 100644 index 000000000..e0fa695c3 --- /dev/null +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/system/Wakeful.java @@ -0,0 +1,19 @@ +package org.briarproject.bramble.api.system; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Annotation for methods that must be called while holding a wake lock, if + * the platform supports wake locks. + */ +@Qualifier +@Target(METHOD) +@Retention(RUNTIME) +public @interface Wakeful { +} \ No newline at end of file diff --git a/bramble-core/src/main/java/org/briarproject/bramble/io/TimeoutMonitorImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/io/TimeoutMonitorImpl.java index e4bc71e1d..825ddc9da 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/io/TimeoutMonitorImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/io/TimeoutMonitorImpl.java @@ -4,6 +4,7 @@ import org.briarproject.bramble.api.io.TimeoutMonitor; import org.briarproject.bramble.api.lifecycle.IoExecutor; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.Wakeful; import java.io.IOException; import java.io.InputStream; @@ -75,6 +76,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor { } @IoExecutor + @Wakeful private void checkTimeouts() { List snapshot; synchronized (lock) { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java index 439e78efb..dbdd6e1d7 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java @@ -256,6 +256,7 @@ class PollerImpl implements Poller, EventListener { @Override @IoExecutor + @Wakeful public void run() { lock.lock(); try { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/rendezvous/RendezvousPollerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/rendezvous/RendezvousPollerImpl.java index 22f781617..7b0f21e8b 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/rendezvous/RendezvousPollerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/rendezvous/RendezvousPollerImpl.java @@ -42,6 +42,7 @@ import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedE import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.Wakeful; import java.security.GeneralSecurityException; import java.util.ArrayList; @@ -205,6 +206,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { } // Worker + @Wakeful private void poll() { removeExpiredPendingContacts(); for (PluginState ps : pluginStates.values()) poll(ps); @@ -235,6 +237,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { } // Worker + @Wakeful private void poll(PluginState ps) { if (ps.endpoints.isEmpty()) return; TransportId t = ps.plugin.getId(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java index f15fd82c7..f55fa7998 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java @@ -13,6 +13,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.Wakeful; import org.briarproject.bramble.api.transport.KeySetId; import org.briarproject.bramble.api.transport.StreamContext; import org.briarproject.bramble.api.transport.TransportKeySet; @@ -203,6 +204,7 @@ class TransportKeyManagerImpl implements TransportKeyManager { } @DatabaseExecutor + @Wakeful private void updateKeys() { try { db.transaction(false, this::updateKeys); @@ -441,6 +443,8 @@ class TransportKeyManagerImpl implements TransportKeyManager { } } + @DatabaseExecutor + @Wakeful private void updateKeys(Transaction txn) throws DbException { long now = clock.currentTimeMillis(); lock.lock(); diff --git a/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java index 713e2c090..b2ac62c83 100644 --- a/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java @@ -30,6 +30,7 @@ import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.Wakeful; import org.briarproject.bramble.util.StringUtils; import org.briarproject.briar.api.blog.Blog; import org.briarproject.briar.api.blog.BlogManager; @@ -284,7 +285,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook, } /** - * This method is called periodically from a background service. + * This method is called periodically by the task scheduler. * It fetches all available feeds and posts new entries to the respective * blog. *

@@ -292,6 +293,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook, * because fetching can take a long time * and we can not block the database that long. */ + @Wakeful void fetchFeeds() { if (!torActive) return; LOG.info("Updating RSS feeds...");