From 8ae9b9ddbb0081c9c65f9893e754898380fd7d70 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 28 Aug 2020 10:21:10 +0200 Subject: [PATCH] Code cleanup --- src/dorkbox/network/Client.kt | 20 +++++---- src/dorkbox/network/Configuration.kt | 2 +- src/dorkbox/network/Server.kt | 8 ++-- src/dorkbox/network/connection/Connection.kt | 41 +++++++------------ src/dorkbox/network/connection/EndPoint.kt | 5 +-- .../rmi/RmiDelayedInvocationSpamTest.kt | 6 ++- .../network/rmi/RmiDelayedInvocationTest.kt | 6 ++- 7 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/dorkbox/network/Client.kt b/src/dorkbox/network/Client.kt index 5df62a15..0b31379e 100644 --- a/src/dorkbox/network/Client.kt +++ b/src/dorkbox/network/Client.kt @@ -72,7 +72,7 @@ open class Client(config: Configuration = Configuration private val previousClosedConnectionActivity: Long = 0 - private val rmiConnectionSupport = RmiManagerConnections(logger, rmiGlobalSupport, serialization) + private val rmiConnectionSupport = RmiManagerConnections(logger, listenerManager, rmiGlobalSupport, serialization) init { // have to do some basic validation of our configuration @@ -498,7 +498,7 @@ open class Client(config: Configuration = Configuration * * @see RemoteObject */ - fun saveObject(`object`: Any): Int { + suspend fun saveObject(`object`: Any): Int { return rmiConnectionSupport.saveImplObject(`object`) } @@ -519,7 +519,7 @@ open class Client(config: Configuration = Configuration * * @see RemoteObject */ - fun saveObject(`object`: Any, objectId: Int): Boolean { + suspend fun saveObject(`object`: Any, objectId: Int): Boolean { return rmiConnectionSupport.saveImplObject(`object`, objectId) } @@ -543,8 +543,10 @@ open class Client(config: Configuration = Configuration inline fun getObject(objectId: Int): Iface { // NOTE: It's not possible to have reified inside a virtual function // https://stackoverflow.com/questions/60037849/kotlin-reified-generic-in-virtual-function + val kryoId = serialization.getKryoIdForRmiClient(Iface::class.java) + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") - return rmiConnectionSupport.getRemoteObject(getConnection(), objectId, Iface::class.java) + return rmiConnectionSupport.getRemoteObject(getConnection(), kryoId, objectId, Iface::class.java) } /** @@ -568,7 +570,7 @@ open class Client(config: Configuration = Configuration suspend inline fun createObject(vararg objectParameters: Any?, noinline callback: suspend (Int, Iface) -> Unit) { // NOTE: It's not possible to have reified inside a virtual function // https://stackoverflow.com/questions/60037849/kotlin-reified-generic-in-virtual-function - val kryoId = serialization.getKryoIdForRmi(Iface::class.java) + val kryoId = serialization.getKryoIdForRmiClient(Iface::class.java) @Suppress("UNCHECKED_CAST") objectParameters as Array @@ -598,10 +600,10 @@ open class Client(config: Configuration = Configuration suspend inline fun createObject(noinline callback: suspend (Int, Iface) -> Unit) { // NOTE: It's not possible to have reified inside a virtual function // https://stackoverflow.com/questions/60037849/kotlin-reified-generic-in-virtual-function - val classId = serialization.getKryoIdForRmi(Iface::class.java) + val kryoId = serialization.getKryoIdForRmiClient(Iface::class.java) @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") - rmiConnectionSupport.createRemoteObject(getConnection(), classId, null, callback) + rmiConnectionSupport.createRemoteObject(getConnection(), kryoId, null, callback) } // @@ -628,7 +630,7 @@ open class Client(config: Configuration = Configuration * * @see RemoteObject */ - fun saveGlobalObject(`object`: Any): Int { + suspend fun saveGlobalObject(`object`: Any): Int { return rmiGlobalSupport.saveImplObject(`object`) } @@ -649,7 +651,7 @@ open class Client(config: Configuration = Configuration * * @see RemoteObject */ - fun saveGlobalObject(`object`: Any, objectId: Int): Boolean { + suspend fun saveGlobalObject(`object`: Any, objectId: Int): Boolean { return rmiGlobalSupport.saveImplObject(`object`, objectId) } diff --git a/src/dorkbox/network/Configuration.kt b/src/dorkbox/network/Configuration.kt index 53cf1506..9405818f 100644 --- a/src/dorkbox/network/Configuration.kt +++ b/src/dorkbox/network/Configuration.kt @@ -95,7 +95,7 @@ open class Configuration { /** * Specify the serialization manager to use. */ - var serialization: Serialization = Serialization.DEFAULT() + var serialization: Serialization = Serialization() /** * The idle strategy used when polling the Media Driver for new messages. BackOffIdleStrategy is the DEFAULT. diff --git a/src/dorkbox/network/Server.kt b/src/dorkbox/network/Server.kt index 8a60bd09..6df17fc4 100644 --- a/src/dorkbox/network/Server.kt +++ b/src/dorkbox/network/Server.kt @@ -530,8 +530,8 @@ open class Server(config: ServerConfiguration = ServerC * * @see RemoteObject */ - fun saveGlobalObject(`object`: Any): Int { - return rmiGlobalSupport.saveImplObject(logger, `object`) + suspend fun saveGlobalObject(`object`: Any): Int { + return rmiGlobalSupport.saveImplObject(`object`) } /** @@ -551,7 +551,7 @@ open class Server(config: ServerConfiguration = ServerC * * @see RemoteObject */ - fun saveGlobalObject(`object`: Any, objectId: Int): Boolean { - return rmiGlobalSupport.saveImplObject(logger, `object`, objectId) + suspend fun saveGlobalObject(`object`: Any, objectId: Int): Boolean { + return rmiGlobalSupport.saveImplObject(`object`, objectId) } } diff --git a/src/dorkbox/network/connection/Connection.kt b/src/dorkbox/network/connection/Connection.kt index 27a74c60..2d0879ac 100644 --- a/src/dorkbox/network/connection/Connection.kt +++ b/src/dorkbox/network/connection/Connection.kt @@ -97,7 +97,10 @@ open class Connection(connectionParameters: ConnectionParams<*>) { return pingFuture2?.response ?: -1 } - private val endPoint = connectionParameters.endPoint + /** + * the endpoint associated with this connection + */ + internal val endPoint = connectionParameters.endPoint private val listenerManager = atomic?>(null) val logger = endPoint.logger @@ -158,13 +161,6 @@ open class Connection(connectionParameters: ConnectionParams<*>) { return remoteKeyChanged } - /** - * @return the endpoint associated with this connection - */ - internal fun endPoint(): EndPoint<*> { - return endPoint - } - // /** // * This is the per-message sequence number. // * @@ -442,13 +438,8 @@ open class Connection(connectionParameters: ConnectionParams<*>) { * * @see RemoteObject */ - fun saveObject(`object`: Any): Int { - val rmiId = rmiConnectionSupport.saveImplObject(`object`) - if (rmiId == RemoteObjectStorage.INVALID_RMI) { - logger.error("Invalid RMI ID for saving object: $`object`") - } - - return rmiId + suspend fun saveObject(`object`: Any): Int { + return rmiConnectionSupport.saveImplObject(`object`) } /** @@ -468,13 +459,8 @@ open class Connection(connectionParameters: ConnectionParams<*>) { * * @see RemoteObject */ - fun saveObject(`object`: Any, objectId: Int): Boolean { - val success = rmiConnectionSupport.saveImplObject(`object`, objectId) - if (!success) { - logger.error("Unable to save object $`object` with RMI ID $objectId") - } - - return success + suspend fun saveObject(`object`: Any, objectId: Int): Boolean { + return rmiConnectionSupport.saveImplObject(`object`, objectId) } /** @@ -498,7 +484,10 @@ open class Connection(connectionParameters: ConnectionParams<*>) { // NOTE: It's not possible to have reified inside a virtual function // https://stackoverflow.com/questions/60037849/kotlin-reified-generic-in-virtual-function @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") - return rmiConnectionSupport.getRemoteObject(this, objectId, Iface::class.java) + val kryoId = endPoint.serialization.getKryoIdForRmiClient(Iface::class.java) + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + return rmiConnectionSupport.getRemoteObject(this, kryoId, objectId, Iface::class.java) } /** @@ -544,7 +533,7 @@ open class Connection(connectionParameters: ConnectionParams<*>) { */ suspend fun createObject(vararg objectParameters: Any?, callback: suspend (Int, Iface) -> Unit) { val iFaceClass = ClassHelper.getGenericParameterAsClassForSuperClass(Function2::class.java, callback.javaClass, 1) - val kryoId = endPoint.serialization.getKryoIdForRmi(iFaceClass) + val kryoId = endPoint.serialization.getKryoIdForRmiClient(iFaceClass) @Suppress("UNCHECKED_CAST") objectParameters as Array @@ -571,8 +560,8 @@ open class Connection(connectionParameters: ConnectionParams<*>) { */ suspend fun createObject(callback: suspend (Int, Iface) -> Unit) { val iFaceClass = ClassHelper.getGenericParameterAsClassForSuperClass(Function2::class.java, callback.javaClass, 1) - val interfaceClassId = endPoint.serialization.getKryoIdForRmi(iFaceClass) + val kryoId = endPoint.serialization.getKryoIdForRmiClient(iFaceClass) - rmiConnectionSupport.createRemoteObject(this, interfaceClassId, null, callback) + rmiConnectionSupport.createRemoteObject(this, kryoId, null, callback) } } diff --git a/src/dorkbox/network/connection/EndPoint.kt b/src/dorkbox/network/connection/EndPoint.kt index a24e04ae..bc1c8741 100644 --- a/src/dorkbox/network/connection/EndPoint.kt +++ b/src/dorkbox/network/connection/EndPoint.kt @@ -141,7 +141,7 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A // we only want one instance of these created. These will be called appropriately val settingsStore: SettingsStore - internal val rmiGlobalSupport = RmiManagerGlobal(logger, actionDispatch, config.serialization) + internal val rmiGlobalSupport = RmiManagerGlobal(logger, listenerManager, actionDispatch, config.serialization) init { logger.error("NETWORK STACK IS ONLY IPV4 AT THE MOMENT. IPV6 is in progress!") @@ -239,7 +239,6 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A crypto = CryptoManagement(logger, settingsStore, type, config) - // we are done with initial configuration, now finish serialization runBlocking { serialization.finishInit(type) @@ -336,7 +335,7 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A * from a "global" context */ internal open fun getRmiConnectionSupport() : RmiManagerConnections { - return RmiManagerConnections(logger, rmiGlobalSupport, serialization) + return RmiManagerConnections(logger, listenerManager, rmiGlobalSupport, serialization) } /** diff --git a/test/dorkboxTest/network/rmi/RmiDelayedInvocationSpamTest.kt b/test/dorkboxTest/network/rmi/RmiDelayedInvocationSpamTest.kt index 413fc4b4..9f5bb62b 100644 --- a/test/dorkboxTest/network/rmi/RmiDelayedInvocationSpamTest.kt +++ b/test/dorkboxTest/network/rmi/RmiDelayedInvocationSpamTest.kt @@ -37,7 +37,9 @@ class RmiDelayedInvocationSpamTest : BaseTest() { @Test @Throws(SecurityException::class, IOException::class) fun rmiNetwork() { - rmi() + runBlocking { + rmi() + } } @Test @@ -58,7 +60,7 @@ class RmiDelayedInvocationSpamTest : BaseTest() { * In this test the server has two objects in an object space. The client * uses the first remote object to get the second remote object. */ - fun rmi(config: (Configuration) -> Unit = {}) { + suspend fun rmi(config: (Configuration) -> Unit = {}) { val server: Server val async = false diff --git a/test/dorkboxTest/network/rmi/RmiDelayedInvocationTest.kt b/test/dorkboxTest/network/rmi/RmiDelayedInvocationTest.kt index b815b983..6c3734a3 100644 --- a/test/dorkboxTest/network/rmi/RmiDelayedInvocationTest.kt +++ b/test/dorkboxTest/network/rmi/RmiDelayedInvocationTest.kt @@ -34,7 +34,9 @@ class RmiDelayedInvocationTest : BaseTest() { @Test @Throws(SecurityException::class, IOException::class) fun rmiNetwork() { - rmi() + runBlocking { + rmi() + } } @Test @@ -55,7 +57,7 @@ class RmiDelayedInvocationTest : BaseTest() { * In this test the server has two objects in an object space. The client * uses the first remote object to get the second remote object. */ - fun rmi(config: (Configuration) -> Unit = {}) { + suspend fun rmi(config: (Configuration) -> Unit = {}) { run { val configuration = serverConfig() config(configuration)