From e7abbe6d1d5b6a55bf5574d0f0f9a0541754647b Mon Sep 17 00:00:00 2001 From: sergeych Date: Fri, 17 Nov 2023 17:43:38 +0300 Subject: [PATCH] KiloSession.onConnected handler added --- .../kotlin/net/sergeych/kiloparsec/KiloClientConnection.kt | 3 ++- .../kotlin/net/sergeych/kiloparsec/KiloInterface.kt | 5 +++++ .../kotlin/net/sergeych/kiloparsec/KiloServerConnection.kt | 1 + .../net/sergeych/kiloparsec/adapter/NetworkProvider.kt | 1 - .../net/sergeych/kiloparsec/adapter/NetworkProvider.jvm.kt | 1 + src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt | 3 ++- 6 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClientConnection.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClientConnection.kt index 64fa898..ad4cf83 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClientConnection.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClientConnection.kt @@ -12,7 +12,7 @@ import net.sergeych.utools.pack private var clientIds = 0 class KiloClientConnection( - private val clientInterface: LocalInterface>, + private val clientInterface: KiloInterface, private val device: Transport.Device, private val session: S, private val secretIdKey: Key.Signing? = null, @@ -81,6 +81,7 @@ class KiloClientConnection( kiloRemoteInterface.complete( KiloRemoteInterface(deferredParams, clientInterface) ) + clientInterface.onConnectHandler?.invoke(params.scope) onConnectedStateChanged?.invoke(true) job.join() diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloInterface.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloInterface.kt index 6b58cec..1e915ff 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloInterface.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloInterface.kt @@ -7,6 +7,11 @@ package net.sergeych.kiloparsec * BAse implementation registers relevant exceptions. */ class KiloInterface : LocalInterface>() { + + internal var onConnectHandler: (KiloScope.()->Unit) ? = null + + fun onConnected(f: KiloScope.()->Unit) { onConnectHandler = f } + init { registerError { RemoteInterface.UnknownCommand() } registerError { RemoteInterface.ClosedException(it) } diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloServerConnection.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloServerConnection.kt index ee634a2..b1e0056 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloServerConnection.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloServerConnection.kt @@ -83,6 +83,7 @@ class KiloServerConnection( kiloRemoteInterface.complete( KiloRemoteInterface(deferredParams, clientInterface) ) + clientInterface.onConnectHandler?.invoke(p.scope) } } diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.kt index 50af716..fb8fb68 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.kt @@ -49,7 +49,6 @@ fun String.toNetworkAddress() : NetworkAddress { return NetworkAddress(host, port.toInt()) } - expect fun acceptTcpDevice(port: Int): Flow expect suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice \ No newline at end of file diff --git a/src/jvmMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.jvm.kt b/src/jvmMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.jvm.kt index 3e165f9..186e3dc 100644 --- a/src/jvmMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.jvm.kt +++ b/src/jvmMain/kotlin/net/sergeych/kiloparsec/adapter/NetworkProvider.jvm.kt @@ -34,6 +34,7 @@ actual fun acceptTcpDevice(port: Int): Flow { } suspend fun connectTcpDevice(address: String) = connectTcpDevice(address.toNetworkAddress()) +@Suppress("unused") suspend fun connectTcpDevice(host: String, port: Int) = connectTcpDevice(NetworkAddress(host,port)) actual suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice { address as JvmNetworkAddress diff --git a/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt b/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt index 85a846c..058bb92 100644 --- a/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt +++ b/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt @@ -30,6 +30,7 @@ class ClientTest { val cmdLoad by command() val cli = KiloInterface().apply { + onConnected { session.data = "start" } on(cmdSave) { session.data = it } on(cmdLoad) { println("load!") @@ -41,7 +42,7 @@ class ClientTest { } println(client.call(cmdLoad)) - assertEquals("unknown", client.call(cmdLoad)) + assertEquals("start", client.call(cmdLoad)) client.call(cmdSave, "foobar") assertEquals("foobar", client.call(cmdLoad)) server.close()