Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Session joiner #1

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ repositories {

dependencies {
implementation("io.xconn:wampproto:0.1.0")
implementation("io.ktor:ktor-client-websockets:2.3.12")
implementation("io.ktor:ktor-client-cio:2.3.12")
testImplementation(kotlin("test"))
}

Expand Down
77 changes: 77 additions & 0 deletions src/main/kotlin/io/xconn/xconn/Types.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.xconn.xconn

import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
import io.ktor.websocket.close
import io.xconn.wampproto.SessionDetails
import io.xconn.wampproto.messages.Message
import io.xconn.wampproto.serializers.Serializer

interface IBaseSession {
fun id(): Long

fun realm(): String

fun authid(): String

fun authrole(): String

fun serializer(): Serializer

suspend fun send(data: Any)

suspend fun receive(): Any

suspend fun sendMessage(msg: Message)

suspend fun receiveMessage(): Message

suspend fun close()
}

class BaseSession(
private val webSocketSession: DefaultClientWebSocketSession,
private val sessionDetails: SessionDetails,
private val serializer: Serializer,
) : IBaseSession {
override fun id(): Long {
return sessionDetails.sessionID
}

override fun realm(): String {
return sessionDetails.realm
}

override fun authid(): String {
return sessionDetails.authid
}

override fun authrole(): String {
return sessionDetails.authrole
}

override fun serializer(): Serializer {
return serializer
}

override suspend fun send(data: Any) {
webSocketSession.sendFrame(data)
}

override suspend fun sendMessage(msg: Message) {
val serializedData = serializer.serialize(msg)
send(serializedData)
}

override suspend fun receive(): Any {
val frame = webSocketSession.incoming.receive()
return receiveFrame(frame)
}

override suspend fun receiveMessage(): Message {
return serializer.deserialize(receive())
}

override suspend fun close() {
webSocketSession.close()
}
}
88 changes: 88 additions & 0 deletions src/main/kotlin/io/xconn/xconn/WAMPSessionJoiner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.xconn.xconn

import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.client.plugins.websocket.webSocketSession
import io.ktor.client.request.header
import io.ktor.http.HttpMethod
import io.ktor.websocket.DefaultWebSocketSession
import io.ktor.websocket.Frame
import io.ktor.websocket.readBytes
import io.ktor.websocket.readText
import io.xconn.wampproto.Joiner
import io.xconn.wampproto.auth.AnonymousAuthenticator
import io.xconn.wampproto.auth.ClientAuthenticator
import io.xconn.wampproto.serializers.JSONSerializer
import io.xconn.wampproto.serializers.Serializer
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

class WAMPSessionJoiner(
private val authenticator: ClientAuthenticator = AnonymousAuthenticator(""),
private val serializer: Serializer = JSONSerializer(),
) {
private val subProtocol = getSubProtocol(serializer)
private val client =
HttpClient(CIO) {
install(WebSockets)
defaultRequest {
header("Sec-WebSocket-Protocol", subProtocol)
}
}

suspend fun join(host: String, port: Int, realm: String): BaseSession {
val welcomeCompleter = CompletableDeferred<BaseSession>()
val joiner = Joiner(realm, serializer, authenticator)

val session = client.webSocketSession(HttpMethod.Get, host, port)

// Send initial Hello message
session.sendFrame(joiner.sendHello())

coroutineScope {
val handshakeJob =
async {
for (frame in session.incoming) {
try {
val receivedData = receiveFrame(frame)
val toSend = joiner.receive(receivedData)

if (toSend == null) {
// Complete handshake and session creation
welcomeCompleter.complete(BaseSession(session, joiner.getSessionDetails(), serializer))
break
} else {
session.sendFrame(toSend)
}
} catch (error: Exception) {
welcomeCompleter.completeExceptionally(error)
break
}
}
}

handshakeJob.await()
}

return welcomeCompleter.await()
}
}

internal suspend fun DefaultWebSocketSession.sendFrame(data: Any) {
when (data) {
is String -> outgoing.send(Frame.Text(data))
is ByteArray -> outgoing.send(Frame.Binary(true, data))
else -> throw IllegalArgumentException("Unsupported frame type")
}
}

internal fun receiveFrame(frame: Frame): Any {
return when (frame) {
is Frame.Text -> frame.readText()
is Frame.Binary -> frame.readBytes()
else -> throw Exception("Unsupported frame type")
}
}