tie in sending of pings on a timer

This commit is contained in:
Zlatin Balevsky
2018-07-27 11:54:10 +01:00
parent cfb04a9811
commit 8edd495430
4 changed files with 41 additions and 11 deletions

View File

@@ -26,7 +26,7 @@ abstract class Connection implements Closeable {
protected final String name protected final String name
long lastPingSentTime, lastPingReceivedTime long lastPingSentTime, lastPongReceivedTime
Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache) { Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache) {
this.eventBus = eventBus this.eventBus = eventBus
@@ -105,6 +105,7 @@ abstract class Connection implements Closeable {
ping.type = "Ping" ping.type = "Ping"
ping.version = 1 ping.version = 1
messages.put(ping) messages.put(ping)
lastPingSentTime = System.currentTimeMillis()
} }
protected void handlePing() { protected void handlePing() {
@@ -118,6 +119,7 @@ abstract class Connection implements Closeable {
protected void handlePong(def pong) { protected void handlePong(def pong) {
log.fine("$name received pong") log.fine("$name received pong")
lastPongReceivedTime = System.currentTimeMillis()
if (pong.pongs == null) if (pong.pongs == null)
throw new Exception("Pong doesn't have pongs") throw new Exception("Pong doesn't have pongs")
pong.pongs.each { pong.pongs.each {

View File

@@ -8,12 +8,26 @@ import net.i2p.data.Destination
abstract class ConnectionManager { abstract class ConnectionManager {
private static final int PING_TIME = 20000
final EventBus eventBus final EventBus eventBus
private final Timer timer
ConnectionManager() {} ConnectionManager() {}
ConnectionManager(EventBus eventBus) { ConnectionManager(EventBus eventBus) {
this.eventBus = eventBus this.eventBus = eventBus
this.timer = new Timer("connections-pinger",true)
}
void start() {
timer.schedule({sendPings()} as TimerTask, 1000,1000)
}
void stop() {
timer.cancel()
getConnections().each { it.close() }
} }
void onTrustEvent(TrustEvent e) { void onTrustEvent(TrustEvent e) {
@@ -34,4 +48,12 @@ abstract class ConnectionManager {
abstract boolean isConnected(Destination d) abstract boolean isConnected(Destination d)
abstract void onConnectionEvent(ConnectionEvent e) abstract void onConnectionEvent(ConnectionEvent e)
private void sendPings() {
final long now = System.currentTimeMillis()
getConnections().each {
if (now - it.lastPingSentTime > PING_TIME)
it.sendPing()
}
}
} }

View File

@@ -27,8 +27,7 @@ class LeafConnectionManager extends ConnectionManager {
@Override @Override
public Collection<Connection> getConnections() { public Collection<Connection> getConnections() {
// TODO implement connections.values()
[]
} }
@Override @Override
@@ -47,9 +46,12 @@ class LeafConnectionManager extends ConnectionManager {
log.severe("Got inconsistent event as a leaf! $e") log.severe("Got inconsistent event as a leaf! $e")
return return
} }
if (e.status != ConnectionAttemptStatus.SUCCESSFUL)
return
Connection c = new UltrapeerConnection(eventBus, e.endpoint) Connection c = new UltrapeerConnection(eventBus, e.endpoint)
// TODO: start and stuff
connections.put(e.endpoint.destination, c) connections.put(e.endpoint.destination, c)
c.start()
} }
} }

View File

@@ -31,19 +31,20 @@ class UltrapeerConnectionManager extends ConnectionManager {
@Override @Override
public Collection<Connection> getConnections() { public Collection<Connection> getConnections() {
// TODO implement def rv = new ArrayList(peerConnections.size() + leafConnections.size())
[] rv.addAll(peerConnections.values())
rv.addAll(leafConnections.values())
rv
} }
boolean hasLeafSlots() { boolean hasLeafSlots() {
// TODO implement leafConnections.size() < maxLeafs
true
} }
boolean hasPeerSlots() { boolean hasPeerSlots() {
// TODO implement peerConnections.size() < maxPeers
true
} }
@Override @Override
protected int getDesiredConnections() { protected int getDesiredConnections() {
return maxPeers / 2; return maxPeers / 2;
@@ -60,9 +61,12 @@ class UltrapeerConnectionManager extends ConnectionManager {
return return
} }
if (e.status != ConnectionAttemptStatus.SUCCESSFUL)
return
Connection c = e.leaf ? new LeafConnection(eventBus, e.endpoint) : new PeerConnection(eventBus, e.endpoint, e.incoming) 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 def map = e.leaf ? leafConnections : peerConnections
map.put(e.endpoint.destination, c) map.put(e.endpoint.destination, c)
c.start()
} }
} }