mirror of https://github.com/zlatinb/muwire
logic in connection managers to check if already connected. Prevent infinite connect loop. Fix Connction constructors
parent
7c0f5b4e88
commit
b2796a3d40
|
@ -87,6 +87,7 @@ class Core {
|
|||
ConnectionManager connectionManager = props.isLeaf() ?
|
||||
new LeafConnectionManager(eventBus,3) : new UltrapeerConnectionManager(eventBus, 512, 512)
|
||||
eventBus.register(TrustEvent.class, connectionManager)
|
||||
eventBus.register(ConnectionEvent.class, connectionManager)
|
||||
|
||||
log.info("initializing cache client")
|
||||
CacheClient cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000)
|
||||
|
|
|
@ -78,7 +78,7 @@ class ConnectionEstablisher {
|
|||
}
|
||||
if (toTry == null)
|
||||
return
|
||||
if (inProgress.add(toTry))
|
||||
if (!connectionManager.isConnected(toTry) && inProgress.add(toTry))
|
||||
executor.execute({connect(toTry)} as Runnable)
|
||||
}
|
||||
|
||||
|
|
|
@ -31,5 +31,7 @@ abstract class ConnectionManager {
|
|||
return getConnections().size() < getDesiredConnections()
|
||||
}
|
||||
|
||||
abstract boolean isConnected(Destination d);
|
||||
abstract boolean isConnected(Destination d)
|
||||
|
||||
abstract void onConnectionEvent(ConnectionEvent e)
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ import net.i2p.data.Destination
|
|||
*/
|
||||
class LeafConnection extends Connection {
|
||||
|
||||
public LeafConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream, Destination remoteSide) {
|
||||
super(eventBus, inputStream, outputStream, remoteSide, true);
|
||||
public LeafConnection(EventBus eventBus, Endpoint endpoint) {
|
||||
super(eventBus, endpoint, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
package com.muwire.core.connection
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
import com.muwire.core.EventBus
|
||||
|
||||
import groovy.util.logging.Log
|
||||
import net.i2p.data.Destination
|
||||
|
||||
@Log
|
||||
class LeafConnectionManager extends ConnectionManager {
|
||||
|
||||
final int maxConnections
|
||||
|
||||
final Map<Destination, UltrapeerConnection> connections = new ConcurrentHashMap()
|
||||
|
||||
public LeafConnectionManager(EventBus eventBus, int maxConnections) {
|
||||
super(eventBus)
|
||||
this.maxConnections = maxConnections
|
||||
|
@ -32,8 +38,18 @@ class LeafConnectionManager extends ConnectionManager {
|
|||
|
||||
@Override
|
||||
public boolean isConnected(Destination d) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
connections.containsKey(d)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionEvent(ConnectionEvent e) {
|
||||
if (e.incoming || e.leaf) {
|
||||
log.severe("Got inconsistent event as a leaf! $e")
|
||||
return
|
||||
}
|
||||
Connection c = new UltrapeerConnection(eventBus, e.endpoint)
|
||||
// TODO: start and stuff
|
||||
connections.put(e.endpoint.destination, c)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ import net.i2p.data.Destination
|
|||
*/
|
||||
class PeerConnection extends Connection {
|
||||
|
||||
public PeerConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream, Destination remoteSide,
|
||||
public PeerConnection(EventBus eventBus, Endpoint endpoint,
|
||||
boolean incoming) {
|
||||
super(eventBus, inputStream, outputStream, remoteSide, incoming)
|
||||
super(eventBus, endpoint, incoming)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,9 +15,8 @@ import net.i2p.data.Destination
|
|||
*/
|
||||
class UltrapeerConnection extends Connection {
|
||||
|
||||
public UltrapeerConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream,
|
||||
Destination remoteSide) {
|
||||
super(eventBus, inputStream, outputStream, remoteSide, false)
|
||||
public UltrapeerConnection(EventBus eventBus, Endpoint endpoint) {
|
||||
super(eventBus, endpoint, false)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
package com.muwire.core.connection
|
||||
|
||||
import java.util.Collection
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
import com.muwire.core.EventBus
|
||||
|
||||
import groovy.util.logging.Log
|
||||
import net.i2p.data.Destination
|
||||
|
||||
@Log
|
||||
class UltrapeerConnectionManager extends ConnectionManager {
|
||||
|
||||
final int maxPeers, maxLeafs
|
||||
|
||||
final Map<Destination, PeerConnection> peerConnections = new ConcurrentHashMap()
|
||||
final Map<Destination, LeafConnection> leafConnections = new ConcurrentHashMap()
|
||||
|
||||
UltrapeerConnectionManager() {}
|
||||
|
||||
public UltrapeerConnectionManager(EventBus eventBus, int maxPeers, int maxLeafs) {
|
||||
|
@ -44,7 +50,19 @@ class UltrapeerConnectionManager extends ConnectionManager {
|
|||
}
|
||||
@Override
|
||||
public boolean isConnected(Destination d) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
peerConnections.containsKey(d) || leafConnections.containsKey(d)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionEvent(ConnectionEvent e) {
|
||||
if (!e.incoming && e.leaf) {
|
||||
log.severe("Inconsistent event $e")
|
||||
return
|
||||
}
|
||||
|
||||
Connection c = e.leaf ? new LeafConnection(eventBus, e.endpoint) : new PeerConnection(eventBus, e.endpoint, e.incoming)
|
||||
// TODO: start and stuff
|
||||
def map = e.leaf ? leafConnections : peerConnections
|
||||
map.put(e.endpoint.destination, c)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue