Work on connection establishment

This commit is contained in:
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.MuWireSettings
import com.muwire.core.hostcache.HostCache import com.muwire.core.hostcache.HostCache
import net.i2p.data.Destination
import net.i2p.util.ConcurrentHashSet
class ConnectionEstablisher { class ConnectionEstablisher {
private static final int CONCURRENT = 4
final EventBus eventBus final EventBus eventBus
final I2PConnector i2pConnector final I2PConnector i2pConnector
@@ -19,6 +24,8 @@ class ConnectionEstablisher {
final Timer timer final Timer timer
final ExecutorService executor final ExecutorService executor
final Set inProgress = new ConcurrentHashSet()
ConnectionEstablisher(EventBus eventBus, I2PConnector i2pConnector, MuWireSettings settings, ConnectionEstablisher(EventBus eventBus, I2PConnector i2pConnector, MuWireSettings settings,
ConnectionManager connectionManager, HostCache hostCache) { ConnectionManager connectionManager, HostCache hostCache) {
this.eventBus = eventBus this.eventBus = eventBus
@@ -27,7 +34,7 @@ class ConnectionEstablisher {
this.connectionManager = connectionManager this.connectionManager = connectionManager
this.hostCache = hostCache this.hostCache = hostCache
timer = new Timer("connection-timer",true) timer = new Timer("connection-timer",true)
executor = Executors.newFixedThreadPool(4, { r -> executor = Executors.newFixedThreadPool(CONCURRENT, { r ->
def rv = new Thread(r, true) def rv = new Thread(r, true)
rv.setName("connector-${System.currentTimeMillis()}") rv.setName("connector-${System.currentTimeMillis()}")
rv rv
@@ -46,6 +53,26 @@ class ConnectionEstablisher {
private void connectIfNeeded() { private void connectIfNeeded() {
if (!connectionManager.needsConnections()) if (!connectionManager.needsConnections())
return 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() { boolean needsConnections() {
return getConnections().size() < getDesiredConnections() return getConnections().size() < getDesiredConnections()
} }
abstract boolean isConnected(Destination d);
} }

View File

@@ -29,6 +29,11 @@ class LeafConnectionManager extends ConnectionManager {
protected int getDesiredConnections() { protected int getDesiredConnections() {
return maxConnections; 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() { protected int getDesiredConnections() {
return maxPeers / 2; return maxPeers / 2;
} }
@Override
public boolean isConnected(Destination d) {
// TODO Auto-generated method stub
return false;
}
} }