Code polish, tweaked Listeners to support a 'default' listener to cleanup the verbosity that was necessary for default connections. Tweaked RMI test

This commit is contained in:
nathan 2014-09-26 18:44:24 +02:00
parent 7603b96d5e
commit bcb49807e0
48 changed files with 657 additions and 565 deletions

View File

@ -22,8 +22,6 @@ import java.util.List;
import org.slf4j.Logger;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionBridge;
import dorkbox.network.connection.ConnectionBridgeFlushAlways;
import dorkbox.network.connection.EndPointClient;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleSender;
@ -48,8 +46,6 @@ 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
*/
@ -311,22 +307,6 @@ public class Client extends EndPointClient {
}
}
/**
* Expose methods to send objects to a destination.
* <p>
* This returns a bridge that will flush after EVERY send! This is because sending data can occur on the client, outside
* of the normal eventloop patterns, and it is confusing to the user to have to manually flush the channel each time.
*/
public ConnectionBridge send() {
ConnectionBridgeFlushAlways connectionBridgeFlushAlways2 = this.connectionBridgeFlushAlways;
if (connectionBridgeFlushAlways2 == null) {
this.connectionBridgeFlushAlways = new ConnectionBridgeFlushAlways(this.connectionManager.getConnection0().send());
}
return this.connectionBridgeFlushAlways;
}
/**
* Expose methods to send objects to a destination when the connection has become idle.
*/

View File

@ -3,6 +3,7 @@ package dorkbox.network.connection;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.bridge.ConnectionBridge;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleSender;

View File

@ -1,29 +0,0 @@
package dorkbox.network.connection;
public interface ConnectionBridgeServer {
/**
* Sends the object all server connections over the network using TCP. (or
* via LOCAL when it's a local channel).
*/
public void TCP(Object message);
/**
* Sends the object all server connections over the network using UDP (or
* via LOCAL when it's a local channel).
*/
public void UDP(Object message);
/**
* Sends the object all server connections over the network using UDT. (or
* via LOCAL when it's a local channel).
*/
public void UDT(Object message);
/**
* Exposes methods to send the object to all server connections (except the specified one)
* over the network. (or via LOCAL when it's a local channel).
*/
public ConnectionExceptSpecifiedBridgeServer except();
}

View File

