Add RemovableDriveManager method.

This commit is contained in:
akwizgran
2021-06-17 13:01:33 +01:00
committed by Torsten Grote
parent 796cbcaf4b
commit 1f1ea8f3ed
3 changed files with 20 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package org.briarproject.bramble.api.plugin.file; package org.briarproject.bramble.api.plugin.file;
import org.briarproject.bramble.api.contact.ContactId; 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.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
@@ -37,4 +38,9 @@ public interface RemovableDriveManager {
* ignored. * ignored.
*/ */
RemovableDriveTask startWriterTask(ContactId c, TransportProperties p); RemovableDriveTask startWriterTask(ContactId c, TransportProperties p);
/**
* Returns true if there is anything to send to the given contact.
*/
boolean isWriterTaskNeeded(ContactId c) throws DbException;
} }

View File

@@ -1,6 +1,8 @@
package org.briarproject.bramble.plugin.file; package org.briarproject.bramble.plugin.file;
import org.briarproject.bramble.api.contact.ContactId; 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.lifecycle.IoExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.file.RemovableDriveManager; import org.briarproject.bramble.api.plugin.file.RemovableDriveManager;
@@ -14,12 +16,15 @@ import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject; import javax.inject.Inject;
import static org.briarproject.bramble.plugin.file.RemovableDrivePluginFactory.MAX_LATENCY;
@ThreadSafe @ThreadSafe
@NotNullByDefault @NotNullByDefault
class RemovableDriveManagerImpl class RemovableDriveManagerImpl
implements RemovableDriveManager, RemovableDriveTaskRegistry { implements RemovableDriveManager, RemovableDriveTaskRegistry {
private final Executor ioExecutor; private final Executor ioExecutor;
private final DatabaseComponent db;
private final RemovableDriveTaskFactory taskFactory; private final RemovableDriveTaskFactory taskFactory;
private final Object lock = new Object(); private final Object lock = new Object();
@@ -30,8 +35,9 @@ class RemovableDriveManagerImpl
@Inject @Inject
RemovableDriveManagerImpl(@IoExecutor Executor ioExecutor, RemovableDriveManagerImpl(@IoExecutor Executor ioExecutor,
RemovableDriveTaskFactory taskFactory) { DatabaseComponent db, RemovableDriveTaskFactory taskFactory) {
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.db = db;
this.taskFactory = taskFactory; this.taskFactory = taskFactory;
} }
@@ -74,6 +80,12 @@ class RemovableDriveManagerImpl
return created; return created;
} }
@Override
public boolean isWriterTaskNeeded(ContactId c) throws DbException {
return db.transactionWithResult(true, txn ->
db.containsAnythingToSend(txn, c, MAX_LATENCY, true));
}
@Override @Override
public void removeReader(RemovableDriveTask task) { public void removeReader(RemovableDriveTask task) {
synchronized (lock) { synchronized (lock) {

View File

@@ -17,7 +17,7 @@ import static org.briarproject.bramble.api.plugin.file.RemovableDriveConstants.I
@NotNullByDefault @NotNullByDefault
public class RemovableDrivePluginFactory implements SimplexPluginFactory { 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 @Inject
RemovableDrivePluginFactory() { RemovableDrivePluginFactory() {