added method to import errors into client interface KiloClient.Builder#addErrors
This commit is contained in:
parent
ae624ee051
commit
38d800c7ac
@ -112,6 +112,10 @@ class KiloClient<S>(
|
|||||||
interfaceBuilder = f
|
interfaceBuilder = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addErrors(from: LocalInterface<*>) {
|
||||||
|
errorProviders += from
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new session object, otherwise Unit session will be used
|
* Create a new session object, otherwise Unit session will be used
|
||||||
*/
|
*/
|
||||||
@ -123,8 +127,11 @@ class KiloClient<S>(
|
|||||||
connectionBuilder = f
|
connectionBuilder = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val errorProviders = mutableListOf<LocalInterface<*>>()
|
||||||
|
|
||||||
internal fun build(): KiloClient<S> {
|
internal fun build(): KiloClient<S> {
|
||||||
val i = KiloInterface<S>()
|
val i = KiloInterface<S>()
|
||||||
|
for(ep in errorProviders) i.addErrorProvider(ep)
|
||||||
interfaceBuilder?.let { i.it() }
|
interfaceBuilder?.let { i.it() }
|
||||||
val connector = connectionBuilder ?: throw IllegalArgumentException("connect handler was not set")
|
val connector = connectionBuilder ?: throw IllegalArgumentException("connect handler was not set")
|
||||||
return KiloClient(i,secretIdKey) {
|
return KiloClient(i,secretIdKey) {
|
||||||
|
@ -11,7 +11,7 @@ private typealias RawCommandHandler<C> = suspend (C, UByteArray) -> UByteArray
|
|||||||
|
|
||||||
private val idCounter = AtomicCounter()
|
private val idCounter = AtomicCounter()
|
||||||
|
|
||||||
open class LocalInterface<S>: Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") {
|
open class LocalInterface<S> : Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") {
|
||||||
|
|
||||||
private val commands = mutableMapOf<String, RawCommandHandler<S>>()
|
private val commands = mutableMapOf<String, RawCommandHandler<S>>()
|
||||||
|
|
||||||
|
@ -13,10 +13,13 @@ import net.sergeych.mp_logger.Log
|
|||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertIs
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class ClientTest {
|
class ClientTest {
|
||||||
|
|
||||||
|
class TestException : Exception("test1")
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testAddresses() {
|
fun testAddresses() {
|
||||||
println(InetAddress.getLocalHost())
|
println(InetAddress.getLocalHost())
|
||||||
@ -34,16 +37,30 @@ class ClientTest {
|
|||||||
|
|
||||||
val cmdSave by command<String,Unit>()
|
val cmdSave by command<String,Unit>()
|
||||||
val cmdLoad by command<Unit,String>()
|
val cmdLoad by command<Unit,String>()
|
||||||
|
val cmdDrop by command<Unit,Unit>()
|
||||||
|
val cmdException by command<Unit,Unit>()
|
||||||
|
|
||||||
val cli = KiloInterface<Session>().apply {
|
val cli = KiloInterface<Session>().apply {
|
||||||
|
registerError { TestException() }
|
||||||
onConnected { session.data = "start" }
|
onConnected { session.data = "start" }
|
||||||
on(cmdSave) { session.data = it }
|
on(cmdSave) { session.data = it }
|
||||||
on(cmdLoad) {
|
on(cmdLoad) {
|
||||||
println("load!")
|
println("load!")
|
||||||
session.data }
|
session.data
|
||||||
}
|
}
|
||||||
val server = KiloServer(cli, acceptTcpDevice(17101)) { Session("unknown")}
|
on(cmdException) {
|
||||||
val client = KiloClient<Unit> {
|
throw TestException()
|
||||||
|
}
|
||||||
|
on(cmdDrop) {
|
||||||
|
throw RemoteInterface.ClosedException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val server = KiloServer(cli, acceptTcpDevice(17101)) {
|
||||||
|
Session("unknown")
|
||||||
|
}
|
||||||
|
|
||||||
|
val client = KiloClient<Unit>() {
|
||||||
|
addErrors(cli)
|
||||||
connect { connectTcpDevice("localhost:17101") }
|
connect { connectTcpDevice("localhost:17101") }
|
||||||
}
|
}
|
||||||
println(client.call(cmdLoad))
|
println(client.call(cmdLoad))
|
||||||
@ -51,6 +68,13 @@ class ClientTest {
|
|||||||
assertEquals("start", 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))
|
||||||
|
|
||||||
|
// client.call(cmdException)
|
||||||
|
val res = kotlin.runCatching { client.call(cmdException) }
|
||||||
|
println(res.exceptionOrNull())
|
||||||
|
assertIs<TestException>(res.exceptionOrNull())
|
||||||
|
assertEquals("foobar", client.call(cmdLoad))
|
||||||
|
|
||||||
server.close()
|
server.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user