diff --git a/Dorkbox-Network/src/dorkbox/network/connection/EndPoint.java b/Dorkbox-Network/src/dorkbox/network/connection/EndPoint.java index cc68d067..5e89649b 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/EndPoint.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/EndPoint.java @@ -304,7 +304,9 @@ public abstract class EndPoint { } /** - * Closes all connections ONLY (keeps the server/client running) + * Closes all connections ONLY (keeps the server/client running). + *

+ * This is used, for example, when reconnecting to a server. The server should ALWAYS use STOP. */ public void close() { // give a chance to other threads. @@ -328,7 +330,7 @@ public abstract class EndPoint { /** * Safely closes all associated resources/threads/connections */ - public final void stop() { + public void stop() { // check to make sure we are in our OWN thread, otherwise, this thread will never exit -- because it will wait indefinitely // for itself to finish (since it blocks itself). // This occurs when calling stop from within a listener callback. @@ -442,7 +444,7 @@ public abstract class EndPoint { /** * Extra actions to perform when stopping this endpoint. */ - protected void stopExtraActions() { + void stopExtraActions() { } public String getName() { diff --git a/Dorkbox-Network/src/dorkbox/network/connection/EndPointWithSerialization.java b/Dorkbox-Network/src/dorkbox/network/connection/EndPointWithSerialization.java index 4da3c8e2..ad34b30d 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/EndPointWithSerialization.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/EndPointWithSerialization.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; @@ -23,6 +24,7 @@ import dorkbox.network.connection.wrapper.ChannelWrapper; import dorkbox.network.pipeline.KryoEncoder; import dorkbox.network.pipeline.KryoEncoderCrypto; import dorkbox.network.rmi.RmiBridge; +import dorkbox.network.util.EndpointTool; import dorkbox.network.util.KryoSerializationManager; import dorkbox.network.util.SerializationManager; import dorkbox.network.util.exceptions.InitializationException; @@ -228,6 +230,8 @@ public abstract class EndPointWithSerialization extends EndPoint { /** * Closes all connections ONLY (keeps the server/client running) + *

+ * This is used, for example, when reconnecting to a server. The server should ALWAYS use STOP. */ @Override public void close() { @@ -241,7 +245,60 @@ public abstract class EndPointWithSerialization extends EndPoint { * Extra actions to perform when stopping this endpoint. */ @Override - protected void stopExtraActions() { + final void stopExtraActions() { this.connectionManager.stop(); } + + ConcurrentHashMap, EndpointTool> toolMap = new ConcurrentHashMap, EndpointTool>(); + + /** + * Registers a tool with the server, to be used by other services. + */ + public void registerTool(EndpointTool toolClass) { + if (toolClass == null) { + throw new IllegalArgumentException("Tool must not be null! Unable to add tool"); + } + + Class[] interfaces = toolClass.getClass().getInterfaces(); + int length = interfaces.length; + int index = -1; + + if (length > 1) { + Class clazz2; + Class cls = EndpointTool.class; + + for (int i=0;i clazz = interfaces[index]; + EndpointTool put = this.toolMap.put(clazz, toolClass); + if (put != null) { + throw new IllegalArgumentException("Tool must be unique! Unable to add tool"); + } + } + + /** + * Only get the tools in the ModuleStart (ie: load) methods. If done in the constructor, the tool might not be available yet + */ + public T getTool(Class toolClass) { + if (toolClass == null) { + throw new IllegalArgumentException("Tool must not be null! Unable to add tool"); + } + + @SuppressWarnings("unchecked") + T tool = (T) this.toolMap.get(toolClass); + return tool; + } } diff --git a/Dorkbox-Network/src/dorkbox/network/util/EndpointTool.java b/Dorkbox-Network/src/dorkbox/network/util/EndpointTool.java new file mode 100644 index 00000000..eab35dfd --- /dev/null +++ b/Dorkbox-Network/src/dorkbox/network/util/EndpointTool.java @@ -0,0 +1,5 @@ +package dorkbox.network.util; + +public interface EndpointTool { + +}