logic in connection managers to check if already connected. Prevent infinite connect loop. Fix Connction constructors

pull/4/head
Zlatin Balevsky 2018-07-26 19:03:13 +01:00
parent 7c0f5b4e88
commit b2796a3d40
8 changed files with 49 additions and 13 deletions

View File

@ -87,6 +87,7 @@ class Core {
ConnectionManager connectionManager = props.isLeaf() ? ConnectionManager connectionManager = props.isLeaf() ?
new LeafConnectionManager(eventBus,3) : new UltrapeerConnectionManager(eventBus, 512, 512) new LeafConnectionManager(eventBus,3) : new UltrapeerConnectionManager(eventBus, 512, 512)
eventBus.register(TrustEvent.class, connectionManager) eventBus.register(TrustEvent.class, connectionManager)
eventBus.register(ConnectionEvent.class, connectionManager)
log.info("initializing cache client") log.info("initializing cache client")
CacheClient cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000) CacheClient cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000)

View File

@ -78,7 +78,7 @@ class ConnectionEstablisher {
} }
if (toTry == null) if (toTry == null)
return return
if (inProgress.add(toTry)) if (!connectionManager.isConnected(toTry) && inProgress.add(toTry))
executor.execute({connect(toTry)} as Runnable) executor.execute({connect(toTry)} as Runnable)
} }

View File

@ -31,5 +31,7 @@ abstract class ConnectionManager {
return getConnections().size() < getDesiredConnections() return getConnections().size() < getDesiredConnections()
} }
abstract boolean isConnected(Destination d); abstract boolean isConnected(Destination d)
abstract void onConnectionEvent(ConnectionEvent e)
} }

View File

@ -14,8 +14,8 @@ import net.i2p.data.Destination
*/ */
class LeafConnection extends Connection { class LeafConnection extends Connection {
public LeafConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream, Destination remoteSide) { public LeafConnection(EventBus eventBus, Endpoint endpoint) {
super(eventBus, inputStream, outputStream, remoteSide, true); super(eventBus, endpoint, true);
} }
} }

View File

@ -1,12 +1,18 @@
package com.muwire.core.connection package com.muwire.core.connection
import java.util.concurrent.ConcurrentHashMap
import com.muwire.core.EventBus import com.muwire.core.EventBus
import groovy.util.logging.Log
import net.i2p.data.Destination import net.i2p.data.Destination
@Log
class LeafConnectionManager extends ConnectionManager { class LeafConnectionManager extends ConnectionManager {
final int maxConnections final int maxConnections
final Map<Destination, UltrapeerConnection> connections = new ConcurrentHashMap()
public LeafConnectionManager(EventBus eventBus, int maxConnections) { public LeafConnectionManager(EventBus eventBus, int maxConnections) {
super(eventBus) super(eventBus)
@ -32,8 +38,18 @@ class LeafConnectionManager extends ConnectionManager {
@Override @Override
public boolean isConnected(Destination d) { public boolean isConnected(Destination d) {
// TODO Auto-generated method stub connections.containsKey(d)
return false; }
@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)
} }
} }

View File

@ -13,9 +13,9 @@ import net.i2p.data.Destination
*/ */
class PeerConnection extends Connection { class PeerConnection extends Connection {
public PeerConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream, Destination remoteSide, public PeerConnection(EventBus eventBus, Endpoint endpoint,
boolean incoming) { boolean incoming) {
super(eventBus, inputStream, outputStream, remoteSide, incoming) super(eventBus, endpoint, incoming)
} }
} }

View File

@ -15,9 +15,8 @@ import net.i2p.data.Destination
*/ */
class UltrapeerConnection extends Connection { class UltrapeerConnection extends Connection {
public UltrapeerConnection(EventBus eventBus, InputStream inputStream, OutputStream outputStream, public UltrapeerConnection(EventBus eventBus, Endpoint endpoint) {
Destination remoteSide) { super(eventBus, endpoint, false)
super(eventBus, inputStream, outputStream, remoteSide, false)
} }
} }

View File

@ -1,15 +1,21 @@
package com.muwire.core.connection package com.muwire.core.connection
import java.util.Collection import java.util.Collection
import java.util.concurrent.ConcurrentHashMap
import com.muwire.core.EventBus import com.muwire.core.EventBus
import groovy.util.logging.Log
import net.i2p.data.Destination import net.i2p.data.Destination
@Log
class UltrapeerConnectionManager extends ConnectionManager { class UltrapeerConnectionManager extends ConnectionManager {
final int maxPeers, maxLeafs final int maxPeers, maxLeafs
final Map<Destination, PeerConnection> peerConnections = new ConcurrentHashMap()
final Map<Destination, LeafConnection> leafConnections = new ConcurrentHashMap()
UltrapeerConnectionManager() {} UltrapeerConnectionManager() {}
public UltrapeerConnectionManager(EventBus eventBus, int maxPeers, int maxLeafs) { public UltrapeerConnectionManager(EventBus eventBus, int maxPeers, int maxLeafs) {
@ -44,7 +50,19 @@ class UltrapeerConnectionManager extends ConnectionManager {
} }
@Override @Override
public boolean isConnected(Destination d) { public boolean isConnected(Destination d) {
// TODO Auto-generated method stub peerConnections.containsKey(d) || leafConnections.containsKey(d)
return false; }
@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)
} }
} }