From 1f1ea8f3edf63aa0ed413983573d0b38772ca015 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 17 Jun 2021 13:01:33 +0100 Subject: [PATCH] Add RemovableDriveManager method. --- .../api/plugin/file/RemovableDriveManager.java | 6 ++++++ .../plugin/file/RemovableDriveManagerImpl.java | 14 +++++++++++++- .../plugin/file/RemovableDrivePluginFactory.java | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/file/RemovableDriveManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/file/RemovableDriveManager.java index cc14a08bb..8c74497b0 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/file/RemovableDriveManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/file/RemovableDriveManager.java @@ -1,6 +1,7 @@ package org.briarproject.bramble.api.plugin.file; import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.properties.TransportProperties; @@ -37,4 +38,9 @@ public interface RemovableDriveManager { * ignored. */ RemovableDriveTask startWriterTask(ContactId c, TransportProperties p); + + /** + * Returns true if there is anything to send to the given contact. + */ + boolean isWriterTaskNeeded(ContactId c) throws DbException; } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDriveManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDriveManagerImpl.java index 78490a045..2c96d5d53 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDriveManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDriveManagerImpl.java @@ -1,6 +1,8 @@ package org.briarproject.bramble.plugin.file; import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.db.DatabaseComponent; +import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.lifecycle.IoExecutor; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.file.RemovableDriveManager; @@ -14,12 +16,15 @@ import javax.annotation.concurrent.GuardedBy; import javax.annotation.concurrent.ThreadSafe; import javax.inject.Inject; +import static org.briarproject.bramble.plugin.file.RemovableDrivePluginFactory.MAX_LATENCY; + @ThreadSafe @NotNullByDefault class RemovableDriveManagerImpl implements RemovableDriveManager, RemovableDriveTaskRegistry { private final Executor ioExecutor; + private final DatabaseComponent db; private final RemovableDriveTaskFactory taskFactory; private final Object lock = new Object(); @@ -30,8 +35,9 @@ class RemovableDriveManagerImpl @Inject RemovableDriveManagerImpl(@IoExecutor Executor ioExecutor, - RemovableDriveTaskFactory taskFactory) { + DatabaseComponent db, RemovableDriveTaskFactory taskFactory) { this.ioExecutor = ioExecutor; + this.db = db; this.taskFactory = taskFactory; } @@ -74,6 +80,12 @@ class RemovableDriveManagerImpl return created; } + @Override + public boolean isWriterTaskNeeded(ContactId c) throws DbException { + return db.transactionWithResult(true, txn -> + db.containsAnythingToSend(txn, c, MAX_LATENCY, true)); + } + @Override public void removeReader(RemovableDriveTask task) { synchronized (lock) { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDrivePluginFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDrivePluginFactory.java index 6f1ad7564..d0bc374ed 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDrivePluginFactory.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/file/RemovableDrivePluginFactory.java @@ -17,7 +17,7 @@ import static org.briarproject.bramble.api.plugin.file.RemovableDriveConstants.I @NotNullByDefault public class RemovableDrivePluginFactory implements SimplexPluginFactory { - private static final int MAX_LATENCY = (int) DAYS.toMillis(14); + static final int MAX_LATENCY = (int) DAYS.toMillis(14); @Inject RemovableDrivePluginFactory() {