Work on connection establishment

pull/4/head
Zlatin Balevsky 2018-07-25 19:05:25 +01:00
parent b0b9430479
commit fd4ee74079
4 changed files with 41 additions and 2 deletions

View File

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

View File

@ -28,4 +28,6 @@ abstract class ConnectionManager {
boolean needsConnections() {
return getConnections().size() < getDesiredConnections()
}
abstract boolean isConnected(Destination d);
}

View File

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

View File

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