From b7518a8b63c7d914fbd0f3632dc2b484fd5bb5e2 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 25 Jul 2018 14:46:06 +0100 Subject: [PATCH] skeleton of connection establisher --- .../connection/ConnectionEstablisher.groovy | 51 +++++++++++++++++++ .../core/connection/ConnectionManager.groovy | 6 +++ .../connection/LeafConnectionManager.groovy | 7 +++ .../UltrapeerConnectionManager.groovy | 4 ++ 4 files changed, 68 insertions(+) create mode 100644 core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy new file mode 100644 index 00000000..5d838957 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionEstablisher.groovy @@ -0,0 +1,51 @@ +package com.muwire.core.connection + +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors +import java.util.concurrent.ThreadFactory + +import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings +import com.muwire.core.hostcache.HostCache + +class ConnectionEstablisher { + + final EventBus eventBus + final I2PConnector i2pConnector + final MuWireSettings settings + final ConnectionManager connectionManager + final HostCache hostCache + + final Timer timer + final ExecutorService executor + + ConnectionEstablisher(EventBus eventBus, I2PConnector i2pConnector, MuWireSettings settings, + ConnectionManager connectionManager, HostCache hostCache) { + this.eventBus = eventBus + this.i2pConnector = i2pConnector + this.settings = settings + this.connectionManager = connectionManager + this.hostCache = hostCache + timer = new Timer("connection-timer",true) + executor = Executors.newFixedThreadPool(4, { r -> + def rv = new Thread(r, true) + rv.setName("connector-${System.currentTimeMillis()}") + rv + } as ThreadFactory) + } + + void start() { + timer.schedule({connectIfNeeded()} as TimerTask, 100, 1000) + } + + void stop() { + timer.cancel() + executor.shutdownNow() + } + + private void connectIfNeeded() { + if (!connectionManager.needsConnections()) + return + + } +} 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 7af84855..660da9c5 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy @@ -22,4 +22,10 @@ abstract class ConnectionManager { abstract void drop(Destination d) abstract Collection getConnections() + + protected abstract int getDesiredConnections() + + boolean needsConnections() { + return getConnections().size() < getDesiredConnections() + } } 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 78cfd7d2..9c4187f9 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy @@ -24,4 +24,11 @@ class LeafConnectionManager extends ConnectionManager { // TODO implement [] } + + @Override + protected int getDesiredConnections() { + return maxConnections; + } + + } 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 a6b2ee3b..e523c6a8 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -36,4 +36,8 @@ class UltrapeerConnectionManager extends ConnectionManager { // TODO implement true } + @Override + protected int getDesiredConnections() { + return maxPeers / 2; + } }