better naming & interface for 3.1
This commit is contained in:
		
							parent
							
								
									b35ca92e48
								
							
						
					
					
						commit
						ceb6ea53db
					
				@ -0,0 +1,15 @@
 | 
			
		||||
package net.sergeych.parsec3
 | 
			
		||||
 | 
			
		||||
import kotlinx.coroutines.flow.StateFlow
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Transport level protocol (conforms to parsec 3.0). It is not specially secured could be used as is, as it provides bidirectional
 | 
			
		||||
 * asynchronous (push capable) calls, but normally it should be used as a transport for parsec 3.1 secure
 | 
			
		||||
 * protocol which uses this transport interface to run.
 | 
			
		||||
 */
 | 
			
		||||
interface Parsec3Transport<S> {
 | 
			
		||||
    val connectedFlow: StateFlow<Boolean>
 | 
			
		||||
    fun close()
 | 
			
		||||
 | 
			
		||||
    suspend fun adapter(): Adapter<S>
 | 
			
		||||
}
 | 
			
		||||
@ -16,27 +16,27 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
 | 
			
		||||
    val api: H,
 | 
			
		||||
    val exceptionsRegistry: ExceptionsRegistry = ExceptionsRegistry(),
 | 
			
		||||
    f: AdapterBuilder<S, H>.() -> Unit,
 | 
			
		||||
) : LogTag("P3WSC") {
 | 
			
		||||
) : LogTag("P3WSC"), Parsec3Transport<S> {
 | 
			
		||||
 | 
			
		||||
    val builder = AdapterBuilder(api, exceptionsRegistry, f)
 | 
			
		||||
 | 
			
		||||
    private val _connectionFlow = MutableStateFlow(false)
 | 
			
		||||
    private val closeFlow = MutableStateFlow(false)
 | 
			
		||||
    val connectedFlow: StateFlow<Boolean> = _connectionFlow
 | 
			
		||||
    override val connectedFlow: StateFlow<Boolean> = _connectionFlow
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    init {
 | 
			
		||||
        start()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun close() {
 | 
			
		||||
    override fun close() {
 | 
			
		||||
        if( closeFlow.value == false ) closeFlow.value = true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var deferredAdapter = CompletableDeferred<Adapter<S>>()
 | 
			
		||||
    private set
 | 
			
		||||
 | 
			
		||||
    suspend fun adapter(): Adapter<S> = deferredAdapter.await()
 | 
			
		||||
    override suspend fun adapter(): Adapter<S> = deferredAdapter.await()
 | 
			
		||||
 | 
			
		||||
    fun start() {
 | 
			
		||||
        globalLaunch {
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ import java.time.Duration
 | 
			
		||||
import java.util.concurrent.atomic.AtomicInteger
 | 
			
		||||
import java.util.concurrent.atomic.AtomicLong
 | 
			
		||||
 | 
			
		||||
fun <S, H : CommandHost<S>>Application.parsec3Server(
 | 
			
		||||
fun <S, H : CommandHost<S>>Application.parsec3TransportServer(
 | 
			
		||||
    api: H,
 | 
			
		||||
    exceptionsRegistry: ExceptionsRegistry = ExceptionsRegistry(),
 | 
			
		||||
    path: String = "/api/p3",
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,7 @@ internal class WsServerKtTest {
 | 
			
		||||
    fun testWsServer() {
 | 
			
		||||
 | 
			
		||||
        embeddedServer(Netty, port = 8080) {
 | 
			
		||||
            parsec3Server(
 | 
			
		||||
            parsec3TransportServer(
 | 
			
		||||
                TestApiServer,
 | 
			
		||||
            ) {
 | 
			
		||||
                newSession { TestSession() }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user