From fd4ee740794b57d409110a7646e3d121b932d4e3 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 25 Jul 2018 19:05:25 +0100 Subject: [PATCH] Work on connection establishment --- .../connection/ConnectionEstablisher.groovy | 29 ++++++++++++++++++- .../core/connection/ConnectionManager.groovy | 2 ++ .../connection/LeafConnectionManager.groovy | 7 ++++- .../UltrapeerConnectionManager.groovy | 5 ++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy index 5d838957..f7ad7505 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy @@ -8,7 +8,12 @@ import com.muwire.core.EventBus import com.muwire.core.MuWireSettings import com.muwire.core.hostcache.HostCache +import net.i2p.data.Destination +import net.i2p.util.ConcurrentHashSet + class ConnectionEstablisher { + + private static final int CONCURRENT = 4 final EventBus eventBus final I2PConnector i2pConnector @@ -19,6 +24,8 @@ class ConnectionEstablisher { final Timer timer final ExecutorService executor + final Set inProgress = new ConcurrentHashSet() + ConnectionEstablisher(EventBus eventBus, I2PConnector i2pConnector, MuWireSettings settings, ConnectionManager connectionManager, HostCache hostCache) { this.eventBus = eventBus @@ -27,7 +34,7 @@ class ConnectionEstablisher { this.connectionManager = connectionManager this.hostCache = hostCache timer = new Timer("connection-timer",true) - executor = Executors.newFixedThreadPool(4, { r -> + executor = Executors.newFixedThreadPool(CONCURRENT, { r -> def rv = new Thread(r, true) rv.setName("connector-${System.currentTimeMillis()}") rv @@ -46,6 +53,26 @@ class ConnectionEstablisher { private void connectIfNeeded() { if (!connectionManager.needsConnections()) return + if (inProgress.size() >= CONCURRENT) + return + + def toTry + while(true) { + toTry = hostCache.getHosts(1) + if (toTry.isEmpty()) + return + toTry = toTry[0] + if (!connectionManager.isConnected(toTry) && + !inProgress.contains(toTry)) { + break + } + } + inProgress.add(toTry) + executor.execute({connect(toTry)} as Runnable) + } + + private void connect(Destination toTry) { + // TODO: implement } } diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy index 660da9c5..469db8a8 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy @@ -28,4 +28,6 @@ abstract class ConnectionManager { boolean needsConnections() { return getConnections().size() < getDesiredConnections() } + + abstract boolean isConnected(Destination d); } diff --git a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy index 9c4187f9..95353d3d 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy @@ -29,6 +29,11 @@ class LeafConnectionManager extends ConnectionManager { protected int getDesiredConnections() { return maxConnections; } - + + @Override + public boolean isConnected(Destination d) { + // TODO Auto-generated method stub + return false; + } } diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy index e523c6a8..c16d04cc 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -40,4 +40,9 @@ class UltrapeerConnectionManager extends ConnectionManager { protected int getDesiredConnections() { return maxPeers / 2; } + @Override + public boolean isConnected(Destination d) { + // TODO Auto-generated method stub + return false; + } }