All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.caesarealabs.rpc4k.runtime.api.RpcServerSetup.kt Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
@file:Suppress("UNCHECKED_CAST")

package com.caesarealabs.rpc4k.runtime.api

import com.caesarealabs.rpc4k.runtime.api.components.MemoryEventManager
import com.caesarealabs.rpc4k.runtime.implementation.routeRpcCallImpl


//public typealias AnyRpcServerSetup = RpcServerSetup<*, *, *>
//public typealias RpcSetupOf = RpcServerSetup
//public typealias RpcSetupEngine = RpcServerSetup<*, E, *>

//public interface WithFormat {
//}

/**
 * All configuration a generated RPC responding router needs to function.
 *
 * A _handler_ is the user class annotated with @Api.
 *
 * Needs to be an interface to allow the invoker to access the handler
 */
public interface HandlerConfig {
    public val handler: T
    public val format: SerializationFormat
    public val eventManager: EventManager
    public val engine: RpcServerEngine

    public object InMemory : HandlerConfig {
        override val handler: Nothing get() = error("No actual handler is used as everything is done in-memory")
        override val format: SerializationFormat get() = error("No serialization is used as everything is done in-memory")
        override val eventManager: EventManager = MemoryEventManager()
        override val engine: RpcServerEngine get() = error("No Server Engine is used as everything is done in-memory")
    }
}

public data class ServerConfig(val router: RpcRouter<*>, private val config: HandlerConfig<*>) : HandlerConfig by config


/**
 * Small hack to get the handler and invoker to reference each other.
 */
internal class HandlerConfigImpl(
    handler: (I) -> T,
    invoker: (HandlerConfigImpl) -> I,
    override val format: SerializationFormat,
    override val eventManager: EventManager,
    override val engine: RpcServerEngine
) : HandlerConfig {
    public val invoker: I = invoker(this)
    override val handler: T = handler(this.invoker)
}




public data class Rpc4kSCServerSuite(
    val engine: E,
    val config: ServerConfig,
    val server: S,
    val invoker: Inv
)

public typealias Rpc4kWithEngine = Rpc4kSCServerSuite<*, *, E>


public suspend fun > Rpc4kWithEngine.routeRpcs(input: I): O {
    return engine.routeRpcCallImpl(input, null, config)!!
}

public suspend fun > Rpc4kWithEngine.routeRpcs(input: I, output: O) {
    engine.routeRpcCallImpl(input, output, config)
}

public fun  Rpc4kWithEngine.start(wait: Boolean) {
    engine.start(wait)
}

public fun  Rpc4kWithEngine.stop() {
    engine.stop()
}



//internal
//    public fun create(handler: (HandlerConfig) -> T,
//                         format: SerializationFormat,
//                         eventManager: EventManager<*>) {
//        return
//    }
//}

//public class RpcServerSetup(
////    internal val handlerProvider: (Invoker) -> T,
//    public val handler: T,
//    public val generated: Rpc4kIndex,
////    public val generatedHelper: GeneratedServerHelper,
//    public val engine: Engine,
//    public val format: SerializationFormat = JsonFormat(),
////    invokerProvider: (RpcServerSetup) -> GeneratedEventInvoker
////    val eventHelper: GeneratedEventHelper? = null
//) {
//    public fun  withEngine(engine: New): RpcServerSetup {
//        return RpcServerSetup(handlerProvider, generated, engine, format)
//    }
//
////    internal constructor(
////        handler: T,
////        generatedHelper: GeneratedServerHelper,
////        engine: Engine,
////        format: SerializationFormat = JsonFormat()
////    ) : this(handlerProvider = { handler }, generatedHelper, engine, format)
//
////    public companion object {
////        // i want to get rid of this, not a very great api?
////        public fun  managedKtor(
////            handler: (Invoker) -> T, generated: Rpc4kIndex, format: SerializationFormat = JsonFormat(),
////            ktorServer: KtorManagedRpcServer = KtorManagedRpcServer()
////        ): RpcServerSetup {
////            return RpcServerSetup(handler, generated, ktorServer, format)
////        }
////    }
//
////    public val invoker: Invoker = generatedHelper.createInvoker(this)
////    public val handler: T = handlerProvider(invoker)
//}

//public fun  RpcServerSetup.withEngine(
//    engine: NewEngine
//): RpcServerSetup =
//    RpcServerSetup(handler, generatedHelper, engine, format)


//public fun  RpcSetupEngine.createServer(): RpcServerEngine.MultiCall.Instance = engine.create(this)
//public fun  RpcSetupEngine.startServer(wait: Boolean): Unit = engine.create(this)
//    .start(wait)






© 2015 - 2024 Weber Informatics LLC | Privacy Policy