mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Refactor transport preferences.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.connection;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.connection.InterruptibleConnection;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
@@ -45,7 +44,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
getLogger(ConnectionRegistryImpl.class.getName());
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final Map<TransportId, List<TransportId>> betterTransports;
|
||||
private final Map<TransportId, List<TransportId>> transportPrefs;
|
||||
|
||||
private final Object lock = new Object();
|
||||
@GuardedBy("lock")
|
||||
@@ -56,26 +55,11 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
@Inject
|
||||
ConnectionRegistryImpl(EventBus eventBus, PluginConfig pluginConfig) {
|
||||
this.eventBus = eventBus;
|
||||
betterTransports = new HashMap<>();
|
||||
initTransportPreferences(pluginConfig.getTransportPreferences());
|
||||
transportPrefs = pluginConfig.getTransportPreferences();
|
||||
contactConnections = new HashMap<>();
|
||||
connectedPendingContacts = new HashSet<>();
|
||||
}
|
||||
|
||||
private void initTransportPreferences(
|
||||
List<Pair<TransportId, TransportId>> prefs) {
|
||||
for (Pair<TransportId, TransportId> pair : prefs) {
|
||||
TransportId better = pair.getFirst();
|
||||
TransportId worse = pair.getSecond();
|
||||
List<TransportId> betterList = betterTransports.get(worse);
|
||||
if (betterList == null) {
|
||||
betterList = new ArrayList<>();
|
||||
betterTransports.put(worse, betterList);
|
||||
}
|
||||
betterList.add(better);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerConnection(ContactId c, TransportId t,
|
||||
InterruptibleConnection conn, boolean incoming) {
|
||||
@@ -146,7 +130,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
}
|
||||
|
||||
private List<TransportId> getBetterTransports(TransportId t) {
|
||||
List<TransportId> better = betterTransports.get(t);
|
||||
List<TransportId> better = transportPrefs.get(t);
|
||||
return better == null ? emptyList() : better;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.connection;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.connection.InterruptibleConnection;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
@@ -22,7 +21,9 @@ import org.junit.Test;
|
||||
import java.util.Collection;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
@@ -60,7 +61,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
public void testRegisterMultipleConnections() {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(emptyList()));
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -141,7 +142,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
public void testRegisterMultipleContacts() {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(emptyList()));
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -183,7 +184,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(
|
||||
singletonList(new Pair<>(transportId2, transportId1))));
|
||||
singletonMap(transportId1, singletonList(transportId2))));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -256,7 +257,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(
|
||||
singletonList(new Pair<>(transportId1, transportId2))));
|
||||
singletonMap(transportId2, singletonList(transportId1))));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -357,7 +358,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(
|
||||
singletonList(new Pair<>(transportId2, transportId1))));
|
||||
singletonMap(transportId1, singletonList(transportId2))));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -452,7 +453,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
public void testNewConnectionIsInterruptedIfOldConnectionHasHigherPriority() {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(emptyList()));
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -517,7 +518,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
public void testOldConnectionIsInterruptedIfNewConnectionHasHigherPriority() {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(emptyList()));
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
@@ -567,7 +568,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
public void testRegisterAndUnregisterPendingContacts() {
|
||||
context.checking(new Expectations() {{
|
||||
allowing(pluginConfig).getTransportPreferences();
|
||||
will(returnValue(emptyList()));
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c =
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||
@@ -12,13 +11,14 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
|
||||
@@ -89,11 +89,11 @@ public class TestPluginConfigModule {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Pair<TransportId, TransportId>> getTransportPreferences() {
|
||||
return emptyList();
|
||||
public Map<TransportId, List<TransportId>> getTransportPreferences() {
|
||||
return emptyMap();
|
||||
}
|
||||
|
||||
};
|
||||
return pluginConfig;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user