KiloSession.onConnected handler added
This commit is contained in:
parent
c6ac6f5907
commit
e7abbe6d1d
@ -12,7 +12,7 @@ import net.sergeych.utools.pack
|
|||||||
private var clientIds = 0
|
private var clientIds = 0
|
||||||
|
|
||||||
class KiloClientConnection<S>(
|
class KiloClientConnection<S>(
|
||||||
private val clientInterface: LocalInterface<KiloScope<S>>,
|
private val clientInterface: KiloInterface<S>,
|
||||||
private val device: Transport.Device,
|
private val device: Transport.Device,
|
||||||
private val session: S,
|
private val session: S,
|
||||||
private val secretIdKey: Key.Signing? = null,
|
private val secretIdKey: Key.Signing? = null,
|
||||||
@ -81,6 +81,7 @@ class KiloClientConnection<S>(
|
|||||||
kiloRemoteInterface.complete(
|
kiloRemoteInterface.complete(
|
||||||
KiloRemoteInterface(deferredParams, clientInterface)
|
KiloRemoteInterface(deferredParams, clientInterface)
|
||||||
)
|
)
|
||||||
|
clientInterface.onConnectHandler?.invoke(params.scope)
|
||||||
onConnectedStateChanged?.invoke(true)
|
onConnectedStateChanged?.invoke(true)
|
||||||
job.join()
|
job.join()
|
||||||
|
|
||||||
|
@ -7,6 +7,11 @@ package net.sergeych.kiloparsec
|
|||||||
* BAse implementation registers relevant exceptions.
|
* BAse implementation registers relevant exceptions.
|
||||||
*/
|
*/
|
||||||
class KiloInterface<S> : LocalInterface<KiloScope<S>>() {
|
class KiloInterface<S> : LocalInterface<KiloScope<S>>() {
|
||||||
|
|
||||||
|
internal var onConnectHandler: (KiloScope<S>.()->Unit) ? = null
|
||||||
|
|
||||||
|
fun onConnected(f: KiloScope<S>.()->Unit) { onConnectHandler = f }
|
||||||
|
|
||||||
init {
|
init {
|
||||||
registerError { RemoteInterface.UnknownCommand() }
|
registerError { RemoteInterface.UnknownCommand() }
|
||||||
registerError { RemoteInterface.ClosedException(it) }
|
registerError { RemoteInterface.ClosedException(it) }
|
||||||
|
@ -83,6 +83,7 @@ class KiloServerConnection<S>(
|
|||||||
kiloRemoteInterface.complete(
|
kiloRemoteInterface.complete(
|
||||||
KiloRemoteInterface(deferredParams, clientInterface)
|
KiloRemoteInterface(deferredParams, clientInterface)
|
||||||
)
|
)
|
||||||
|
clientInterface.onConnectHandler?.invoke(p.scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,6 @@ fun String.toNetworkAddress() : NetworkAddress {
|
|||||||
return NetworkAddress(host, port.toInt())
|
return NetworkAddress(host, port.toInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
expect fun acceptTcpDevice(port: Int): Flow<InetTransportDevice>
|
expect fun acceptTcpDevice(port: Int): Flow<InetTransportDevice>
|
||||||
|
|
||||||
expect suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice
|
expect suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice
|
@ -34,6 +34,7 @@ actual fun acceptTcpDevice(port: Int): Flow<InetTransportDevice> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun connectTcpDevice(address: String) = connectTcpDevice(address.toNetworkAddress())
|
suspend fun connectTcpDevice(address: String) = connectTcpDevice(address.toNetworkAddress())
|
||||||
|
@Suppress("unused")
|
||||||
suspend fun connectTcpDevice(host: String, port: Int) = connectTcpDevice(NetworkAddress(host,port))
|
suspend fun connectTcpDevice(host: String, port: Int) = connectTcpDevice(NetworkAddress(host,port))
|
||||||
actual suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice {
|
actual suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice {
|
||||||
address as JvmNetworkAddress
|
address as JvmNetworkAddress
|
||||||
|
@ -30,6 +30,7 @@ class ClientTest {
|
|||||||
val cmdLoad by command<Unit,String>()
|
val cmdLoad by command<Unit,String>()
|
||||||
|
|
||||||
val cli = KiloInterface<Session>().apply {
|
val cli = KiloInterface<Session>().apply {
|
||||||
|
onConnected { session.data = "start" }
|
||||||
on(cmdSave) { session.data = it }
|
on(cmdSave) { session.data = it }
|
||||||
on(cmdLoad) {
|
on(cmdLoad) {
|
||||||
println("load!")
|
println("load!")
|
||||||
@ -41,7 +42,7 @@ class ClientTest {
|
|||||||
}
|
}
|
||||||
println(client.call(cmdLoad))
|
println(client.call(cmdLoad))
|
||||||
|
|
||||||
assertEquals("unknown", client.call(cmdLoad))
|
assertEquals("start", client.call(cmdLoad))
|
||||||
client.call(cmdSave, "foobar")
|
client.call(cmdSave, "foobar")
|
||||||
assertEquals("foobar", client.call(cmdLoad))
|
assertEquals("foobar", client.call(cmdLoad))
|
||||||
server.close()
|
server.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user