Work on connection establishment
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,4 +28,6 @@ abstract class ConnectionManager {
|
|||||||
boolean needsConnections() {
|
boolean needsConnections() {
|
||||||
return getConnections().size() < getDesiredConnections()
|
return getConnections().size() < getDesiredConnections()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract boolean isConnected(Destination d);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user