@ -19,6 +19,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.slf4j.Logger;
import dorkbox.network.connection.bridge.ConnectionBridge;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleObjectSender;
import dorkbox.network.connection.idle.IdleSender;
@ -245,7 +246,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isTraceEnabled()) {
logger2.trace("Sending TCP {}", message);
}
ConnectionPoint tcp = this.channelWrapper.tcp();
ConnectionPointWriter tcp = this.channelWrapper.tcp();
tcp.write(message);
return tcp;
} else {
@ -268,7 +269,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isTraceEnabled()) {
logger2.trace("Sending UDP {}", message);
}
ConnectionPoint udp = this.channelWrapper.udp();
ConnectionPointWriter udp = this.channelWrapper.udp();
udp.write(message);
return udp;
} else {
@ -290,7 +291,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isTraceEnabled()) {
logger2.trace("Sending UDT {}", message);
}
ConnectionPoint udt = this.channelWrapper.udt();
ConnectionPointWriter udt = this.channelWrapper.udt();
udt.write(message);
return udt;
} else {
@ -513,7 +514,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
*/
@SuppressWarnings("rawtypes")
@Override
public final void add(Listener listener) {
public final void add(ListenerRaw listener) {
if (this.endPoint instanceof EndPointServer) {
// when we are a server, NORMALLY listeners are added at the GLOBAL level
// meaning --
@ -550,7 +551,7 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
*/
@SuppressWarnings("rawtypes")
@Override
public final void remove(Listener listener) {
public final void remove(ListenerRaw listener) {
if (this.endPoint instanceof EndPointServer) {
// when we are a server, NORMALLY listeners are added at the GLOBAL level
// meaning --

View File

@ -21,7 +21,7 @@ import dorkbox.util.ClassHelper;
public class ConnectionManager implements ListenerBridge, ISessionManager {
// these are final, because the REFERENCE to these will never change. They ARE NOT immutable objects (meaning their content can change)
private final ConcurrentHashMapFactory<Type, CopyOnWriteArrayList<Listener<Connection, Object>>> listeners;
private final ConcurrentHashMapFactory<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>> listeners;
private final ConcurrentHashMapFactory<Connection, ConnectionManager> localManagers;
private final CopyOnWriteArrayList<Connection> connections = new CopyOnWriteArrayList<Connection>();
@ -37,12 +37,12 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
this.baseClass = baseClass;
this.listeners = new ConcurrentHashMapFactory<Type, CopyOnWriteArrayList<Listener<Connection, Object>>>() {
this.listeners = new ConcurrentHashMapFactory<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>>() {
private static final long serialVersionUID = 1L;
@Override
public CopyOnWriteArrayList<Listener<Connection, Object>> createNewOject(Object... args) {
return new CopyOnWriteArrayList<Listener<Connection, Object>>();
public CopyOnWriteArrayList<ListenerRaw<Connection, Object>> createNewOject(Object... args) {
return new CopyOnWriteArrayList<ListenerRaw<Connection, Object>>();
}
};
@ -72,14 +72,14 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@SuppressWarnings("rawtypes")
@Override
public final void add(Listener listener) {
public final void add(ListenerRaw listener) {
if (listener == null) {
throw new IllegalArgumentException("listener cannot be null.");
}
// find the class that uses Listener.class.
Class<?> clazz = listener.getClass();
while (clazz.getSuperclass() != Listener.class) {
while (clazz.getSuperclass() != ListenerRaw.class) {
clazz = clazz.getSuperclass();
}
@ -101,17 +101,17 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
}
// didn't successfully add the listener.
throw new RuntimeException("Unable to add incompatible connection types as a listener!");
throw new IllegalArgumentException("Unable to add incompatible connection type as a listener! : " + this.baseClass);
}
/**
* INTERNAL USE ONLY
*/
@SuppressWarnings({"unchecked","rawtypes"})
private final void addListener0(Listener listener) {
private final void addListener0(ListenerRaw listener) {
Class<?> type = listener.getObjectType();
CopyOnWriteArrayList<Listener<Connection, Object>> list = this.listeners.getOrCreate(type);
CopyOnWriteArrayList<ListenerRaw<Connection, Object>> list = this.listeners.getOrCreate(type);
list.addIfAbsent(listener);
Logger logger2 = this.logger;
@ -134,14 +134,14 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@SuppressWarnings("rawtypes")
@Override
public final void remove(Listener listener) {
public final void remove(ListenerRaw listener) {
if (listener == null) {
throw new IllegalArgumentException("listener cannot be null.");
}
Class<?> type = listener.getObjectType();
CopyOnWriteArrayList<Listener<Connection, Object>> list = this.listeners.get(type);
CopyOnWriteArrayList<ListenerRaw<Connection, Object>> list = this.listeners.get(type);
if (list != null) {
list.remove(listener);
}
@ -196,18 +196,18 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@Override
public final void notifyOnMessage(Connection connection, Object message) {
notifyOnMessage(connection, message, false);
notifyOnMessage0(connection, message, false);
}
private final void notifyOnMessage(Connection connection, Object message, boolean foundListener) {
private final boolean notifyOnMessage0(Connection connection, Object message, boolean foundListener) {
Class<?> objectType = message.getClass();
// this is the GLOBAL version (unless it's the call from below, then it's the connection scoped version)
CopyOnWriteArrayList<Listener<Connection, Object>> list = this.listeners.get(objectType);
CopyOnWriteArrayList<ListenerRaw<Connection, Object>> list = this.listeners.get(objectType);
if (list != null) {
for (Listener<Connection, Object> listener : list) {
for (ListenerRaw<Connection, Object> listener : list) {
if (this.shutdown) {
return;
return true;
}
listener.received(connection, message);
@ -239,26 +239,17 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
}
if (list != null) {
for (Listener<Connection, Object> listener : list) {
for (ListenerRaw<Connection, Object> listener : list) {
if (this.shutdown) {
return;
return true;
}
listener.received(connection, message);
foundListener = true;
}
} else if (!foundListener) {
Logger logger2 = this.logger;
if (logger2.isDebugEnabled()) {
this.logger.debug("----------- LISTENER NOT REGISTERED FOR TYPE: {}", message.getClass().getSimpleName());
}
}
}
// only run a flush once
if (foundListener) {
connection.send().flush();
}
// now have to account for additional connection listener managers (non-global).
ConnectionManager localManager = this.localManagers.get(connection);
@ -266,8 +257,19 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
// if we found a listener during THIS method call, we need to let the NEXT method call know,
// so it doesn't spit out error for not handling a message (since that message MIGHT have
// been found in this method).
localManager.notifyOnMessage(connection, message, foundListener);
foundListener |= localManager.notifyOnMessage0(connection, message, foundListener);
}
// only run a flush once
if (foundListener) {
connection.send().flush();
} else {
Logger logger2 = this.logger;
if (logger2.isDebugEnabled()) {
this.logger.debug("----------- LISTENER NOT REGISTERED FOR TYPE: {}", message.getClass().getSimpleName());
}
}
return foundListener;
}
/**
@ -277,12 +279,12 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@Override
public final void notifyOnIdle(Connection connection) {
Set<Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<Listener<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>> entry : entrySet) {
Set<Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<ListenerRaw<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>> entry : entrySet) {
list = entry.getValue();
if (list != null) {
for (Listener<Connection,Object> listener : list) {
for (ListenerRaw<Connection,Object> listener : list) {
if (this.shutdown) {
return;
}
@ -311,12 +313,12 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
this.connections.add(connection);
try {
Set<Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<Listener<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>> entry : entrySet) {
Set<Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<ListenerRaw<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>> entry : entrySet) {
list = entry.getValue();
if (list != null) {
for (Listener<Connection,Object> listener : list) {
for (ListenerRaw<Connection,Object> listener : list) {
if (this.shutdown) {
return;
}
@ -343,12 +345,12 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@Override
public void connectionDisconnected(Connection connection) {
Set<Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<Listener<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>> entry : entrySet) {
Set<Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<ListenerRaw<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>> entry : entrySet) {
list = entry.getValue();
if (list != null) {
for (Listener<Connection, Object> listener : list) {
for (ListenerRaw<Connection, Object> listener : list) {
if (this.shutdown) {
return;
}
@ -378,12 +380,12 @@ public class ConnectionManager implements ListenerBridge, ISessionManager {
*/
@Override
public void connectionError(Connection connection, Throwable throwable) {
Set<Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<Listener<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<Listener<Connection, Object>>> entry : entrySet) {
Set<Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>>> entrySet = this.listeners.entrySet();
CopyOnWriteArrayList<ListenerRaw<Connection,Object>> list;
for (Entry<Type, CopyOnWriteArrayList<ListenerRaw<Connection, Object>>> entry : entrySet) {
list = entry.getValue();
if (list != null) {
for (Listener<Connection, Object> listener : list) {
for (ListenerRaw<Connection, Object> listener : list) {
if (this.shutdown) {
return;
}

View File

@ -2,11 +2,6 @@ package dorkbox.network.connection;
public interface ConnectionPoint {
/**
* Writes data to the pipe. <b>DOES NOT FLUSH</b> the pipe to the wire!
*/
public void write(Object object);
/**
* Waits for the last write to complete. Useful when sending large amounts of data at once.
*/

View File

@ -0,0 +1,9 @@
package dorkbox.network.connection;
public interface ConnectionPointWriter extends ConnectionPoint {
/**
* Writes data to the pipe. <b>DOES NOT FLUSH</b> the pipe to the wire!
*/
public void write(Object object);
}

View File

@ -3,6 +3,8 @@ package dorkbox.network.connection;
import org.slf4j.Logger;
import dorkbox.network.ConnectionOptions;
import dorkbox.network.connection.bridge.ConnectionBridge;
import dorkbox.network.connection.bridge.ConnectionBridgeFlushAlways;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -16,6 +18,9 @@ public class EndPointClient extends EndPointWithSerialization {
protected volatile boolean registrationComplete = false;
private volatile ConnectionBridgeFlushAlways connectionBridgeFlushAlways;
public EndPointClient(String name, ConnectionOptions options) throws InitializationException, SecurityException {
super(name, options);
}
@ -68,4 +73,21 @@ public class EndPointClient extends EndPointWithSerialization {
this.registrationInProgress = false;
stop();
}
/**
* Expose methods to send objects to a destination.
* <p>
* This returns a bridge that will flush after EVERY send! This is because sending data can occur on the client, outside
* of the normal eventloop patterns, and it is confusing to the user to have to manually flush the channel each time.
*/
@Override
public ConnectionBridge send() {
ConnectionBridgeFlushAlways connectionBridgeFlushAlways2 = this.connectionBridgeFlushAlways;
if (connectionBridgeFlushAlways2 == null) {
ConnectionBridge clientBridge = this.connectionManager.getConnection0().send();
this.connectionBridgeFlushAlways = new ConnectionBridgeFlushAlways(clientBridge);
}
return this.connectionBridgeFlushAlways;
}
}

View File

@ -1,6 +1,7 @@
package dorkbox.network.connection;
import dorkbox.network.ConnectionOptions;
import dorkbox.network.connection.bridge.ConnectionBridgeServer;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;

View File

@ -14,6 +14,7 @@ import org.bouncycastle.crypto.params.IESWithCipherParameters;
import com.esotericsoftware.kryo.factories.SerializerFactory;
import dorkbox.network.ConnectionOptions;
import dorkbox.network.connection.bridge.ConnectionBridgeBase;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration;
import dorkbox.network.connection.wrapper.ChannelLocalWrapper;
@ -33,7 +34,7 @@ import dorkbox.util.crypto.serialization.EccPublicKeySerializer;
import dorkbox.util.crypto.serialization.IesParametersSerializer;
import dorkbox.util.crypto.serialization.IesWithCipherParametersSerializer;
public class EndPointWithSerialization extends EndPoint {
public abstract class EndPointWithSerialization extends EndPoint {
protected final ConnectionManager connectionManager;
@ -219,6 +220,12 @@ public class EndPointWithSerialization extends EndPoint {
return (Collection<C>) this.connectionManager.getConnections();
}
/**
* Expose methods to send objects to a destination.
*/
public abstract ConnectionBridgeBase send();
/**
* Closes all connections ONLY (keeps the server/client running)
*/

View File

@ -1,98 +1,10 @@
package dorkbox.network.connection;
import dorkbox.util.ClassHelper;
public abstract class Listener<C extends Connection, M extends Object> {
private final Class<?> objectType;
// for compile time code. The generic type parameter #2 (index 1) is pulled from type arguments.
// generic parameters cannot be primitive types
public abstract class Listener<M extends Object> extends ListenerRaw<Connection, M> {
public Listener() {
this(1);
}
// for sub-classed listeners, we might have to specify which parameter to use.
protected Listener(int lastParameterIndex) {
if (lastParameterIndex > -1) {
Class<?> objectType = ClassHelper.getGenericParameterAsClassForSuperClass(getClass(), lastParameterIndex);
if (objectType != null) {
this.objectType = objectType;
} else {
this.objectType = Object.class;
}
} else {
// for when we want to override it
this.objectType = Object.class;
}
}
/**
* Gets the referenced object type.
*
* non-final so this can be overridden by listeners that aren't able to define their type as a generic parameter
*/
public Class<?> getObjectType() {
return this.objectType;
}
/**
* Called when the remote end has been connected. This will be invoked before any objects are received by the network.
* This method should not block for long periods as other network activity will not be processed
* until it returns.
*/
public void connected(C connection) {
}
/**
* Called when the remote end is no longer connected. There is no guarantee as to what thread will invoke this method.
* <p>
* Do not write data in this method! The channel can be closed, resulting in an error if you attempt to do so.
*/
public void disconnected(C connection) {
}
/**
* Called when an object has been received from the remote end of the connection.
* This method should not block for long periods as other network activity will not be processed until it returns.
*/
public void received(C connection, M message) {
}
/**
* Called when the connection is idle for longer than the {@link EndPoint#setIdleTimeout(idle) idle threshold}.
*/
public void idle(C connection) {
}
/**
* Called when there is an error of some kind during the up/down stream process (to/from the socket or otherwise)
*/
public void error(C connection, Throwable throwable) {
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() + "]";
super(0);
}
}

View File

@ -22,7 +22,7 @@ public interface ListenerBridge {
* the connection is notified on that event (ie, admin type listeners)
*/
@SuppressWarnings("rawtypes")
public void add(Listener listener);
public void add(ListenerRaw listener);
/**
* Removes a listener from this connection/endpoint to NO LONGER be notified
@ -37,7 +37,7 @@ public interface ListenerBridge {
* the connection is removed
*/
@SuppressWarnings("rawtypes")
public void remove(Listener listener);
public void remove(ListenerRaw listener);
/**
* Removes all registered listeners from this connection/endpoint to NO

View File

@ -0,0 +1,103 @@
package dorkbox.network.connection;
import dorkbox.util.ClassHelper;
public abstract class ListenerRaw<C extends Connection, M extends Object> {
private final Class<?> objectType;
// for compile time code. The generic type parameter #2 (index 1) is pulled from type arguments.
// generic parameters cannot be primitive types
public ListenerRaw() {
this(1);
}
// for sub-classed listeners, we might have to specify which parameter to use.
protected ListenerRaw(int lastParameterIndex) {
if (lastParameterIndex > -1) {
Class<?> objectType = ClassHelper.getGenericParameterAsClassForSuperClass(getClass(), lastParameterIndex);
if (objectType != null) {
// SOMETIMES generics get confused on which parameter we actually mean (when sub-classing)
if (objectType.isAssignableFrom(Connection.class)) {
objectType = ClassHelper.getGenericParameterAsClassForSuperClass(getClass(), lastParameterIndex+1);
}
this.objectType = objectType;
} else {
this.objectType = Object.class;
}
} else {
// for when we want to override it
this.objectType = Object.class;
}
}
/**
* Gets the referenced object type.
*
* non-final so this can be overridden by listeners that aren't able to define their type as a generic parameter
*/
public Class<?> getObjectType() {
return this.objectType;
}
/**
* Called when the remote end has been connected. This will be invoked before any objects are received by the network.
* This method should not block for long periods as other network activity will not be processed
* until it returns.
*/
public void connected(C connection) {
}
/**
* Called when the remote end is no longer connected. There is no guarantee as to what thread will invoke this method.
* <p>
* Do not write data in this method! The channel can be closed, resulting in an error if you attempt to do so.
*/
public void disconnected(C connection) {
}
/**
* Called when an object has been received from the remote end of the connection.
* This method should not block for long periods as other network activity will not be processed until it returns.
*/
public void received(C connection, M message) {
}
/**
* Called when the connection is idle for longer than the {@link EndPoint#setIdleTimeout(idle) idle threshold}.
*/
public void idle(C connection) {
}
/**
* Called when there is an error of some kind during the up/down stream process (to/from the socket or otherwise)
*/
public void error(C connection, Throwable throwable) {
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,7 @@
package dorkbox.network.connection;
class PingSystemListener extends Listener<ConnectionImpl, PingMessage> {
class PingSystemListener extends ListenerRaw<ConnectionImpl, PingMessage> {
PingSystemListener(String name) {
}

View File

@ -83,7 +83,7 @@ public class RegistrationWrapper implements UdpServer {
}
public void releaseChannelMap() {
// try to unlocal access
// try to unlock access
this.channelMapLock.unlock();
}

View File

@ -2,7 +2,11 @@ package dorkbox.network.connection;
import java.util.Collection;
public class ServerConnectionBridge implements ConnectionBridgeServer, ConnectionExceptSpecifiedBridgeServer {
import dorkbox.network.connection.bridge.ConnectionBridgeServer;
import dorkbox.network.connection.bridge.ConnectionExceptSpecifiedBridgeServer;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class ServerConnectionBridge implements ConnectionPoint, ConnectionBridgeServer, ConnectionExceptSpecifiedBridgeServer {
private final ConnectionManager connectionManager;
@ -15,11 +19,13 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
* via LOCAL when it's a local channel).
*/
@Override
public void TCP(Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
public ConnectionPoint TCP(Object message) {
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
c.send().TCP(message);
}
return this;
}
/**
@ -27,11 +33,13 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
* via LOCAL when it's a local channel).
*/
@Override
public void UDP(Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
public ConnectionPoint UDP(Object message) {
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
c.send().UDP(message);
}
return this;
}
/**
@ -39,13 +47,39 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
* via LOCAL when it's a local channel).
*/
@Override
public void UDT(Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
public ConnectionPoint UDT(Object message) {
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
c.send().UDT(message);
}
return this;
}
/**
* Not implemented, since this would cause horrendous problems.
*
* @see dorkbox.network.connection.ConnectionPoint#waitForWriteToComplete()
*/
@Override
public void waitForWriteToComplete() {
throw new NotImplementedException();
}
/**
* This will flush the data from EVERY connection on this server.
* <p>
* THIS WILL BE SLOW!
*
* @see dorkbox.network.connection.ConnectionPoint#flush()
*/
@Override
public void flush() {
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
c.send().flush();
}
}
/**
* Exposes methods to send the object to all server connections (except the specified one)
@ -62,7 +96,7 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
*/
@Override
public void TCP(Connection connection, Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
if (c != connection) {
c.send().TCP(message);
@ -76,7 +110,7 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
*/
@Override
public void UDP(Connection connection, Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
if (c != connection) {
c.send().UDP(message);
@ -90,11 +124,23 @@ public class ServerConnectionBridge implements ConnectionBridgeServer, Connectio
*/
@Override
public void UDT(Connection connection, Object message) {
Collection<Connection> connections0 = connectionManager.getConnections0();
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
if (c != connection) {
c.send().UDT(message);
}
}
}
/**
* Sends the message to other listeners INSIDE this endpoint for EVERY connection. It does not
* send it to a remote address.
*/
@Override
public void self(Object message) {
Collection<Connection> connections0 = this.connectionManager.getConnections0();
for (Connection c : connections0) {
this.connectionManager.notifyOnMessage(c, message);
}
}
}

View File

@ -0,0 +1,19 @@
package dorkbox.network.connection.bridge;
import dorkbox.network.connection.Ping;
public interface ConnectionBridge extends ConnectionBridgeBase {
/**
* Sends a "ping" packet, trying UDP, then UDT, then TCP (in that order) to measure <b>ROUND TRIP</b> time to the remote connection.
*
* @return Ping can have a listener attached, which will get called when the ping returns.
*/
public Ping ping();
/**
* Flushes the contents of the TCP/UDP/UDT/etc pipes to the actual transport.
*/
public void flush();
}

View File

@ -1,8 +1,10 @@
package dorkbox.network.connection;
package dorkbox.network.connection.bridge;
import dorkbox.network.connection.ConnectionPoint;
public interface ConnectionBridge {
public interface ConnectionBridgeBase {
/**
* Sends the message to other listeners INSIDE this endpoint. It does not
* send it to a remote address.
@ -26,16 +28,4 @@ public interface ConnectionBridge {
* local channel).
*/
public ConnectionPoint UDT(Object message);
/**
* Sends a "ping" packet, trying UDP, then UDT, then TCP (in that order) to measure <b>ROUND TRIP</b> time to the remote connection.
*
* @return Ping can have a listener attached, which will get called when the ping returns.
*/
public Ping ping();
/**
* Flushes the contents of the TCP/UDP/UDT/etc pipes to the actual transport.
*/
public void flush();
}

View File

@ -1,4 +1,7 @@
package dorkbox.network.connection;
package dorkbox.network.connection.bridge;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.Ping;
public class ConnectionBridgeFlushAlways implements ConnectionBridge {

View File

@ -0,0 +1,11 @@
package dorkbox.network.connection.bridge;
public interface ConnectionBridgeServer extends ConnectionBridgeBase {
/**
* Exposes methods to send the object to all server connections (except the specified one)
* over the network. (or via LOCAL when it's a local channel).
*/
public ConnectionExceptSpecifiedBridgeServer except();
}

View File

@ -1,4 +1,6 @@
package dorkbox.network.connection;
package dorkbox.network.connection.bridge;
import dorkbox.network.connection.Connection;
public interface ConnectionExceptSpecifiedBridgeServer {

View File

@ -1,9 +1,9 @@
package dorkbox.network.connection.idle;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
abstract class IdleListener<C extends Connection, M> extends Listener<C, M> {
abstract class IdleListener<C extends Connection, M> extends ListenerRaw<C, M> {
/**
* used by the Idle Sender

View File

@ -2,10 +2,10 @@
package dorkbox.network.connection.idle;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
abstract public class IdleSender<C extends Connection, M> extends Listener<C, M> implements IdleBridge {
abstract public class IdleSender<C extends Connection, M> extends ListenerRaw<C, M> implements IdleBridge {
volatile boolean started;
IdleListener<C, M> idleListener;

View File

@ -8,13 +8,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.ConnectionPointWriter;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.ISessionManager;
import dorkbox.network.connection.registration.MetaChannel;
public class ChannelLocalWrapper implements ChannelWrapper, ConnectionPoint {
public class ChannelLocalWrapper implements ChannelWrapper, ConnectionPointWriter {
private final Channel channel;
private String remoteAddress;
@ -68,17 +68,17 @@ public class ChannelLocalWrapper implements ChannelWrapper, ConnectionPoint {
}
@Override
public ConnectionPoint tcp() {
public ConnectionPointWriter tcp() {
return this;
}
@Override
public ConnectionPoint udp() {
public ConnectionPointWriter udp() {
return this;
}
@Override
public ConnectionPoint udt() {
public ConnectionPointWriter udt() {
return this;
}

View File

@ -5,9 +5,9 @@ import io.netty.channel.ChannelFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.ConnectionPointWriter;
public class ChannelNetwork implements ConnectionPoint {
public class ChannelNetwork implements ConnectionPointWriter {
private volatile ChannelFuture lastWriteFuture;
private final Channel channel;

View File

@ -8,8 +8,8 @@ import java.net.InetSocketAddress;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.ConnectionPointWriter;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.ISessionManager;
import dorkbox.network.connection.UdpServer;
@ -104,17 +104,17 @@ public class ChannelNetworkWrapper implements ChannelWrapper {
}
@Override
public ConnectionPoint tcp() {
public ConnectionPointWriter tcp() {
return this.tcp;
}
@Override
public ConnectionPoint udp() {
public ConnectionPointWriter udp() {
return this.udp;
}
@Override
public ConnectionPoint udt() {
public ConnectionPointWriter udt() {
return this.udt;
}

View File

@ -1,8 +1,9 @@
package dorkbox.network.connection.wrapper;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.ConnectionPointWriter;
public class ChannelNull implements ConnectionPoint {
public class ChannelNull implements ConnectionPointWriter {
private static final ConnectionPoint INSTANCE = new ChannelNull();
public static ConnectionPoint get() {

View File

@ -5,15 +5,15 @@ import io.netty.channel.EventLoop;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.ConnectionPointWriter;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionPoint;
import dorkbox.network.connection.ISessionManager;
public interface ChannelWrapper {
public ConnectionPoint tcp();
public ConnectionPoint udp();
public ConnectionPoint udt();
public ConnectionPointWriter tcp();
public ConnectionPointWriter udp();
public ConnectionPointWriter udt();
/**
* Initialize the connection with any extra info that is needed but was unavailable at the channel construction.

View File

@ -10,52 +10,52 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
/** Handles network communication when methods are invoked on a proxy. */
class RemoteInvocationHandler implements InvocationHandler {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RemoteInvocationHandler.class);
private final Connection connection;
private final Connection connection;
final int objectID;
private int timeoutMillis = 3000;
final int objectID;
private int timeoutMillis = 3000;
private boolean nonBlocking = false;
private boolean nonBlocking = false;
private boolean transmitReturnValue = true;
private boolean transmitExceptions = true;
private Byte lastResponseID;
private byte nextResponseNum = 1;
private Byte lastResponseID;
private byte nextResponseNum = 1;
private Listener<Connection, InvokeMethodResult> responseListener;
private ListenerRaw<Connection, InvokeMethodResult> responseListener;
final ReentrantLock lock = new ReentrantLock();
final ReentrantLock lock = new ReentrantLock();
final Condition responseCondition = lock.newCondition();
final ConcurrentHashMap<Byte, InvokeMethodResult> responseTable = new ConcurrentHashMap<Byte, InvokeMethodResult>();
public RemoteInvocationHandler(Connection connection, final int objectID) {
super();
this.connection = connection;
this.objectID = objectID;
super();
this.connection = connection;
this.objectID = objectID;
responseListener = new Listener<Connection, InvokeMethodResult>() {
responseListener = new ListenerRaw<Connection, InvokeMethodResult>() {
@Override
public void received (Connection connection, InvokeMethodResult invokeMethodResult) {
byte responseID = invokeMethodResult.responseID;
byte responseID = invokeMethodResult.responseID;
if (invokeMethodResult.objectID != objectID) {
// System.err.println("FAILED: " + responseID);
// logger.trace("{} FAILED to received data: {} with id ({})", connection, invokeMethodResult.result, invokeMethodResult.responseID);
return;
}
return;
}
// System.err.println("Recieved: " + responseID);
// logger.trace("{} received data: {} with id ({})", connection, invokeMethodResult.result, invokeMethodResult.responseID);
responseTable.put(responseID, invokeMethodResult);
responseTable.put(responseID, invokeMethodResult);
// System.err.println("L");
lock.lock();
@ -65,18 +65,18 @@ class RemoteInvocationHandler implements InvocationHandler {
lock.unlock();
// System.err.println("U");
}
}
}
@Override
@Override
public void disconnected(Connection connection) {
close();
}
};
close();
}
};
connection.listeners().add(responseListener);
}
connection.listeners().add(responseListener);
}
@Override
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
@ -123,7 +123,7 @@ class RemoteInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
if (method.getDeclaringClass() == RemoteObject.class) {
String name = method.getName();
if (name.equals("close")) {
@ -261,9 +261,9 @@ class RemoteInvocationHandler implements InvocationHandler {
}
}
private Object waitForResponse(byte responseID) {
private Object waitForResponse(byte responseID) {
long endTime = System.currentTimeMillis() + timeoutMillis;
long endTime = System.currentTimeMillis() + timeoutMillis;
long remaining = timeoutMillis;
while (remaining > 0) {
@ -297,6 +297,6 @@ class RemoteInvocationHandler implements InvocationHandler {
void close() {
connection.listeners().remove(responseListener);
}
connection.listeners().remove(responseListener);
}
}

View File

@ -5,7 +5,7 @@ import dorkbox.network.connection.Connection;
/** Provides access to various settings on a remote object.
* @see RmiBridge#getRemoteObject(dorkbox.networking.connection.interfaces.IConnection.Connection, int, Class...)
* @see RmiBridge#getRemoteObject(dorkbox.Connection.connection.interfaces.IConnection.Connection, int, Class...)
* @author Nathan Sweet <misc@n4te.com> */
public interface RemoteObject {
/** Sets the milliseconds to wait for a method to return value. Default is 3000. */
@ -38,7 +38,7 @@ public interface RemoteObject {
/** Waits for the response to the last method invocation to be received or the response timeout to be reached. Must not be
* called from the connection's update thread.
* @see RmiBridge#getRemoteObject(dorkbox.networking.connection.interfaces.IConnection.Connection, int, Class...) */
* @see RmiBridge#getRemoteObject(dorkbox.Connection.connection.interfaces.IConnection.Connection, int, Class...) */
public Object waitForLastResponse ();
/** Gets the ID of response for the last method invocation. */
@ -48,7 +48,7 @@ public interface RemoteObject {
* from the connection's update thread. Response IDs use a six bit identifier, with one identifier reserved for "no response".
* This means that this method should be called to get the result for a non-blocking call before an additional 63 non-blocking
* calls are made, or risk undefined behavior due to identical IDs.
* @see RmiBridge#getRemoteObject(dorkbox.networking.connection.interfaces.IConnection.Connection, int, Class...) */
* @see RmiBridge#getRemoteObject(dorkbox.Connection.connection.interfaces.IConnection.Connection, int, Class...) */
public Object waitForResponse (byte responseID);
/** Causes this RemoteObject to stop listening to the connection for method invocation response messages. */

View File

@ -29,7 +29,7 @@ import com.esotericsoftware.kryo.util.IntMap;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
import dorkbox.network.util.SerializationManager;
import dorkbox.util.primativeCollections.ObjectIntMap;
@ -78,7 +78,7 @@ public class RmiBridge {
// the name of who created this object space.
private final org.slf4j.Logger logger;
private final Listener<Connection, InvokeMethod> invokeListener= new Listener<Connection, InvokeMethod>() {
private final ListenerRaw<Connection, InvokeMethod> invokeListener= new ListenerRaw<Connection, InvokeMethod>() {
@Override
public void received(final Connection connection, final InvokeMethod invokeMethod) {
boolean found = false;

View File

@ -14,6 +14,7 @@ import dorkbox.util.storage.Storage;
* <p>
* A static "create" method, with any number of parameters, is required to create this class (which is done via reflection)
*/
@SuppressWarnings("deprecation")
public abstract class SettingsStore {
/**

View File

@ -60,7 +60,7 @@ public class ChunkedDataTest extends BaseTest {
addEndPoint(server);
server.setIdleTimeout(100);
server.bind(false);
server.listeners().add(new Listener<Connection, Data>() {
server.listeners().add(new Listener<Data>() {
@Override
public void connected (Connection connection) {
@ -82,7 +82,7 @@ public class ChunkedDataTest extends BaseTest {
Client client = new Client(connectionOptions);
register(client.getSerialization());
addEndPoint(client);
client.listeners().add(new Listener<Connection, Data>() {
client.listeners().add(new Listener<Data>() {
@Override
public void received(Connection connection, Data object) {
if (mainData.equals(object)) {

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
@ -10,7 +11,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -30,7 +30,7 @@ public class ClientSendTest extends BaseTest {
server.bind(false);
register(server.getSerialization());
server.listeners().add(new Listener<Connection, AMessage>() {
server.listeners().add(new Listener<AMessage>() {
@Override
public void received (Connection connection, AMessage object) {
System.err.println("Server received message from client. Bouncing back.");
@ -43,7 +43,7 @@ public class ClientSendTest extends BaseTest {
register(client.getSerialization());
client.connect(5000);
client.listeners().add(new Listener<Connection, AMessage>() {
client.listeners().add(new Listener<AMessage>() {
@Override
public void received (Connection connection, AMessage object) {
ClientSendTest.this.checkPassed.set(true);

View File

@ -2,6 +2,8 @@
package dorkbox.network;
import hive.common.Listener;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
@ -9,7 +11,6 @@ import java.util.TimerTask;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -119,12 +120,12 @@ public class ConnectionTest extends BaseTest {
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<Connection, Object>() {
server.listeners().add(new Listener<Object>() {
Timer timer = new Timer();
@Override
public void connected (final Connection connection) {
timer.schedule(new TimerTask() {
this.timer.schedule(new TimerTask() {
@Override
public void run () {
System.out.println("Disconnecting after 1 second.");
@ -146,9 +147,9 @@ public class ConnectionTest extends BaseTest {
}
addEndPoint(client);
client.listeners().add(new Listener<Connection, Object>() {
client.listeners().add(new Listener<Object>() {
@Override
public void disconnected (Connection connection) {
public void disconnected(Connection connection) {
stopEndPoints();
}
});

View File

@ -3,13 +3,13 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -39,7 +39,7 @@ public class DiscoverHostTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, Object>() {
client.listeners().add(new Listener<Object>() {
@Override
public void connected(Connection connection) {
DiscoverHostTest.this.connected = true;

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -13,7 +14,6 @@ import org.junit.Test;
import dorkbox.network.PingPongTest.TYPE;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.InputStreamSender;
import dorkbox.network.util.SerializationManager;
@ -97,7 +97,7 @@ public class IdleTest extends BaseTest {
addEndPoint(server);
server.setIdleTimeout(100);
server.bind(false);
server.listeners().add(new Listener<Connection, Data>() {
server.listeners().add(new Listener<Data>() {
@Override
public void connected (Connection connection) {
@ -116,7 +116,7 @@ public class IdleTest extends BaseTest {
Client client = new Client(connectionOptions);
register(client.getSerialization());
addEndPoint(client);
client.listeners().add(new Listener<Connection, Data>() {
client.listeners().add(new Listener<Data>() {
@Override
public void received(Connection connection, Data object) {
if (mainData.equals(object)) {
@ -144,7 +144,7 @@ public class IdleTest extends BaseTest {
addEndPoint(server);
server.setIdleTimeout(100);
server.bind(false);
server.listeners().add(new Listener<Connection, byte[]>() {
server.listeners().add(new Listener<byte[]>() {
@Override
public void connected (Connection connection) {
ByteArrayOutputStream output = new ByteArrayOutputStream(largeDataSize);
@ -182,7 +182,7 @@ public class IdleTest extends BaseTest {
Client client = new Client(connectionOptions);
client.getSerialization().setRegistrationRequired(false);
addEndPoint(client);
client.listeners().add(new Listener<Connection, byte[]>() {
client.listeners().add(new Listener<byte[]>() {
int total;
@Override

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.security.SecureRandom;
@ -11,7 +12,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -37,7 +37,7 @@ public class LargeBufferTest extends BaseTest {
server.bind(false);
register(server.getSerialization());
server.listeners().add(new Listener<Connection, LargeMessage>() {
server.listeners().add(new Listener<LargeMessage>() {
AtomicInteger received = new AtomicInteger();
AtomicInteger receivedBytes = new AtomicInteger();
@ -62,7 +62,7 @@ public class LargeBufferTest extends BaseTest {
register(client.getSerialization());
client.connect(5000);
client.listeners().add(new Listener<Connection, LargeMessage>() {
client.listeners().add(new Listener<LargeMessage>() {
AtomicInteger received = new AtomicInteger();
AtomicInteger receivedBytes = new AtomicInteger();

View File

@ -15,6 +15,7 @@ import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -28,6 +29,7 @@ public class ListenerTest extends BaseTest {
AtomicBoolean subClassWorkedOK = new AtomicBoolean(false);
AtomicBoolean subClassWorkedOK2 = new AtomicBoolean(false);
AtomicBoolean superClassWorkedOK = new AtomicBoolean(false);
AtomicBoolean superClass2WorkedOK = new AtomicBoolean(false);
AtomicBoolean disconnectWorkedOK = new AtomicBoolean(false);
// quick and dirty test to also test connection sub-classing
@ -37,7 +39,7 @@ public class ListenerTest extends BaseTest {
}
public void check() {
subClassWorkedOK.set(true);
ListenerTest.this.subClassWorkedOK.set(true);
}
}
@ -48,7 +50,7 @@ public class ListenerTest extends BaseTest {
@Override
public void check() {
subClassWorkedOK.set(true);
ListenerTest.this.subClassWorkedOK.set(true);
}
}
@ -71,7 +73,7 @@ public class ListenerTest extends BaseTest {
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<TestConnectionA, String>() {
server.listeners().add(new ListenerRaw<TestConnectionA, String>() {
@Override
public void received (TestConnectionA connection, String string) {
connection.check();
@ -80,11 +82,11 @@ public class ListenerTest extends BaseTest {
}
});
server.listeners().add(new Listener<Connection, String>() {
server.listeners().add(new Listener<String>() {
@Override
public void received (Connection connection, String string) {
// System.err.println("subclass check");
subClassWorkedOK2.set(true);
ListenerTest.this.subClassWorkedOK2.set(true);
}
});
@ -93,7 +95,17 @@ public class ListenerTest extends BaseTest {
@Override
public void received(Connection connection, Object string) {
// System.err.println("generic class check");
superClassWorkedOK.set(true);
ListenerTest.this.superClassWorkedOK.set(true);
}
});
// should be able to happen!
server.listeners().add(new ListenerRaw() {
@Override
public void received(Connection connection, Object string) {
// System.err.println("generic class check");
ListenerTest.this.superClass2WorkedOK.set(true);
}
});
@ -101,13 +113,13 @@ public class ListenerTest extends BaseTest {
@Override
public void disconnected(Connection connection) {
// System.err.println("disconnect check");
disconnectWorkedOK.set(true);
ListenerTest.this.disconnectWorkedOK.set(true);
}
});
// should not let this happen!
try {
server.listeners().add(new Listener<TestConnectionB, String>() {
server.listeners().add(new ListenerRaw<TestConnectionB, String>() {
@Override
public void received (TestConnectionB connection, String string) {
connection.check();
@ -115,7 +127,7 @@ public class ListenerTest extends BaseTest {
connection.send().TCP(string);
}
});
fail = "Should not be able to ADD listeners that are NOT the basetype or the interface";
this.fail = "Should not be able to ADD listeners that are NOT the basetype or the interface";
} catch (Exception e) {
System.err.println("Successfully did NOT add listener that was not the base class");
}
@ -126,20 +138,20 @@ public class ListenerTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, String>() {
client.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP(origString); // 20 a's
connection.send().TCP(ListenerTest.this.origString); // 20 a's
}
@Override
public void received (Connection connection, String string) {
if (count.get() < limit) {
count.getAndIncrement();
if (ListenerTest.this.count.get() < ListenerTest.this.limit) {
ListenerTest.this.count.getAndIncrement();
connection.send().TCP(string);
} else {
if (!origString.equals(string)) {
fail = "original string not equal to the string received";
if (!ListenerTest.this.origString.equals(string)) {
ListenerTest.this.fail = "original string not equal to the string received";
}
stopEndPoints();
}
@ -150,14 +162,15 @@ public class ListenerTest extends BaseTest {
client.connect(5000);
waitForThreads();
assertEquals(limit, count.get());
assertTrue(subClassWorkedOK.get());
assertTrue(subClassWorkedOK2.get());
assertTrue(superClassWorkedOK.get());
assertTrue(disconnectWorkedOK.get());
assertEquals(this.limit, this.count.get());
assertTrue(this.subClassWorkedOK.get());
assertTrue(this.subClassWorkedOK2.get());
assertTrue(this.superClassWorkedOK.get());
assertTrue(this.superClass2WorkedOK.get());
assertTrue(this.disconnectWorkedOK.get());
if (fail != null) {
fail(fail);
if (this.fail != null) {
fail(this.fail);
}
}
}

View File

@ -2,6 +2,7 @@
package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
@ -9,7 +10,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -30,13 +30,13 @@ public class MultipleServerTest extends BaseTest {
addEndPoint(server1);
server1.bind(false);
server1.listeners().add(new Listener<Connection, String>() {
server1.listeners().add(new Listener<String>() {
@Override
public void received (Connection connection, String object) {
if (!object.equals("client1")) {
fail();
}
if (received.incrementAndGet() == 2) {
if (MultipleServerTest.this.received.incrementAndGet() == 2) {
stopEndPoints();
}
}
@ -52,13 +52,13 @@ public class MultipleServerTest extends BaseTest {
server2.getSerialization().register(String[].class);
addEndPoint(server2);
server2.bind(false);
server2.listeners().add(new Listener<Connection, String>() {
server2.listeners().add(new Listener<String>() {
@Override
public void received (Connection connection, String object) {
if (!object.equals("client2")) {
fail();
}
if (received.incrementAndGet() == 2) {
if (MultipleServerTest.this.received.incrementAndGet() == 2) {
stopEndPoints();
}
}
@ -71,7 +71,7 @@ public class MultipleServerTest extends BaseTest {
Client client1 = new Client(connectionOptions1);
client1.getSerialization().register(String[].class);
addEndPoint(client1);
client1.listeners().add(new Listener<Connection, String>() {
client1.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("client1");
@ -85,7 +85,7 @@ public class MultipleServerTest extends BaseTest {
Client client2 = new Client(connectionOptions2);
client2.getSerialization().register(String[].class);
addEndPoint(client2);
client2.listeners().add(new Listener<Connection, String>() {
client2.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("client2");

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.assertEquals;
import hive.common.Listener;
import java.io.IOException;
import java.util.ArrayList;
@ -14,7 +15,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -45,7 +46,7 @@ public class MultipleThreadTest extends BaseTest {
server.getSerialization().register(DataClass.class);
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<Connection, DataClass>() {
server.listeners().add(new Listener<DataClass>() {
@Override
public void connected(final Connection connection) {
System.err.println("Client connected to server.");
@ -94,7 +95,7 @@ public class MultipleThreadTest extends BaseTest {
client.getSerialization().register(String[].class);
client.getSerialization().register(DataClass.class);
addEndPoint(client);
client.listeners().add(new Listener<Connection, DataClass>() {
client.listeners().add(new ListenerRaw<Connection, DataClass>() {
AtomicInteger received = new AtomicInteger(1);
@Override

View File

@ -2,6 +2,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.util.Arrays;
@ -10,7 +11,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -21,7 +21,7 @@ public class PingPongLocalTest extends BaseTest {
@Test
public void pingPongLocal() throws IOException, InitializationException, SecurityException {
fail = "Data not received.";
this.fail = "Data not received.";
final Data dataLOCAL = new Data();
populateData(dataLOCAL);
@ -30,18 +30,18 @@ public class PingPongLocalTest extends BaseTest {
addEndPoint(server);
register(server.getSerialization());
server.bind(false);
server.listeners().add(new Listener<Connection, Data>() {
server.listeners().add(new Listener<Data>() {
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
PingPongLocalTest.this.fail = "Error during processing. " + throwable;
}
@Override
public void received(Connection connection, Data data) {
connection.id();
if (!data.equals(dataLOCAL)) {
fail = "data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
PingPongLocalTest.this.fail = "data is not equal on server.";
throw new RuntimeException("Fail! " + PingPongLocalTest.this.fail);
}
connection.send().TCP(data);
}
@ -52,33 +52,33 @@ public class PingPongLocalTest extends BaseTest {
Client client = new Client();
addEndPoint(client);
register(client.getSerialization());
client.listeners().add(new Listener<Connection, Data>() {
client.listeners().add(new Listener<Data>() {
AtomicInteger check = new AtomicInteger(0);
@Override
public void connected(Connection connection) {
fail = null;
PingPongLocalTest.this.fail = null;
connection.send().TCP(dataLOCAL);
// connection.sendUDP(dataUDP); // TCP and UDP are the same for a local channel.
}
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
System.err.println(fail);
PingPongLocalTest.this.fail = "Error during processing. " + throwable;
System.err.println(PingPongLocalTest.this.fail);
}
@Override
public void received(Connection connection, Data data) {
if (!data.equals(dataLOCAL)) {
fail = "data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
PingPongLocalTest.this.fail = "data is not equal on client.";
throw new RuntimeException("Fail! " + PingPongLocalTest.this.fail);
}
if (check.getAndIncrement() <= tries) {
if (this.check.getAndIncrement() <= PingPongLocalTest.this.tries) {
connection.send().TCP(data);
} else {
System.err.println("Ran LOCAL " + tries + " times");
System.err.println("Ran LOCAL " + PingPongLocalTest.this.tries + " times");
stopEndPoints();
}
}
@ -88,8 +88,8 @@ public class PingPongLocalTest extends BaseTest {
waitForThreads();
if (fail != null) {
fail(fail);
if (this.fail != null) {
fail(this.fail);
}
}
@ -187,24 +187,24 @@ public class PingPongLocalTest extends BaseTest {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(Booleans);
result = prime * result + Arrays.hashCode(Bytes);
result = prime * result + Arrays.hashCode(Chars);
result = prime * result + Arrays.hashCode(Doubles);
result = prime * result + Arrays.hashCode(Floats);
result = prime * result + Arrays.hashCode(Ints);
result = prime * result + Arrays.hashCode(Longs);
result = prime * result + Arrays.hashCode(Shorts);
result = prime * result + Arrays.hashCode(booleans);
result = prime * result + Arrays.hashCode(bytes);
result = prime * result + Arrays.hashCode(chars);
result = prime * result + Arrays.hashCode(doubles);
result = prime * result + Arrays.hashCode(floats);
result = prime * result + Arrays.hashCode(ints);
result = prime * result + Arrays.hashCode(longs);
result = prime * result + Arrays.hashCode(shorts);
result = prime * result + (string == null ? 0 : string.hashCode());
result = prime * result + Arrays.hashCode(strings);
result = prime * result + Arrays.hashCode(this.Booleans);
result = prime * result + Arrays.hashCode(this.Bytes);
result = prime * result + Arrays.hashCode(this.Chars);
result = prime * result + Arrays.hashCode(this.Doubles);
result = prime * result + Arrays.hashCode(this.Floats);
result = prime * result + Arrays.hashCode(this.Ints);
result = prime * result + Arrays.hashCode(this.Longs);
result = prime * result + Arrays.hashCode(this.Shorts);
result = prime * result + Arrays.hashCode(this.booleans);
result = prime * result + Arrays.hashCode(this.bytes);
result = prime * result + Arrays.hashCode(this.chars);
result = prime * result + Arrays.hashCode(this.doubles);
result = prime * result + Arrays.hashCode(this.floats);
result = prime * result + Arrays.hashCode(this.ints);
result = prime * result + Arrays.hashCode(this.longs);
result = prime * result + Arrays.hashCode(this.shorts);
result = prime * result + (this.string == null ? 0 : this.string.hashCode());
result = prime * result + Arrays.hashCode(this.strings);
return result;
}
@ -220,62 +220,62 @@ public class PingPongLocalTest extends BaseTest {
return false;
}
Data other = (Data) obj;
if (!Arrays.equals(Booleans, other.Booleans)) {
if (!Arrays.equals(this.Booleans, other.Booleans)) {
return false;
}
if (!Arrays.equals(Bytes, other.Bytes)) {
if (!Arrays.equals(this.Bytes, other.Bytes)) {
return false;
}
if (!Arrays.equals(Chars, other.Chars)) {
if (!Arrays.equals(this.Chars, other.Chars)) {
return false;
}
if (!Arrays.equals(Doubles, other.Doubles)) {
if (!Arrays.equals(this.Doubles, other.Doubles)) {
return false;
}
if (!Arrays.equals(Floats, other.Floats)) {
if (!Arrays.equals(this.Floats, other.Floats)) {
return false;
}
if (!Arrays.equals(Ints, other.Ints)) {
if (!Arrays.equals(this.Ints, other.Ints)) {
return false;
}
if (!Arrays.equals(Longs, other.Longs)) {
if (!Arrays.equals(this.Longs, other.Longs)) {
return false;
}
if (!Arrays.equals(Shorts, other.Shorts)) {
if (!Arrays.equals(this.Shorts, other.Shorts)) {
return false;
}
if (!Arrays.equals(booleans, other.booleans)) {
if (!Arrays.equals(this.booleans, other.booleans)) {
return false;
}
if (!Arrays.equals(bytes, other.bytes)) {
if (!Arrays.equals(this.bytes, other.bytes)) {
return false;
}
if (!Arrays.equals(chars, other.chars)) {
if (!Arrays.equals(this.chars, other.chars)) {
return false;
}
if (!Arrays.equals(doubles, other.doubles)) {
if (!Arrays.equals(this.doubles, other.doubles)) {
return false;
}
if (!Arrays.equals(floats, other.floats)) {
if (!Arrays.equals(this.floats, other.floats)) {
return false;
}
if (!Arrays.equals(ints, other.ints)) {
if (!Arrays.equals(this.ints, other.ints)) {
return false;
}
if (!Arrays.equals(longs, other.longs)) {
if (!Arrays.equals(this.longs, other.longs)) {
return false;
}
if (!Arrays.equals(shorts, other.shorts)) {
if (!Arrays.equals(this.shorts, other.shorts)) {
return false;
}
if (string == null) {
if (this.string == null) {
if (other.string != null) {
return false;
}
} else if (!string.equals(other.string)) {
} else if (!this.string.equals(other.string)) {
return false;
}
if (!Arrays.equals(strings, other.strings)) {
if (!Arrays.equals(this.strings, other.strings)) {
return false;
}
return true;

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.util.Arrays;
@ -13,7 +14,6 @@ import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -33,7 +33,7 @@ public class PingPongTest extends BaseTest {
int origSize = EndPoint.udpMaxSize;
EndPoint.udpMaxSize = 2048;
fail = "Data not received.";
this.fail = "Data not received.";
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.tcpPort = tcpPort;
@ -52,38 +52,38 @@ public class PingPongTest extends BaseTest {
addEndPoint(server);
register(server.getSerialization());
server.bind(false);
server.listeners().add(new Listener<Connection, Data>() {
server.listeners().add(new Listener<Data>() {
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
PingPongTest.this.fail = "Error during processing. " + throwable;
}
@Override
public void received (Connection connection, Data data) {
if (data.type == TYPE.TCP) {
if (!data.equals(dataTCP)) {
fail = "TCP data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "TCP data is not equal on server.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
connection.send().TCP(data);
}
else if (data.type == TYPE.UDP) {
if (!data.equals(dataUDP)) {
fail = "UDP data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "UDP data is not equal on server.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
connection.send().UDP(data);
}
else if (data.type == TYPE.UDT) {
if (!data.equals(dataUDT)) {
fail = "UDT data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "UDT data is not equal on server.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
connection.send().UDT(data);
}
else {
fail = "Unknown data type on server.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "Unknown data type on server.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
}
});
@ -93,7 +93,7 @@ public class PingPongTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
register(client.getSerialization());
client.listeners().add(new Listener<Connection, Data>() {
client.listeners().add(new Listener<Data>() {
AtomicInteger checkTCP = new AtomicInteger(0);
AtomicInteger checkUDP = new AtomicInteger(0);
AtomicInteger checkUDT = new AtomicInteger(0);
@ -103,7 +103,7 @@ public class PingPongTest extends BaseTest {
@Override
public void connected (Connection connection) {
fail = null;
PingPongTest.this.fail = null;
connection.send().TCP(dataTCP);
connection.send().UDP(dataUDP); // UDP ping pong stops if a UDP packet is lost.
connection.send().UDT(dataUDT);
@ -111,7 +111,7 @@ public class PingPongTest extends BaseTest {
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
PingPongTest.this.fail = "Error during processing. " + throwable;
throwable.printStackTrace();
}
@ -119,44 +119,44 @@ public class PingPongTest extends BaseTest {
public void received (Connection connection, Data data) {
if (data.type == TYPE.TCP) {
if (!data.equals(dataTCP)) {
fail = "TCP data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "TCP data is not equal on client.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
if (checkTCP.getAndIncrement() <= tries) {
if (this.checkTCP.getAndIncrement() <= PingPongTest.this.tries) {
connection.send().TCP(data);
} else {
System.err.println("TCP done.");
doneTCP.set(true);
this.doneTCP.set(true);
}
} else if (data.type == TYPE.UDP) {
if (!data.equals(dataUDP)) {
fail = "UDP data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "UDP data is not equal on client.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
if (checkUDP.getAndIncrement() <= tries) {
if (this.checkUDP.getAndIncrement() <= PingPongTest.this.tries) {
connection.send().UDP(data);
} else {
System.err.println("UDP done.");
doneUDP.set(true);
this.doneUDP.set(true);
}
} else if (data.type == TYPE.UDT) {
if (!data.equals(dataUDT)) {
fail = "UDT data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "UDT data is not equal on client.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
if (checkUDT.getAndIncrement() <= tries) {
if (this.checkUDT.getAndIncrement() <= PingPongTest.this.tries) {
connection.send().UDT(data);
} else {
System.err.println("UDT done.");
doneUDT.set(true);
this.doneUDT.set(true);
}
} else {
fail = "Unknown data type on client.";
throw new RuntimeException("Fail! " + fail);
PingPongTest.this.fail = "Unknown data type on client.";
throw new RuntimeException("Fail! " + PingPongTest.this.fail);
}
if (doneTCP.get() && doneUDP.get() && doneUDT.get()) {
System.err.println("Ran TCP, UDP, UDT " + tries + " times each");
if (this.doneTCP.get() && this.doneUDP.get() && this.doneUDT.get()) {
System.err.println("Ran TCP, UDP, UDT " + PingPongTest.this.tries + " times each");
stopEndPoints();
}
}
@ -166,8 +166,8 @@ public class PingPongTest extends BaseTest {
waitForThreads();
if (fail != null) {
fail(fail);
if (this.fail != null) {
fail(this.fail);
}
EndPoint.udpMaxSize = origSize;
@ -251,25 +251,25 @@ public class PingPongTest extends BaseTest {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(Booleans);
result = prime * result + Arrays.hashCode(Bytes);
result = prime * result + Arrays.hashCode(Chars);
result = prime * result + Arrays.hashCode(Doubles);
result = prime * result + Arrays.hashCode(Floats);
result = prime * result + Arrays.hashCode(Ints);
result = prime * result + Arrays.hashCode(Longs);
result = prime * result + Arrays.hashCode(Shorts);
result = prime * result + Arrays.hashCode(booleans);
result = prime * result + Arrays.hashCode(bytes);
result = prime * result + Arrays.hashCode(chars);
result = prime * result + Arrays.hashCode(doubles);
result = prime * result + Arrays.hashCode(floats);
result = prime * result + Arrays.hashCode(ints);
result = prime * result + Arrays.hashCode(longs);
result = prime * result + Arrays.hashCode(shorts);
result = prime * result + (string == null ? 0 : string.hashCode());
result = prime * result + Arrays.hashCode(strings);
result = prime * result + (type == null ? 0 : type.hashCode());
result = prime * result + Arrays.hashCode(this.Booleans);
result = prime * result + Arrays.hashCode(this.Bytes);
result = prime * result + Arrays.hashCode(this.Chars);
result = prime * result + Arrays.hashCode(this.Doubles);
result = prime * result + Arrays.hashCode(this.Floats);
result = prime * result + Arrays.hashCode(this.Ints);
result = prime * result + Arrays.hashCode(this.Longs);
result = prime * result + Arrays.hashCode(this.Shorts);
result = prime * result + Arrays.hashCode(this.booleans);
result = prime * result + Arrays.hashCode(this.bytes);
result = prime * result + Arrays.hashCode(this.chars);
result = prime * result + Arrays.hashCode(this.doubles);
result = prime * result + Arrays.hashCode(this.floats);
result = prime * result + Arrays.hashCode(this.ints);
result = prime * result + Arrays.hashCode(this.longs);
result = prime * result + Arrays.hashCode(this.shorts);
result = prime * result + (this.string == null ? 0 : this.string.hashCode());
result = prime * result + Arrays.hashCode(this.strings);
result = prime * result + (this.type == null ? 0 : this.type.hashCode());
return result;
}
@ -285,65 +285,65 @@ public class PingPongTest extends BaseTest {
return false;
}
Data other = (Data) obj;
if (!Arrays.equals(Booleans, other.Booleans)) {
if (!Arrays.equals(this.Booleans, other.Booleans)) {
return false;
}
if (!Arrays.equals(Bytes, other.Bytes)) {
if (!Arrays.equals(this.Bytes, other.Bytes)) {
return false;
}
if (!Arrays.equals(Chars, other.Chars)) {
if (!Arrays.equals(this.Chars, other.Chars)) {
return false;
}
if (!Arrays.equals(Doubles, other.Doubles)) {
if (!Arrays.equals(this.Doubles, other.Doubles)) {
return false;
}
if (!Arrays.equals(Floats, other.Floats)) {
if (!Arrays.equals(this.Floats, other.Floats)) {
return false;
}
if (!Arrays.equals(Ints, other.Ints)) {
if (!Arrays.equals(this.Ints, other.Ints)) {
return false;
}
if (!Arrays.equals(Longs, other.Longs)) {
if (!Arrays.equals(this.Longs, other.Longs)) {
return false;
}
if (!Arrays.equals(Shorts, other.Shorts)) {
if (!Arrays.equals(this.Shorts, other.Shorts)) {
return false;
}
if (!Arrays.equals(booleans, other.booleans)) {
if (!Arrays.equals(this.booleans, other.booleans)) {
return false;
}
if (!Arrays.equals(bytes, other.bytes)) {
if (!Arrays.equals(this.bytes, other.bytes)) {
return false;
}
if (!Arrays.equals(chars, other.chars)) {
if (!Arrays.equals(this.chars, other.chars)) {
return false;
}
if (!Arrays.equals(doubles, other.doubles)) {
if (!Arrays.equals(this.doubles, other.doubles)) {
return false;
}
if (!Arrays.equals(floats, other.floats)) {
if (!Arrays.equals(this.floats, other.floats)) {
return false;
}
if (!Arrays.equals(ints, other.ints)) {
if (!Arrays.equals(this.ints, other.ints)) {
return false;
}
if (!Arrays.equals(longs, other.longs)) {
if (!Arrays.equals(this.longs, other.longs)) {
return false;
}
if (!Arrays.equals(shorts, other.shorts)) {
if (!Arrays.equals(this.shorts, other.shorts)) {
return false;
}
if (string == null) {
if (this.string == null) {
if (other.string != null) {
return false;
}
} else if (!string.equals(other.string)) {
} else if (!this.string.equals(other.string)) {
return false;
}
if (!Arrays.equals(strings, other.strings)) {
if (!Arrays.equals(this.strings, other.strings)) {
return false;
}
if (type != other.type) {
if (this.type != other.type) {
return false;
}
return true;

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.assertEquals;
import hive.common.Listener;
import java.io.IOException;
import java.util.Timer;
@ -12,7 +13,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -30,9 +30,9 @@ public class ReconnectTest extends BaseTest {
final Server server = new Server(connectionOptions);
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<Connection, Object>() {
server.listeners().add(new Listener<Object>() {
@Override
public void connected (final Connection connection) {
public void connected(final Connection connection) {
timer.schedule(new TimerTask() {
@Override
public void run () {
@ -48,7 +48,7 @@ public class ReconnectTest extends BaseTest {
final AtomicInteger reconnectCount = new AtomicInteger();
final Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, Object>() {
client.listeners().add(new Listener<Object>() {
@Override
public void disconnected (Connection connection) {
if (reconnectCount.getAndIncrement() == 2) {

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.assertEquals;
import hive.common.Listener;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
@ -10,7 +11,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -20,8 +20,8 @@ public class ReuseTest extends BaseTest {
@Test
public void socketReuse() throws IOException, InitializationException, SecurityException {
serverCount = new AtomicInteger(0);
clientCount = new AtomicInteger(0);
this.serverCount = new AtomicInteger(0);
this.clientCount = new AtomicInteger(0);
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.tcpPort = tcpPort;
@ -30,7 +30,7 @@ public class ReuseTest extends BaseTest {
Server server = new Server(connectionOptions);
addEndPoint(server);
server.listeners().add(new Listener<Connection, String>() {
server.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("-- TCP from server");
@ -39,7 +39,7 @@ public class ReuseTest extends BaseTest {
@Override
public void received (Connection connection, String object) {
int incrementAndGet = serverCount.incrementAndGet();
int incrementAndGet = ReuseTest.this.serverCount.incrementAndGet();
System.err.println("<S " + connection + "> " + incrementAndGet + " : " + object);
}
});
@ -48,7 +48,7 @@ public class ReuseTest extends BaseTest {
Client client = new Client(connectionOptions);
addEndPoint(client);
client.listeners().add(new Listener<Connection, String>() {
client.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("-- TCP from client");
@ -57,7 +57,7 @@ public class ReuseTest extends BaseTest {
@Override
public void received (Connection connection, String object) {
int incrementAndGet = clientCount.incrementAndGet();
int incrementAndGet = ReuseTest.this.clientCount.incrementAndGet();
System.err.println("<C " + connection + "> " + incrementAndGet + " : " + object);
}
});
@ -68,7 +68,7 @@ public class ReuseTest extends BaseTest {
client.connect(5000);
int target = i*2;
while (serverCount.get() != target || clientCount.get() != target) {
while (this.serverCount.get() != target || this.clientCount.get() != target) {
System.err.println("Waiting...");
try {
Thread.sleep(100);
@ -79,7 +79,7 @@ public class ReuseTest extends BaseTest {
client.close();
}
assertEquals(count * 2 * 2, clientCount.get() + serverCount.get());
assertEquals(count * 2 * 2, this.clientCount.get() + this.serverCount.get());
stopEndPoints();
waitForThreads(10);
@ -87,12 +87,12 @@ public class ReuseTest extends BaseTest {
@Test
public void localReuse() throws IOException, InitializationException, SecurityException {
serverCount = new AtomicInteger(0);
clientCount = new AtomicInteger(0);
this.serverCount = new AtomicInteger(0);
this.clientCount = new AtomicInteger(0);
Server server = new Server();
addEndPoint(server);
server.listeners().add(new Listener<Connection, String>() {
server.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("-- LOCAL from server");
@ -100,7 +100,7 @@ public class ReuseTest extends BaseTest {
@Override
public void received (Connection connection, String object) {
int incrementAndGet = serverCount.incrementAndGet();
int incrementAndGet = ReuseTest.this.serverCount.incrementAndGet();
System.err.println("<S " + connection + "> " + incrementAndGet + " : " + object);
}
});
@ -109,7 +109,7 @@ public class ReuseTest extends BaseTest {
Client client = new Client();
addEndPoint(client);
client.listeners().add(new Listener<Connection, String>() {
client.listeners().add(new Listener<String>() {
@Override
public void connected (Connection connection) {
connection.send().TCP("-- LOCAL from client");
@ -117,7 +117,7 @@ public class ReuseTest extends BaseTest {
@Override
public void received (Connection connection, String object) {
int incrementAndGet = clientCount.incrementAndGet();
int incrementAndGet = ReuseTest.this.clientCount.incrementAndGet();
System.err.println("<C " + connection + "> " + incrementAndGet + " : " + object);
}
});
@ -128,7 +128,7 @@ public class ReuseTest extends BaseTest {
client.connect(5000);
int target = i;
while (serverCount.get() != target || clientCount.get() != target) {
while (this.serverCount.get() != target || this.clientCount.get() != target) {
System.err.println("Waiting...");
try {
Thread.sleep(100);
@ -139,7 +139,7 @@ public class ReuseTest extends BaseTest {
client.close();
}
assertEquals(count * 2, clientCount.get() + serverCount.get());
assertEquals(count * 2, this.clientCount.get() + this.serverCount.get());
stopEndPoints();
waitForThreads(10);

View File

@ -3,6 +3,7 @@ package dorkbox.network;
import static org.junit.Assert.fail;
import hive.common.Listener;
import java.io.IOException;
import java.util.Arrays;
@ -13,7 +14,6 @@ import org.junit.Test;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -34,7 +34,7 @@ public class UnregisteredClassTest extends BaseTest {
connectionOptions.udpPort = udpPort;
connectionOptions.host = host;
System.err.println("Running test " + tries + " times, please wait for it to finish.");
System.err.println("Running test " + this.tries + " times, please wait for it to finish.");
final Data dataTCP = new Data();
populateData(dataTCP, true);
@ -45,28 +45,28 @@ public class UnregisteredClassTest extends BaseTest {
server.getSerialization().setRegistrationRequired(false);
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<Connection, Data>() {
server.listeners().add(new Listener<Data>() {
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
UnregisteredClassTest.this.fail = "Error during processing. " + throwable;
}
@Override
public void received (Connection connection, Data data) {
if (data.isTCP) {
if (!data.equals(dataTCP)) {
fail = "TCP data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
UnregisteredClassTest.this.fail = "TCP data is not equal on server.";
throw new RuntimeException("Fail! " + UnregisteredClassTest.this.fail);
}
connection.send().TCP(data);
receivedTCP.incrementAndGet();
UnregisteredClassTest.this.receivedTCP.incrementAndGet();
} else {
if (!data.equals(dataUDP)) {
fail = "UDP data is not equal on server.";
throw new RuntimeException("Fail! " + fail);
UnregisteredClassTest.this.fail = "UDP data is not equal on server.";
throw new RuntimeException("Fail! " + UnregisteredClassTest.this.fail);
}
connection.send().UDP(data);
receivedUDP.incrementAndGet();
UnregisteredClassTest.this.receivedUDP.incrementAndGet();
}
}
});
@ -76,7 +76,7 @@ public class UnregisteredClassTest extends BaseTest {
Client client = new Client(connectionOptions);
client.getSerialization().setRegistrationRequired(false);
addEndPoint(client);
client.listeners().add(new Listener<Connection, Data>() {
client.listeners().add(new Listener<Data>() {
AtomicInteger checkTCP = new AtomicInteger(0);
AtomicInteger checkUDP = new AtomicInteger(0);
AtomicBoolean doneTCP = new AtomicBoolean(false);
@ -84,47 +84,47 @@ public class UnregisteredClassTest extends BaseTest {
@Override
public void connected (Connection connection) {
fail = null;
UnregisteredClassTest.this.fail = null;
connection.send().TCP(dataTCP);
connection.send().UDP(dataUDP); // UDP ping pong stops if a UDP packet is lost.
}
@Override
public void error(Connection connection, Throwable throwable) {
fail = "Error during processing. " + throwable;
System.err.println(fail);
UnregisteredClassTest.this.fail = "Error during processing. " + throwable;
System.err.println(UnregisteredClassTest.this.fail);
}
@Override
public void received (Connection connection, Data data) {
if (data.isTCP) {
if (!data.equals(dataTCP)) {
fail = "TCP data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
UnregisteredClassTest.this.fail = "TCP data is not equal on client.";
throw new RuntimeException("Fail! " + UnregisteredClassTest.this.fail);
}
if (checkTCP.getAndIncrement() <= tries) {
if (this.checkTCP.getAndIncrement() <= UnregisteredClassTest.this.tries) {
connection.send().TCP(data);
receivedTCP.incrementAndGet();
UnregisteredClassTest.this.receivedTCP.incrementAndGet();
} else {
System.err.println("TCP done.");
doneTCP.set(true);
this.doneTCP.set(true);
}
} else {
if (!data.equals(dataUDP)) {
fail = "UDP data is not equal on client.";
throw new RuntimeException("Fail! " + fail);
UnregisteredClassTest.this.fail = "UDP data is not equal on client.";
throw new RuntimeException("Fail! " + UnregisteredClassTest.this.fail);
}
if (checkUDP.getAndIncrement() <= tries) {
if (this.checkUDP.getAndIncrement() <= UnregisteredClassTest.this.tries) {
connection.send().UDP(data);
receivedUDP.incrementAndGet();
UnregisteredClassTest.this.receivedUDP.incrementAndGet();
} else {
System.err.println("UDP done.");
doneUDP.set(true);
this.doneUDP.set(true);
}
}
if (doneTCP.get() && doneUDP.get()) {
System.err.println("Ran TCP & UDP " + tries + " times each");
if (this.doneTCP.get() && this.doneUDP.get()) {
System.err.println("Ran TCP & UDP " + UnregisteredClassTest.this.tries + " times each");
stopEndPoints();
}
}
@ -133,14 +133,14 @@ public class UnregisteredClassTest extends BaseTest {
client.connect(5000);
waitForThreads();
if (fail != null) {
fail(fail);
if (this.fail != null) {
fail(this.fail);
}
EndPoint.udpMaxSize = origSize;
}
private void populateData (Data data, boolean isTCP) {
private void populateData(Data data, boolean isTCP) {
data.isTCP = isTCP;
StringBuilder buffer = new StringBuilder();
@ -196,25 +196,25 @@ public class UnregisteredClassTest extends BaseTest {
public int hashCode () {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(Booleans);
result = prime * result + Arrays.hashCode(Bytes);
result = prime * result + Arrays.hashCode(Chars);
result = prime * result + Arrays.hashCode(Doubles);
result = prime * result + Arrays.hashCode(Floats);
result = prime * result + Arrays.hashCode(Ints);
result = prime * result + Arrays.hashCode(Longs);
result = prime * result + Arrays.hashCode(Shorts);
result = prime * result + Arrays.hashCode(booleans);
result = prime * result + Arrays.hashCode(bytes);
result = prime * result + Arrays.hashCode(chars);
result = prime * result + Arrays.hashCode(doubles);
result = prime * result + Arrays.hashCode(floats);
result = prime * result + Arrays.hashCode(ints);
result = prime * result + (isTCP ? 1231 : 1237);
result = prime * result + Arrays.hashCode(longs);
result = prime * result + Arrays.hashCode(shorts);
result = prime * result + (string == null ? 0 : string.hashCode());
result = prime * result + Arrays.hashCode(strings);
result = prime * result + Arrays.hashCode(this.Booleans);
result = prime * result + Arrays.hashCode(this.Bytes);
result = prime * result + Arrays.hashCode(this.Chars);
result = prime * result + Arrays.hashCode(this.Doubles);
result = prime * result + Arrays.hashCode(this.Floats);
result = prime * result + Arrays.hashCode(this.Ints);
result = prime * result + Arrays.hashCode(this.Longs);
result = prime * result + Arrays.hashCode(this.Shorts);
result = prime * result + Arrays.hashCode(this.booleans);
result = prime * result + Arrays.hashCode(this.bytes);
result = prime * result + Arrays.hashCode(this.chars);
result = prime * result + Arrays.hashCode(this.doubles);
result = prime * result + Arrays.hashCode(this.floats);
result = prime * result + Arrays.hashCode(this.ints);
result = prime * result + (this.isTCP ? 1231 : 1237);
result = prime * result + Arrays.hashCode(this.longs);
result = prime * result + Arrays.hashCode(this.shorts);
result = prime * result + (this.string == null ? 0 : this.string.hashCode());
result = prime * result + Arrays.hashCode(this.strings);
return result;
}
@ -230,65 +230,65 @@ public class UnregisteredClassTest extends BaseTest {
return false;
}
Data other = (Data)obj;
if (!Arrays.equals(Booleans, other.Booleans)) {
if (!Arrays.equals(this.Booleans, other.Booleans)) {
return false;
}
if (!Arrays.equals(Bytes, other.Bytes)) {
if (!Arrays.equals(this.Bytes, other.Bytes)) {
return false;
}
if (!Arrays.equals(Chars, other.Chars)) {
if (!Arrays.equals(this.Chars, other.Chars)) {
return false;
}
if (!Arrays.equals(Doubles, other.Doubles)) {
if (!Arrays.equals(this.Doubles, other.Doubles)) {
return false;
}
if (!Arrays.equals(Floats, other.Floats)) {
if (!Arrays.equals(this.Floats, other.Floats)) {
return false;
}
if (!Arrays.equals(Ints, other.Ints)) {
if (!Arrays.equals(this.Ints, other.Ints)) {
return false;
}
if (!Arrays.equals(Longs, other.Longs)) {
if (!Arrays.equals(this.Longs, other.Longs)) {
return false;
}
if (!Arrays.equals(Shorts, other.Shorts)) {
if (!Arrays.equals(this.Shorts, other.Shorts)) {
return false;
}
if (!Arrays.equals(booleans, other.booleans)) {
if (!Arrays.equals(this.booleans, other.booleans)) {
return false;
}
if (!Arrays.equals(bytes, other.bytes)) {
if (!Arrays.equals(this.bytes, other.bytes)) {
return false;
}
if (!Arrays.equals(chars, other.chars)) {
if (!Arrays.equals(this.chars, other.chars)) {
return false;
}
if (!Arrays.equals(doubles, other.doubles)) {
if (!Arrays.equals(this.doubles, other.doubles)) {
return false;
}
if (!Arrays.equals(floats, other.floats)) {
if (!Arrays.equals(this.floats, other.floats)) {
return false;
}
if (!Arrays.equals(ints, other.ints)) {
if (!Arrays.equals(this.ints, other.ints)) {
return false;
}
if (isTCP != other.isTCP) {
if (this.isTCP != other.isTCP) {
return false;
}
if (!Arrays.equals(longs, other.longs)) {
if (!Arrays.equals(this.longs, other.longs)) {
return false;
}
if (!Arrays.equals(shorts, other.shorts)) {
if (!Arrays.equals(this.shorts, other.shorts)) {
return false;
}
if (string == null) {
if (this.string == null) {
if (other.string != null) {
return false;
}
} else if (!string.equals(other.string)) {
} else if (!this.string.equals(other.string)) {
return false;
}
if (!Arrays.equals(strings, other.strings)) {
if (!Arrays.equals(this.strings, other.strings)) {
return false;
}
return true;

View File

@ -12,7 +12,7 @@ import dorkbox.network.ConnectionOptions;
import dorkbox.network.BaseTest;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerRaw;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -52,7 +52,7 @@ public class RmiSendObjectTest extends BaseTest {
serverRMI.register(42, serverTestObject);
serverRMI.register(777, serverTestObject.getOtherObject());
server.listeners().add(new Listener<Connection, OtherObjectImpl>() {
server.listeners().add(new ListenerRaw<Connection, OtherObjectImpl>() {
@Override
public void connected(final Connection connection) {
// Allow the connection to access objects in the ObjectSpace.
@ -74,7 +74,7 @@ public class RmiSendObjectTest extends BaseTest {
register(client.getSerialization());
addEndPoint(client);
client.listeners().add(new Listener<Connection, Object>() {
client.listeners().add(new ListenerRaw<Connection, Object>() {
@Override
public void connected(final Connection connection) {
new Thread(new Runnable() {

View File

@ -4,17 +4,17 @@ package dorkbox.network.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import hive.common.Listener;
import java.io.IOException;
import org.junit.Test;
import dorkbox.network.BaseTest;
import dorkbox.network.Client;
import dorkbox.network.ConnectionOptions;
import dorkbox.network.BaseTest;
import dorkbox.network.Server;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.Listener;
import dorkbox.network.util.SerializationManager;
import dorkbox.network.util.exceptions.InitializationException;
import dorkbox.network.util.exceptions.SecurityException;
@ -38,10 +38,10 @@ public class RmiTest extends BaseTest {
addEndPoint(server);
server.bind(false);
server.listeners().add(new Listener<Connection, MessageWithTestObject>() {
server.listeners().add(new Listener<MessageWithTestObject>() {
@Override
public void received (Connection connection, MessageWithTestObject m) {
assertEquals(SERVER_ID, m.testObject.other());
assertEquals(SERVER_ID, m.testObject.id());
System.err.println("Client Finished!");
runTest(connection, REMOTE_ID_ON_CLIENT, CLIENT_ID);
@ -54,7 +54,7 @@ public class RmiTest extends BaseTest {
register(client.getSerialization());
addEndPoint(client);
client.listeners().add(new Listener<Connection, MessageWithTestObject>() {
client.listeners().add(new Listener<MessageWithTestObject>() {
@Override
public void connected (final Connection connection) {
RmiTest.runTest(connection, REMOTE_ID_ON_SERVER, SERVER_ID);
@ -62,7 +62,7 @@ public class RmiTest extends BaseTest {
@Override
public void received (Connection connection, MessageWithTestObject m) {
assertEquals(CLIENT_ID, m.testObject.other());
assertEquals(CLIENT_ID, m.testObject.id());
System.err.println("Server Finished!");
stopEndPoints(2000);
@ -96,7 +96,7 @@ public class RmiTest extends BaseTest {
// (return values and exceptions are returned, call is synchronous)
test.moo();
test.moo("Cow");
assertEquals(otherID, test.other());
assertEquals(otherID, test.id());
// Test that RMI correctly waits for the remotely invoked method to exit
remoteObject.setResponseTimeout(5000);
@ -118,7 +118,7 @@ public class RmiTest extends BaseTest {
remoteObject.setTransmitReturnValue(false);
test.moo("Baa");
test.other();
test.id();
caught = false;
try {
test.throwException();
@ -131,16 +131,16 @@ public class RmiTest extends BaseTest {
remoteObject.setNonBlocking(true);
remoteObject.setTransmitReturnValue(false);
test.moo("Meow");
assertEquals(0, test.other());
assertEquals(0, test.id());
// Non-blocking call that returns the return value
remoteObject.setTransmitReturnValue(true);
test.moo("Foo");
assertEquals(0, test.other());
assertEquals(0, test.id());
assertEquals(otherID, remoteObject.waitForLastResponse());
assertEquals(0, test.other());
assertEquals(0, test.id());
byte responseID = remoteObject.getLastResponseID();
assertEquals(otherID, remoteObject.waitForResponse(responseID));
@ -184,15 +184,15 @@ public class RmiTest extends BaseTest {
public void moo (String value, long delay);
public int other ();
public int id ();
}
static public class TestObjectImpl implements TestObject {
public long value = System.currentTimeMillis();
private final int other;
private final int id;
public TestObjectImpl (int other) {
this.other = other;
public TestObjectImpl (int id) {
this.id = id;
}
@Override
@ -222,8 +222,8 @@ public class RmiTest extends BaseTest {
}
@Override
public int other () {
return other;
public int id () {
return this.id;
}
}