extending functionality for kilogin

This commit is contained in:
Sergey Chernov 2024-11-19 12:12:41 +07:00
parent 93ab8ddf91
commit 1032eebbbe
6 changed files with 30 additions and 7 deletions

View File

@ -6,7 +6,6 @@ import net.sergeych.bintools.toDataSource
import net.sergeych.bipack.BipackDecoder
import net.sergeych.bipack.BipackEncoder
import net.sergeych.kiloparsec.Command.Call
import net.sergeych.kiloparsec.Command.Companion.unpackCall
import net.sergeych.utools.unpack
/**
@ -27,7 +26,7 @@ import net.sergeych.utools.unpack
* [unpackResult].
*
*/
class Command<A, R>(
open class Command<A, R>(
val name: String,
val argsSerializer: KSerializer<A>,
val resultSerializer: KSerializer<R>

View File

@ -19,7 +19,7 @@ open class KiloInterface<S> : LocalInterface<KiloScope<S>>() {
fun onConnected(f: KiloScope<S>.()->Unit) { onConnectHandler = f }
init {
registerError { RemoteInterface.UnknownCommand() }
registerError { RemoteInterface.UnknownCommand(it) }
registerError { RemoteInterface.InternalError(it) }
registerError { RemoteInterface.ClosedException(it) }
// registerError { RemoteInterface.SecurityException(it) }

View File

@ -67,7 +67,7 @@ open class LocalInterface<S> : Loggable by LogTag("LocalInterface${idCounter.inc
name: String,
packedArgs: UByteArray,
): UByteArray =
(commands[name] ?: throw RemoteInterface.UnknownCommand())
(commands[name] ?: throw RemoteInterface.UnknownCommand(name))
.invoke(scope, packedArgs)

View File

@ -40,7 +40,7 @@ interface RemoteInterface {
/**
* Command is not supported by the remote party
*/
class UnknownCommand : RemoteException("UnknownCommand")
class UnknownCommand(commandName: String) : RemoteException("UnknownCommand: $commandName")
open class InternalError(code: String="0"): RemoteException("Internal error: $code")

View File

@ -18,6 +18,27 @@ import net.sergeych.mp_logger.*
import net.sergeych.tools.AtomicCounter
import java.time.Duration
/**
* Create a ktor-based websocket server.
* This call install Routing and WebSockets with proper configuration.
*
* The course of action is:
*
* - create LocalInterface and populate it with functionality
* - call this method with localInterface
* - optionally, connect the same interface with TCP or UDP providers on supported platforms,
* in which case it might be useful to hae session creating function [createSession] separate.
*
* _Note_: [KiloInterface] as for now does not contain session creation in it as we suggest
* session could be transport specific.
*
* @param localInterface where the actual work is performed.
* @param timeout how long to wait for the connection to be established.
* @param path default http path to the websocket.
* @param serverKey optional key to authenticate the connection. If the client specify expected
* server key it should match of connection will not be established.
* @param createSession function to create a server session.
*/
fun <S> Application.setupWebsocketServer(
localInterface: KiloInterface<S>,
path: String = "/kp",

View File

@ -10,7 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
class InternetrTest {
class InternetTest {
class TestException : Exception("test1")
@Test
@ -46,8 +46,11 @@ class InternetrTest {
Session("unknown")
}
val client = KiloClient<Unit>() {
data class LocalSession(val localFoo: String)
val client = KiloClient {
addErrors(cli)
session { LocalSession("unknown") }
// TODO: add register error variant
connect { connectTcpDevice("localhost:$port") }
}