From 0c338b362e96d8d70fdf1ac4be8e0373bd6ae247 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 13 May 2020 15:35:03 +0100 Subject: [PATCH] Add InterruptibleConnection interface for easier testing. --- .../bramble/connection/ConnectionChooser.java | 14 ++++++++++++-- .../connection/ConnectionChooserImpl.java | 10 +++++----- .../connection/DuplexSyncConnection.java | 6 ++++-- .../connection/InterruptibleConnection.java | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 bramble-core/src/main/java/org/briarproject/bramble/connection/InterruptibleConnection.java diff --git a/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooser.java b/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooser.java index da26c904f..300339878 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooser.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooser.java @@ -5,18 +5,28 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.sync.Priority; +/** + * Chooses one connection per contact and transport to keep open and closes + * any other connections. + */ @NotNullByDefault interface ConnectionChooser { /** * Adds the given connection to the chooser with the given priority. + *

+ * If the chooser has a connection with the same contact and transport and + * a lower {@link Priority priority}, that connection will be + * {@link InterruptibleConnection#interruptOutgoingSession() interrupted}. + * If the chooser has a connection with the same contact and transport and + * a higher priority, the newly added connection will be interrupted. */ - void addConnection(ContactId c, TransportId t, DuplexSyncConnection conn, + void addConnection(ContactId c, TransportId t, InterruptibleConnection conn, Priority p); /** * Removes the given connection from the chooser. */ void removeConnection(ContactId c, TransportId t, - DuplexSyncConnection conn); + InterruptibleConnection conn); } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooserImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooserImpl.java index 0832611c2..b73f74fed 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooserImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/connection/ConnectionChooserImpl.java @@ -31,8 +31,8 @@ class ConnectionChooserImpl implements ConnectionChooser { @Override public void addConnection(ContactId c, TransportId t, - DuplexSyncConnection conn, Priority p) { - DuplexSyncConnection close = null; + InterruptibleConnection conn, Priority p) { + InterruptibleConnection close = null; synchronized (lock) { Key k = new Key(c, t); Value best = bestConnections.get(k); @@ -52,7 +52,7 @@ class ConnectionChooserImpl implements ConnectionChooser { @Override public void removeConnection(ContactId c, TransportId t, - DuplexSyncConnection conn) { + InterruptibleConnection conn) { synchronized (lock) { Key k = new Key(c, t); Value best = bestConnections.get(k); @@ -89,10 +89,10 @@ class ConnectionChooserImpl implements ConnectionChooser { private static class Value { - private final DuplexSyncConnection connection; + private final InterruptibleConnection connection; private final Priority priority; - private Value(DuplexSyncConnection connection, Priority priority) { + private Value(InterruptibleConnection connection, Priority priority) { this.connection = connection; this.priority = priority; } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/connection/DuplexSyncConnection.java b/bramble-core/src/main/java/org/briarproject/bramble/connection/DuplexSyncConnection.java index 2f5415d26..778e7b4d8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/connection/DuplexSyncConnection.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/connection/DuplexSyncConnection.java @@ -27,7 +27,8 @@ import javax.annotation.concurrent.GuardedBy; import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull; @NotNullByDefault -abstract class DuplexSyncConnection extends SyncConnection { +abstract class DuplexSyncConnection extends SyncConnection + implements InterruptibleConnection { final Executor ioExecutor; final ConnectionChooser connectionChooser; @@ -44,7 +45,8 @@ abstract class DuplexSyncConnection extends SyncConnection { @GuardedBy("interruptLock") private boolean interruptWaiting = false; - void interruptOutgoingSession() { + @Override + public void interruptOutgoingSession() { synchronized (interruptLock) { if (outgoingSession == null) interruptWaiting = true; else outgoingSession.interrupt(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/connection/InterruptibleConnection.java b/bramble-core/src/main/java/org/briarproject/bramble/connection/InterruptibleConnection.java new file mode 100644 index 000000000..e9924964d --- /dev/null +++ b/bramble-core/src/main/java/org/briarproject/bramble/connection/InterruptibleConnection.java @@ -0,0 +1,19 @@ +package org.briarproject.bramble.connection; + +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; + +/** + * A duplex sync connection that can be closed by interrupting its outgoing + * sync session. + */ +@NotNullByDefault +interface InterruptibleConnection { + + /** + * Interrupts the connection's outgoing sync session. If the underlying + * transport connection is alive and the remote peer is cooperative, this + * should result in both sync sessions ending and the connection being + * cleanly closed. + */ + void interruptOutgoingSession(); +}