WIP - disabled ping

This commit is contained in:
nathan 2020-08-13 17:02:41 +02:00
parent ac0aba1113
commit bb6cacbaee
3 changed files with 48 additions and 50 deletions

View File

@ -25,13 +25,13 @@ interface Ping {
* Adds a ping listener to this future. The listener is notified when this future is done. If this future is already completed, * Adds a ping listener to this future. The listener is notified when this future is done. If this future is already completed,
* then the listener is notified immediately. * then the listener is notified immediately.
*/ */
fun <C : Connection?> add(listener: PingListener<C>?) fun <C : Connection> add(listener: PingListener<C>?)
/** /**
* Removes a ping listener from this future. The listener is no longer notified when this future is done. If the listener * Removes a ping listener from this future. The listener is no longer notified when this future is done. If the listener
* was not previously associated with this future, this method does nothing and returns silently. * was not previously associated with this future, this method does nothing and returns silently.
*/ */
fun <C : Connection?> remove(listener: PingListener<C>?) fun <C : Connection> remove(listener: PingListener<C>?)
/** /**
* Cancel this Ping. * Cancel this Ping.

View File

@ -15,25 +15,23 @@
*/ */
package dorkbox.network.connection; package dorkbox.network.connection;
import dorkbox.network.connection.ping.PingTuple;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
// note that we specifically DO NOT implement equals/hashCode, because we cannot create two separate // note that we specifically DO NOT implement equals/hashCode, because we cannot create two separate
// listeners that are somehow equal to each other. // listeners that are somehow equal to each other.
public abstract public abstract
class PingListener<C extends Connection> implements GenericFutureListener<Future<PingTuple<C>>> { class PingListener<C extends Connection>
// implements GenericFutureListener<Future<PingTuple<C>>>
{
public public
PingListener() { PingListener() {
} }
@Override // @Override
public // public
void operationComplete(Future<PingTuple<C>> future) throws Exception { // void operationComplete(Future<PingTuple<C>> future) throws Exception {
PingTuple<C> pingTuple = future.get(); // PingTuple<C> pingTuple = future.get();
response(pingTuple.connection, pingTuple.responseTime); // response(pingTuple.connection, pingTuple.responseTime);
} // }
/** /**
* Called when the ping response has been received. * Called when the ping response has been received.

View File

@ -15,21 +15,18 @@
*/ */
package dorkbox.network.connection.ping; package dorkbox.network.connection.ping;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Ping; import dorkbox.network.connection.Ping;
import dorkbox.network.connection.PingListener; import dorkbox.network.connection.PingListener;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
public public
class PingFuture implements Ping { class PingFuture implements Ping {
private static final AtomicInteger pingCounter = new AtomicInteger(0); private static final AtomicInteger pingCounter = new AtomicInteger(0);
private final Promise<PingTuple<? extends Connection>> promise; // private final Promise<PingTuple<? extends Connection>> promise;
private final int id; private final int id;
private final long sentTime; private final long sentTime;
@ -39,19 +36,21 @@ class PingFuture implements Ping {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
PingFuture() { PingFuture() {
this(null); // this(null);
id = -1;
sentTime = -2;
} }
public // public
PingFuture(Promise<PingTuple<? extends Connection>> promise) { // PingFuture(Promise<PingTuple<? extends Connection>> promise) {
this.promise = promise; // this.promise = promise;
this.id = pingCounter.getAndIncrement(); // this.id = pingCounter.getAndIncrement();
this.sentTime = System.currentTimeMillis(); // this.sentTime = System.currentTimeMillis();
//
if (this.id == Integer.MAX_VALUE) { // if (this.id == Integer.MAX_VALUE) {
pingCounter.set(0); // pingCounter.set(0);
} // }
} // }
/** /**
* Wait for the ping to return, and returns the ping response time in MS or -1 if it failed. * Wait for the ping to return, and returns the ping response time in MS or -1 if it failed.
@ -59,15 +58,15 @@ class PingFuture implements Ping {
@Override @Override
public public
int getResponse() { int getResponse() {
try { // try {
PingTuple<? extends Connection> entry = this.promise.syncUninterruptibly() // PingTuple<? extends Connection> entry = this.promise.syncUninterruptibly()
.get(); // .get();
if (entry != null) { // if (entry != null) {
return entry.responseTime; // return entry.responseTime;
} // }
} catch (InterruptedException e) { // } catch (InterruptedException e) {
} catch (ExecutionException e) { // } catch (ExecutionException e) {
} // }
return -1; return -1;
} }
@ -79,7 +78,7 @@ class PingFuture implements Ping {
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public public
<C extends Connection> void add(PingListener<C> listener) { <C extends Connection> void add(PingListener<C> listener) {
this.promise.addListener((GenericFutureListener) listener); // this.promise.addListener((GenericFutureListener) listener);
} }
/** /**
@ -90,7 +89,7 @@ class PingFuture implements Ping {
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public public
<C extends Connection> void remove(PingListener<C> listener) { <C extends Connection> void remove(PingListener<C> listener) {
this.promise.removeListener((GenericFutureListener) listener); // this.promise.removeListener((GenericFutureListener) listener);
} }
/** /**
@ -99,7 +98,7 @@ class PingFuture implements Ping {
@Override @Override
public public
void cancel() { void cancel() {
this.promise.tryFailure(new PingCanceledException()); // this.promise.tryFailure(new PingCanceledException());
} }
/** /**
@ -107,20 +106,21 @@ class PingFuture implements Ping {
*/ */
public public
<C extends Connection> void setSuccess(C connection, PingMessage ping) { <C extends Connection> void setSuccess(C connection, PingMessage ping) {
if (ping.id == this.id) { // if (ping.id == this.id) {
long longTime = System.currentTimeMillis() - this.sentTime; // long longTime = System.currentTimeMillis() - this.sentTime;
if (longTime < Integer.MAX_VALUE) { // if (longTime < Integer.MAX_VALUE) {
this.promise.setSuccess(new PingTuple<C>(connection, (int) longTime)); // this.promise.setSuccess(new PingTuple<C>(connection, (int) longTime));
} // }
else { // else {
this.promise.setSuccess(new PingTuple<C>(connection, Integer.MAX_VALUE)); // this.promise.setSuccess(new PingTuple<C>(connection, Integer.MAX_VALUE));
} // }
} // }
} }
public public
boolean isSuccess() { boolean isSuccess() {
return this.promise.isSuccess(); // return this.promise.isSuccess();
return false;
} }
/** /**