skeleton of connection establisher

pull/4/head
Zlatin Balevsky 2018-07-25 14:46:06 +01:00
parent cf6a9b9314
commit b7518a8b63
4 changed files with 68 additions and 0 deletions

View File

@ -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
}
}

View File

@ -22,4 +22,10 @@ abstract class ConnectionManager {
abstract void drop(Destination d)
abstract Collection<Connection> getConnections()
protected abstract int getDesiredConnections()
boolean needsConnections() {
return getConnections().size() < getDesiredConnections()
}
}

View File

@ -24,4 +24,11 @@ class LeafConnectionManager extends ConnectionManager {
// TODO implement
[]
}
@Override
protected int getDesiredConnections() {
return maxConnections;
}
}

View File

@ -36,4 +36,8 @@ class UltrapeerConnectionManager extends ConnectionManager {
// TODO implement
true
}
@Override
protected int getDesiredConnections() {
return maxPeers / 2;
}
}