Cleaned up PING. Removed client.ping (it's in client.send().ping(), to be consistent)

This commit is contained in:
nathan 2014-09-17 17:39:16 +02:00
parent 1c26fa5023
commit f03c98b2a8
20 changed files with 282 additions and 298 deletions

View File

@ -27,7 +27,6 @@ import dorkbox.network.connection.ConnectionBridgeFlushAlways;
import dorkbox.network.connection.EndPointClient;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleSender;
import dorkbox.network.connection.ping.Ping;
import dorkbox.network.connection.registration.local.RegistrationLocalHandlerClient;
import dorkbox.network.connection.registration.remote.RegistrationRemoteHandlerClientTCP;
import dorkbox.network.connection.registration.remote.RegistrationRemoteHandlerClientUDP;
@ -49,6 +48,8 @@ public class Client extends EndPointClient {
private volatile int connectionTimeout = 5000; // default
private volatile ConnectionBridgeFlushAlways connectionBridgeFlushAlways;
/**
* Starts a LOCAL <b>only</b> client, with the default local channel name and serialization scheme
*/
@ -318,7 +319,12 @@ public class Client extends EndPointClient {
* of the normal eventloop patterns, and it is confusing to the user to have to manually flush the channel each time.
*/
public ConnectionBridge send() {
return new ConnectionBridgeFlushAlways(this.connectionManager.getConnection0().send());
ConnectionBridgeFlushAlways connectionBridgeFlushAlways2 = this.connectionBridgeFlushAlways;
if (connectionBridgeFlushAlways2 == null) {
this.connectionBridgeFlushAlways = new ConnectionBridgeFlushAlways(this.connectionManager.getConnection0().send());
}
return this.connectionBridgeFlushAlways;
}
/**
@ -335,13 +341,6 @@ public class Client extends EndPointClient {
return this.connectionManager.getConnection0().sendOnIdle(message);
}
/**
* Returns a future that will have the last calculated return trip time.
*/
public Ping ping() {
return this.connectionManager.getConnection0().send().ping();
}
/**
* Fetches the connection used by the client.
* <p>

View File

@ -1,6 +1,5 @@
package dorkbox.network.connection;
import dorkbox.network.connection.ping.Ping;
public interface ConnectionBridge {

View File

@ -1,6 +1,5 @@
package dorkbox.network.connection;
import dorkbox.network.connection.ping.Ping;
public class ConnectionBridgeFlushAlways implements ConnectionBridge {

View File

@ -11,6 +11,7 @@ import io.netty.channel.udt.nio.NioUdtByteConnectorChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Promise;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
@ -21,9 +22,6 @@ import org.slf4j.Logger;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleObjectSender;
import dorkbox.network.connection.idle.IdleSender;
import dorkbox.network.connection.ping.Ping;
import dorkbox.network.connection.ping.PingFuture;
import dorkbox.network.connection.ping.PingMessage;
import dorkbox.network.connection.wrapper.ChannelNetworkWrapper;
import dorkbox.network.connection.wrapper.ChannelNull;
import dorkbox.network.connection.wrapper.ChannelWrapper;
@ -149,7 +147,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
*/
public final void updatePingResponse(PingMessage ping) {
if (this.pingFuture != null) {
this.pingFuture.setSuccess(ping);
this.pingFuture.setSuccess(this, ping);
}
}
@ -161,11 +159,12 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
@Override
public final Ping ping() {
PingFuture pingFuture2 = this.pingFuture;
if (pingFuture2 != null) {
if (pingFuture2 != null && !pingFuture2.isSuccess()) {
pingFuture2.cancel();
}
this.pingFuture = this.channelWrapper.pingFuture();
Promise<PingTuple<? extends Connection>> newPromise = this.channelWrapper.getEventLoop().newPromise();
this.pingFuture = new PingFuture(newPromise);
PingMessage ping = new PingMessage();
ping.id = this.pingFuture.getId();
@ -178,7 +177,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
* INTERNAL USE ONLY. Used to initiate a ping, and to return a ping.
* Sends a ping message attempted in the following order: UDP, UDT, TCP
*/
public final void ping0(PingMessage ping) {
final void ping0(PingMessage ping) {
if (this.channelWrapper.udp() != null) {
UDP(ping).flush();
} else if (this.channelWrapper.udt() != null) {

View File

@ -14,8 +14,6 @@ import org.bouncycastle.crypto.params.IESWithCipherParameters;
import com.esotericsoftware.kryo.factories.SerializerFactory;
import dorkbox.network.ConnectionOptions;
import dorkbox.network.connection.ping.PingListener;
import dorkbox.network.connection.ping.PingMessage;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration;
import dorkbox.network.connection.wrapper.ChannelLocalWrapper;
@ -79,7 +77,7 @@ public class EndPointWithSerialization extends EndPoint {
// add the ping listener (internal use only!)
this.connectionManager.add(new PingListener(name));
this.connectionManager.add(new PingSystemListener(name));
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
this.shutdownHook = new Thread() {

View File

@ -2,8 +2,6 @@ package dorkbox.network.connection;
import dorkbox.util.ClassHelper;
// note that we specifically DO NOT implement equals/hashCode, because we cannot create two separate
// listeners that are somehow equal to each other.
public abstract class Listener<C extends Connection, M extends Object> {
private final Class<?> objectType;
@ -75,6 +73,24 @@ public abstract class Listener<C extends Connection, M extends Object> {
throwable.printStackTrace();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.objectType == null ? 0 : this.objectType.hashCode());
return result;
}
// only possible way for it to be equal, is if it is the same object
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return false;
}
@Override
public String toString() {
return "Listener [type=" + getObjectType() + "]";

View File

@ -1,7 +1,5 @@
package dorkbox.network.connection.ping;
package dorkbox.network.connection;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
public interface Ping {
/**
@ -14,7 +12,7 @@ public interface Ping {
* notified when this future is done. If this future is already completed,
* the specified listener is notified immediately.
*/
public void addListener(GenericFutureListener<? extends Future<? super Object>> listener);
public <C extends Connection> void addListener(PingListener<C> listener);
/**
* Removes the specified listener from this future. The specified listener
@ -22,7 +20,7 @@ public interface Ping {
* is not associated with this future, this method does nothing and returns
* silently.
*/
public void removeListener(GenericFutureListener<? extends Future<? super Object>> listener);
public<C extends Connection> void removeListener(PingListener<C> listener);
/**
* Cancel this Ping.

View File

@ -1,4 +1,4 @@
package dorkbox.network.connection.ping;
package dorkbox.network.connection;
public class PingCanceledException extends RuntimeException {

View File

@ -1,4 +1,4 @@
package dorkbox.network.connection.ping;
package dorkbox.network.connection;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
@ -7,11 +7,11 @@ import io.netty.util.concurrent.Promise;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
public class PingFuture implements Ping {
class PingFuture implements Ping {
private static AtomicInteger pingCounter = new AtomicInteger(0);
private final Promise<Integer> promise;
private final Promise<PingTuple<? extends Connection>> promise;
private final int id;
private final long sentTime;
@ -19,14 +19,18 @@ public class PingFuture implements Ping {
/**
* Protected constructor for when we are completely overriding this class. (Used by the "local" connection for instant pings)
*/
protected PingFuture() {
PingFuture() {
this(null);
}
public PingFuture(Promise<Integer> promise) {
PingFuture(Promise<PingTuple<? extends Connection>> promise) {
this.promise = promise;
this.id = pingCounter.getAndIncrement();
this.sentTime = System.currentTimeMillis();
if (this.id == Integer.MAX_VALUE) {
pingCounter.set(0);
}
}
/**
@ -35,7 +39,10 @@ public class PingFuture implements Ping {
@Override
public int getResponse() {
try {
return this.promise.syncUninterruptibly().get();
PingTuple<? extends Connection> entry = this.promise.syncUninterruptibly().get();
if (entry != null) {
return entry.responseTime;
}
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
@ -45,26 +52,33 @@ public class PingFuture implements Ping {
/**
* This is when the endpoint that ORIGINALLY sent the ping, finally receives a response.
* @param <C>
* @param connectionImpl
*/
public void setSuccess(PingMessage ping) {
public <C extends Connection> void setSuccess(C connection, PingMessage ping) {
if (ping.id == this.id) {
long longTime = System.currentTimeMillis() - this.sentTime;
if (longTime < Integer.MAX_VALUE) {
this.promise.setSuccess((int)longTime);
this.promise.setSuccess(new PingTuple<C>(connection, (int) longTime));
} else {
this.promise.setSuccess(Integer.MAX_VALUE);
this.promise.setSuccess(new PingTuple<C>(connection, Integer.MAX_VALUE));
}
}
}
public boolean isSuccess() {
return this.promise.isSuccess();
}
/**
* Adds the specified listener to this future. The specified listener is
* notified when this future is done. If this future is already completed,
* the specified listener is notified immediately.
*/
@Override
public void addListener(GenericFutureListener<? extends Future<? super Object>> listener) {
this.promise.addListener(listener);
@SuppressWarnings("unchecked")
public <C extends Connection> void addListener(PingListener<C> listener) {
this.promise.addListener((GenericFutureListener<? extends Future<? super PingTuple<? extends Connection>>>) listener);
}
/**
@ -74,8 +88,9 @@ public class PingFuture implements Ping {
* silently.
*/
@Override
public void removeListener(GenericFutureListener<? extends Future<? super Object>> listener) {
this.promise.removeListener(listener);
@SuppressWarnings("unchecked")
public <C extends Connection> void removeListener(PingListener<C> listener) {
this.promise.removeListener((GenericFutureListener<? extends Future<? super PingTuple<? extends Connection>>>) listener);
}
/**

View File

@ -0,0 +1,28 @@
package dorkbox.network.connection;
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
// listeners that are somehow equal to each other.
public abstract class PingListener<C extends Connection> implements GenericFutureListener<Future<PingTuple<C>>> {
public PingListener() {
}
@Override
public void operationComplete(Future<PingTuple<C>> future) throws Exception {
PingTuple<C> pingTuple = future.get();
response(pingTuple.connection, pingTuple.responseTime);
}
/**
* Called when the ping response has been received.
*/
public abstract void response(C connection, int pingResponseTime);
@Override
public String toString() {
return "PingListener";
}
}

View File

@ -0,0 +1,9 @@
package dorkbox.network.connection;
/**
* Internal message to determine round trip time.
*/
class PingMessage {
public int id;
public boolean isReply;
}

View File

@ -0,0 +1,20 @@
package dorkbox.network.connection;
class PingSystemListener extends Listener<ConnectionImpl, PingMessage> {
PingSystemListener(String name) {
}
@Override
public void received(ConnectionImpl connection, PingMessage ping) {
if (ping.isReply) {
connection.updatePingResponse(ping);
} else {
// return the ping from whence it came
ping.isReply = true;
connection.ping0(ping);
}
}
}

View File

@ -0,0 +1,12 @@
package dorkbox.network.connection;
public class PingTuple<C extends Connection> {
public C connection;
public int responseTime;
public PingTuple(C connection, int responseTime) {
this.connection = connection;
this.responseTime = responseTime;
}
}

View File

@ -1,57 +0,0 @@
package dorkbox.network.connection.ping;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
public class PingFutureLocal extends PingFuture {
public PingFutureLocal() {
super();
}
/**
* Wait for the ping to return, and returns the ping response time or -1 if it failed failed.
*/
@Override
public int getResponse() {
return 0;
}
/**
* Tells this ping future, that it was successful
*/
@Override
public void setSuccess(PingMessage ping) {
}
/**
* Adds the specified listener to this future. The specified listener is
* notified when this future is done. If this future is already completed,
* the specified listener is notified immediately.
*/
@Override
public void addListener(GenericFutureListener<? extends Future<? super Object>> listener) {
try {
listener.operationComplete(null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Removes the specified listener from this future. The specified listener
* is no longer notified when this future is done. If the specified listener
* is not associated with this future, this method does nothing and returns
* silently.
*/
@Override
public void removeListener(GenericFutureListener<? extends Future<? super Object>> listener) {
}
/**
* Cancel this Ping.
*/
@Override
public void cancel() {
}
}

View File

@ -1,33 +0,0 @@
package dorkbox.network.connection.ping;
import org.slf4j.Logger;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.Listener;
public class PingListener extends Listener<ConnectionImpl, PingMessage> {
private final org.slf4j.Logger logger;
public PingListener(String name) {
this.logger = org.slf4j.LoggerFactory.getLogger(name);
}
@Override
public void received(ConnectionImpl connection, PingMessage ping) {
Logger logger2 = this.logger;
if (ping.isReply) {
if (logger2.isTraceEnabled()) {
logger2.trace("Received a reply to my issued ping request.");
}
connection.updatePingResponse(ping);
} else {
// return the ping from whence it came
if (logger2.isTraceEnabled()) {
logger2.trace( "Received a ping request from {}. Sending a reply.", connection);
}
ping.isReply = true;
connection.ping0(ping);
}
}
}

View File

@ -1,9 +0,0 @@
package dorkbox.network.connection.ping;
/**
* Internal message to determine round trip time.
*/
public class PingMessage {
public int id;
public boolean isReply;
}

View File

@ -1,6 +1,7 @@
package dorkbox.network.connection.wrapper;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.local.LocalAddress;
import java.util.concurrent.atomic.AtomicBoolean;
@ -11,8 +12,6 @@ import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.ISessionManager;
import dorkbox.network.connection.ping.PingFuture;
import dorkbox.network.connection.ping.PingFutureLocal;
import dorkbox.network.connection.registration.MetaChannel;
public class ChannelLocalWrapper implements ChannelWrapper, ConnectionPoint {
@ -84,8 +83,8 @@ public class ChannelLocalWrapper implements ChannelWrapper, ConnectionPoint {
}
@Override
public PingFuture pingFuture() {
return new PingFutureLocal();
public EventLoop getEventLoop() {
return this.channel.eventLoop();
}
@Override

View File

@ -2,7 +2,6 @@ package dorkbox.network.connection.wrapper;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Promise;
import java.net.InetSocketAddress;
@ -14,7 +13,6 @@ import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.ISessionManager;
import dorkbox.network.connection.UdpServer;
import dorkbox.network.connection.ping.PingFuture;
import dorkbox.network.connection.registration.MetaChannel;
public class ChannelNetworkWrapper implements ChannelWrapper {
@ -38,32 +36,32 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
public ChannelNetworkWrapper(MetaChannel metaChannel, UdpServer udpServer) {
Channel tcpChannel = metaChannel.tcpChannel;
eventLoop = tcpChannel.eventLoop();
this.eventLoop = tcpChannel.eventLoop();
tcp = new ChannelNetwork(tcpChannel);
this.tcp = new ChannelNetwork(tcpChannel);
if (metaChannel.udpChannel != null) {
if (metaChannel.udpRemoteAddress != null) {
udp = new ChannelNetworkUdp(metaChannel.udpChannel, metaChannel.udpRemoteAddress, udpServer);
this.udp = new ChannelNetworkUdp(metaChannel.udpChannel, metaChannel.udpRemoteAddress, udpServer);
} else {
udp = new ChannelNetwork(metaChannel.udpChannel);
this.udp = new ChannelNetwork(metaChannel.udpChannel);
}
} else {
udp = null;
this.udp = null;
}
if (metaChannel.udtChannel != null) {
udt = new ChannelNetwork(metaChannel.udtChannel);
this.udt = new ChannelNetwork(metaChannel.udtChannel);
} else {
udt = null;
this.udt = null;
}
remoteAddress = ((InetSocketAddress)tcpChannel.remoteAddress()).getAddress().getHostAddress();
remotePublicKeyChanged = metaChannel.changedRemoteKey;
this.remoteAddress = ((InetSocketAddress)tcpChannel.remoteAddress()).getAddress().getHostAddress();
this.remotePublicKeyChanged = metaChannel.changedRemoteKey;
// AES key & IV (only for networked connections)
cryptoAesKeyAndIV = new ParametersWithIV(new KeyParameter(metaChannel.aesKey), metaChannel.aesIV);
this.cryptoAesKeyAndIV = new ParametersWithIV(new KeyParameter(metaChannel.aesKey), metaChannel.aesIV);
// TODO: have to be able to get a NEW IV, so we can rotate keys!
}
@ -76,7 +74,7 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
}
public final boolean remoteKeyChanged() {
return remotePublicKeyChanged;
return this.remotePublicKeyChanged;
}
/**
@ -84,46 +82,45 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
*/
@Override
public void flush() {
tcp.flush();
this.tcp.flush();
if (udp != null) {
udp.flush();
if (this.udp != null) {
this.udp.flush();
}
if (udt != null) {
udt.flush();
if (this.udt != null) {
this.udt.flush();
}
}
@Override
public EventLoop getEventLoop() {
return this.eventLoop;
}
@Override
public ParametersWithIV cryptoParameters() {
return cryptoAesKeyAndIV;
return this.cryptoAesKeyAndIV;
}
@Override
public ConnectionPoint tcp() {
return tcp;
return this.tcp;
}
@Override
public ConnectionPoint udp() {
return udp;
return this.udp;
}
@Override
public ConnectionPoint udt() {
return udt;
}
@Override
public PingFuture pingFuture() {
Promise<Integer> newPromise = eventLoop.newPromise();
return new PingFuture(newPromise);
return this.udt;
}
@Override
public String getRemoteHost() {
return remoteAddress;
return this.remoteAddress;
}
@ -131,14 +128,14 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
public void close(final Connection connection, final ISessionManager sessionManager) {
long maxShutdownWaitTimeInMilliSeconds = EndPoint.maxShutdownWaitTimeInMilliSeconds;
tcp.close(maxShutdownWaitTimeInMilliSeconds);
this.tcp.close(maxShutdownWaitTimeInMilliSeconds);
if (udp != null) {
udp.close(maxShutdownWaitTimeInMilliSeconds);
if (this.udp != null) {
this.udp.close(maxShutdownWaitTimeInMilliSeconds);
}
if (udt != null) {
udt.close(maxShutdownWaitTimeInMilliSeconds);
if (this.udt != null) {
this.udt.close(maxShutdownWaitTimeInMilliSeconds);
// we need to yield the thread here, so that the socket has a chance to close
Thread.yield();
@ -152,7 +149,7 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
@Override
public int id() {
return tcp.id();
return this.tcp.id();
}
@Override
@ -168,11 +165,11 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
}
ChannelNetworkWrapper other = (ChannelNetworkWrapper) obj;
if (remoteAddress == null) {
if (this.remoteAddress == null) {
if (other.remoteAddress != null) {
return false;
}
} else if (!remoteAddress.equals(other.remoteAddress)) {
} else if (!this.remoteAddress.equals(other.remoteAddress)) {
return false;
}
return true;
@ -180,6 +177,6 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
@Override
public int hashCode() {
return remoteAddress.hashCode();
return this.remoteAddress.hashCode();
}
}

View File

@ -1,12 +1,13 @@
package dorkbox.network.connection.wrapper;
import io.netty.channel.EventLoop;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.ISessionManager;
import dorkbox.network.connection.ping.PingFuture;
public interface ChannelWrapper {
@ -24,7 +25,7 @@ public interface ChannelWrapper {
*/
public void flush();
public PingFuture pingFuture();
public EventLoop getEventLoop();
public ParametersWithIV cryptoParameters();

View File

@ -9,8 +9,8 @@ import java.io.IOException;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ping.PingMessage;
import dorkbox.network.connection.Ping;
import dorkbox.network.connection.PingListener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -37,56 +37,104 @@ public class PingTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, PingMessage>() {
int count = 0;
@Override
public void connected(Connection connection) {
System.err.println("Testing TCP ping");
for (int i=0;i<10;i++) {
int response2 = connection.send().ping().getResponse();
System.err.println("Ping B roundtime: " + response2);
}
}
// @Override
// public void received(Connection connection, PingMessage ping) {
// response = ping.time;
// System.err.println("Ping return time: " + response);
//
// if (count++ < 10) {
// connection.send().ping();
// } else {
// stopEndPoints();
// }
// }
});
client.connect(5000);
System.err.println("Testing TCP ping");
for (int i=0;i<10;i++) {
int response2 = client.ping().getResponse();
System.err.println("Ping A roundtime: " + response2);
this.response = client.send().ping().getResponse();
System.err.println("Ping: " + this.response);
}
// alternate way to register for the receipt of a one-off ping response
// PingFuture ping = connection.ping();
// ping.addListener(new ChannelFutureListener() {
// int count = 0;
// @Override
// public void operationComplete(ChannelFuture future) throws Exception {
// response = ((PingFuture)future).getResponseUninterruptibly();
// System.err.println("Ping return time: " + response);
//
// if (count++ < 10) {
// connection.ping();
// } else {
// stopEndPoints();
// }
// }
// });
stopEndPoints();
if (this.response == -1) {
fail();
}
}
@Test
public void pingTCP_testListeners1() throws IOException, InitializationException, SecurityException {
this.response = -1;
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.tcpPort = tcpPort;
connectionOptions.host = host;
Server server = new Server(connectionOptions);
addEndPoint(server);
server.bind(false);
// ----
Client client = new Client(connectionOptions);
addEndPoint(client);
client.connect(5000);
System.err.println("Testing TCP ping with multi callback");
final PingListener<Connection> pingListener = new PingListener<Connection>() {
volatile int count = 0;
@Override
public void response(Connection connection, int pingResponseTime) {
System.err.println("Ping: " + pingResponseTime);
if (this.count++ < 10) {
connection.send().ping().addListener(this);
} else {
PingTest.this.response = pingResponseTime;
stopEndPoints();
}
}
};
// alternate way to register for the receipt of a one-off ping response
// doesn't matter how many times this is called. If there is a PING waiting, then it's overwritten
Ping ping = client.send().ping();
ping.addListener(pingListener);
waitForThreads();
if (this.response == -1) {
fail();
}
}
@Test
public void pingTCP_testListeners2() throws IOException, InitializationException, SecurityException {
this.response = -1;
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.tcpPort = tcpPort;
connectionOptions.host = host;
Server server = new Server(connectionOptions);
addEndPoint(server);
server.bind(false);
// ----
Client client = new Client(connectionOptions);
addEndPoint(client);
client.connect(5000);
System.err.println("Testing TCP ping with single callback");
final PingListener<Connection> pingListener = new PingListener<Connection>() {
@Override
public void response(Connection connection, int pingResponseTime) {
System.err.println("Ping: " + pingResponseTime);
PingTest.this.response = pingResponseTime;
stopEndPoints();
}
};
// alternate way to register for the receipt of a one-off ping response
// doesn't matter how many times this is called. If there is a PING waiting, then it's overwritten
Ping ping = client.send().ping();
ping.addListener(pingListener);
waitForThreads();
@ -115,36 +163,15 @@ public class PingTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, PingMessage>() {
int count = 0;
@Override
public void connected(Connection connection) {
System.err.println("Testing UDP ping");
}
});
client.connect(5000);
client.ping();
// alternate way to register for the receipt of a one-off ping response
// PingFuture ping = connection.ping();
// ping.addListener(new ChannelFutureListener() {
// int count = 0;
// @Override
// public void operationComplete(ChannelFuture future) throws Exception {
// response = ((PingFuture)future).getResponseUninterruptibly();
// System.err.println("Ping return time: " + response);
//
// if (count++ < 10) {
// connection.ping();
// } else {
// stopEndPoints();
// }
// }
// });
System.err.println("Testing UDP ping");
for (int i=0;i<10;i++) {
this.response = client.send().ping().getResponse();
System.err.println("Ping: " + this.response);
}
waitForThreads();
stopEndPoints();
if (this.response == -1) {
fail();
@ -171,48 +198,15 @@ public class PingTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, PingMessage>() {
int count = 0;
@Override
public void connected(Connection connection) {
System.err.println("Testing UDT ping");
}
// @Override
// public void received(Connection connection, PingMessage ping) {
// PingTest.this.response = ping.time;
// System.err.println("Ping return time: " + PingTest.this.response);
//
// if (this.count++ < 10) {
// connection.send().ping();
// } else {
// stopEndPoints();
// }
// }
});
client.connect(5000);
client.ping();
// alternate way to register for the receipt of a one-off ping response
// PingFuture ping = connection.ping();
// ping.addListener(new ChannelFutureListener() {
// int count = 0;
// @Override
// public void operationComplete(ChannelFuture future) throws Exception {
// response = ((PingFuture)future).getResponseUninterruptibly();
// System.err.println("Ping return time: " + response);
//
// if (count++ < 10) {
// connection.ping();
// } else {
// stopEndPoints();
// }
// }
// });
System.err.println("Testing UDT ping");
for (int i=0;i<10;i++) {
this.response = client.send().ping().getResponse();
System.err.println("Ping: " + this.response);
}
waitForThreads();
stopEndPoints();
if (this.response == -1) {
fail();