add disconnection event, publish it on closing connections, handle it in connection manager

This commit is contained in:
Zlatin Balevsky
2018-07-28 20:19:02 +01:00
parent 9aeb9de070
commit dd287bae18
6 changed files with 36 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import com.muwire.core.connection.ConnectionAcceptor
import com.muwire.core.connection.ConnectionEstablisher import com.muwire.core.connection.ConnectionEstablisher
import com.muwire.core.connection.ConnectionEvent import com.muwire.core.connection.ConnectionEvent
import com.muwire.core.connection.ConnectionManager import com.muwire.core.connection.ConnectionManager
import com.muwire.core.connection.DisconnectionEvent
import com.muwire.core.connection.I2PAcceptor import com.muwire.core.connection.I2PAcceptor
import com.muwire.core.connection.I2PConnector import com.muwire.core.connection.I2PConnector
import com.muwire.core.connection.LeafConnectionManager import com.muwire.core.connection.LeafConnectionManager
@@ -88,6 +89,7 @@ class Core {
new LeafConnectionManager(eventBus,3, hostCache) : new UltrapeerConnectionManager(eventBus, 512, 512, hostCache) new LeafConnectionManager(eventBus,3, hostCache) : new UltrapeerConnectionManager(eventBus, 512, 512, hostCache)
eventBus.register(TrustEvent.class, connectionManager) eventBus.register(TrustEvent.class, connectionManager)
eventBus.register(ConnectionEvent.class, connectionManager) eventBus.register(ConnectionEvent.class, connectionManager)
eventBus.register(DisconnectionEvent.class, connectionManager)
connectionManager.start() connectionManager.start()
log.info("initializing cache client") log.info("initializing cache client")

View File

@@ -65,8 +65,7 @@ abstract class Connection implements Closeable {
} }
reader.interrupt() reader.interrupt()
writer.interrupt() writer.interrupt()
reader.join() eventBus.publish(new DisconnectionEvent(destination: endpoint.destination))
writer.join()
} }
protected void readLoop() { protected void readLoop() {

View File

@@ -53,6 +53,8 @@ abstract class ConnectionManager {
abstract void onConnectionEvent(ConnectionEvent e) abstract void onConnectionEvent(ConnectionEvent e)
abstract void onDisconnectionEvent(DisconnectionEvent e)
protected void sendPings() { protected void sendPings() {
final long now = System.currentTimeMillis() final long now = System.currentTimeMillis()
getConnections().each { getConnections().each {

View File

@@ -0,0 +1,15 @@
package com.muwire.core.connection
import com.muwire.core.Event
import net.i2p.data.Destination
class DisconnectionEvent extends Event {
Destination destination
@Override
public String toString() {
"DisconnectionEvent ${super.toString()} destination:${destianation.toBase32()}"
}
}

View File

@@ -55,4 +55,11 @@ class LeafConnectionManager extends ConnectionManager {
c.start() c.start()
} }
@Override
public void onDisconnectionEvent(DisconnectionEvent e) {
def removed = connections.remove(e.destination)
if (removed == null)
log.severe("removed destination not present in connection manager ${e.destination.toBase32()}")
}
} }

View File

@@ -72,4 +72,13 @@ class UltrapeerConnectionManager extends ConnectionManager {
map.put(e.endpoint.destination, c) map.put(e.endpoint.destination, c)
c.start() c.start()
} }
@Override
public void onDisconnectionEvent(DisconnectionEvent e) {
def removed = peerConnections.remove(e.destination)
if (removed == null)
removed = leafConnections.remove(e.destination)
if (removed == null)
log.severe("Removed connection not present in either leaf or peer map ${e.destination.toBase32()}")
}
} }