extending functionality for kilogin
This commit is contained in:
		
							parent
							
								
									93ab8ddf91
								
							
						
					
					
						commit
						1032eebbbe
					
				| @ -6,7 +6,6 @@ import net.sergeych.bintools.toDataSource | |||||||
| import net.sergeych.bipack.BipackDecoder | import net.sergeych.bipack.BipackDecoder | ||||||
| import net.sergeych.bipack.BipackEncoder | import net.sergeych.bipack.BipackEncoder | ||||||
| import net.sergeych.kiloparsec.Command.Call | import net.sergeych.kiloparsec.Command.Call | ||||||
| import net.sergeych.kiloparsec.Command.Companion.unpackCall |  | ||||||
| import net.sergeych.utools.unpack | import net.sergeych.utools.unpack | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -27,7 +26,7 @@ import net.sergeych.utools.unpack | |||||||
|  * [unpackResult]. |  * [unpackResult]. | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| class Command<A, R>( | open class Command<A, R>( | ||||||
|     val name: String, |     val name: String, | ||||||
|     val argsSerializer: KSerializer<A>, |     val argsSerializer: KSerializer<A>, | ||||||
|     val resultSerializer: KSerializer<R> |     val resultSerializer: KSerializer<R> | ||||||
|  | |||||||
| @ -19,7 +19,7 @@ open class KiloInterface<S> : LocalInterface<KiloScope<S>>() { | |||||||
|     fun onConnected(f: KiloScope<S>.()->Unit) { onConnectHandler = f } |     fun onConnected(f: KiloScope<S>.()->Unit) { onConnectHandler = f } | ||||||
| 
 | 
 | ||||||
|     init { |     init { | ||||||
|         registerError { RemoteInterface.UnknownCommand() } |         registerError { RemoteInterface.UnknownCommand(it) } | ||||||
|         registerError { RemoteInterface.InternalError(it) } |         registerError { RemoteInterface.InternalError(it) } | ||||||
|         registerError { RemoteInterface.ClosedException(it) } |         registerError { RemoteInterface.ClosedException(it) } | ||||||
| //        registerError { RemoteInterface.SecurityException(it) } | //        registerError { RemoteInterface.SecurityException(it) } | ||||||
|  | |||||||
| @ -67,7 +67,7 @@ open class LocalInterface<S> : Loggable by LogTag("LocalInterface${idCounter.inc | |||||||
|         name: String, |         name: String, | ||||||
|         packedArgs: UByteArray, |         packedArgs: UByteArray, | ||||||
|     ): UByteArray = |     ): UByteArray = | ||||||
|         (commands[name] ?: throw RemoteInterface.UnknownCommand()) |         (commands[name] ?: throw RemoteInterface.UnknownCommand(name)) | ||||||
|             .invoke(scope, packedArgs) |             .invoke(scope, packedArgs) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -40,7 +40,7 @@ interface RemoteInterface { | |||||||
|     /** |     /** | ||||||
|      * Command is not supported by the remote party |      * 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") |     open class InternalError(code: String="0"): RemoteException("Internal error: $code") | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -18,6 +18,27 @@ import net.sergeych.mp_logger.* | |||||||
| import net.sergeych.tools.AtomicCounter | import net.sergeych.tools.AtomicCounter | ||||||
| import java.time.Duration | 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( | fun <S> Application.setupWebsocketServer( | ||||||
|     localInterface: KiloInterface<S>, |     localInterface: KiloInterface<S>, | ||||||
|     path: String = "/kp", |     path: String = "/kp", | ||||||
|  | |||||||
| @ -10,7 +10,7 @@ import kotlin.test.Test | |||||||
| import kotlin.test.assertEquals | import kotlin.test.assertEquals | ||||||
| import kotlin.test.assertIs | import kotlin.test.assertIs | ||||||
| 
 | 
 | ||||||
| class InternetrTest { | class InternetTest { | ||||||
|     class TestException : Exception("test1") |     class TestException : Exception("test1") | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| @ -46,8 +46,11 @@ class InternetrTest { | |||||||
|             Session("unknown") |             Session("unknown") | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         val client = KiloClient<Unit>() { |         data class LocalSession(val localFoo: String) | ||||||
|  | 
 | ||||||
|  |         val client = KiloClient { | ||||||
|             addErrors(cli) |             addErrors(cli) | ||||||
|  |             session { LocalSession("unknown") } | ||||||
|             // TODO: add register error variant |             // TODO: add register error variant | ||||||
|             connect { connectTcpDevice("localhost:$port") } |             connect { connectTcpDevice("localhost:$port") } | ||||||
|         } |         } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user