Skip to content

Commit

Permalink
Rename modules to match ktor plugins and migrate them to new ktor API
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed Apr 18, 2024
1 parent 7c9d3a5 commit 0a78bb9
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ public final class io/rsocket/kotlin/ktor/client/BuildersKt {
public static synthetic fun rSocket$default (Lio/ktor/client/HttpClient;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class io/rsocket/kotlin/ktor/client/RSocketSupport {
public static final field Plugin Lio/rsocket/kotlin/ktor/client/RSocketSupport$Plugin;
public synthetic fun <init> (Lio/rsocket/kotlin/core/RSocketConnector;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/rsocket/kotlin/ktor/client/RSocketSupport$Config {
public final class io/rsocket/kotlin/ktor/client/RSocketSupportConfig {
public final fun connector (Lio/rsocket/kotlin/core/RSocketConnector;)V
public final fun connector (Lkotlin/jvm/functions/Function1;)V
public final fun getConnector ()Lio/rsocket/kotlin/core/RSocketConnector;
public final fun setConnector (Lio/rsocket/kotlin/core/RSocketConnector;)V
}

public final class io/rsocket/kotlin/ktor/client/RSocketSupport$Plugin : io/ktor/client/plugins/HttpClientPlugin {
public fun getKey ()Lio/ktor/util/AttributeKey;
public fun install (Lio/rsocket/kotlin/ktor/client/RSocketSupport;Lio/ktor/client/HttpClient;)V
public synthetic fun install (Ljava/lang/Object;Lio/ktor/client/HttpClient;)V
public fun prepare (Lkotlin/jvm/functions/Function1;)Lio/rsocket/kotlin/ktor/client/RSocketSupport;
public synthetic fun prepare (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public final class io/rsocket/kotlin/ktor/client/RSocketSupportKt {
public static final fun getRSocketSupport ()Lio/ktor/client/plugins/api/ClientPlugin;
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ kotlin {

sourceSets {
commonMain.dependencies {
api(projects.rsocketKtor)
implementation(projects.rsocketTransportKtorWebsocketInternal)
api(projects.rsocketCore)
api(libs.ktor.client.websockets)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@
package io.rsocket.kotlin.ktor.client

import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.rsocket.kotlin.*
import io.rsocket.kotlin.transport.*
import io.rsocket.kotlin.transport.ktor.websocket.internal.*
import kotlin.coroutines.*

public suspend fun HttpClient.rSocket(
request: HttpRequestBuilder.() -> Unit,
): RSocket = plugin(RSocketSupport).run {
connector.connect(KtorClientTransport(this@rSocket, request))
}
): RSocket = connectRSocket(request)

public suspend fun HttpClient.rSocket(
urlString: String,
Expand Down Expand Up @@ -59,13 +52,3 @@ public suspend fun HttpClient.rSocket(
}
request()
}

private class KtorClientTransport(
private val client: HttpClient,
private val request: HttpRequestBuilder.() -> Unit,
) : ClientTransport {
override val coroutineContext: CoroutineContext get() = client.coroutineContext

@TransportApi
override suspend fun connect(): Connection = WebSocketConnection(client.webSocketSession(request))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.ktor.client

import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.api.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.util.*
import io.rsocket.kotlin.*
import io.rsocket.kotlin.core.*
import io.rsocket.kotlin.transport.*
import io.rsocket.kotlin.transport.ktor.websocket.internal.*
import kotlinx.coroutines.*
import kotlin.coroutines.*

private val RSocketSupportConfigKey = AttributeKey<RSocketSupportConfig.Internal>("RSocketSupportConfig")

public val RSocketSupport: ClientPlugin<RSocketSupportConfig> = createClientPlugin("RSocketSupport", ::RSocketSupportConfig) {
client.pluginOrNull(WebSockets) ?: error("RSocket require WebSockets to work. You must install WebSockets plugin first.")
client.attributes.put(RSocketSupportConfigKey, pluginConfig.toInternal())
}

public class RSocketSupportConfig internal constructor() {
private var connector = RSocketConnector()

public fun connector(connector: RSocketConnector) {
this.connector = connector
}

public fun connector(block: RSocketConnectorBuilder.() -> Unit) {
this.connector = RSocketConnector(block)
}

internal fun toInternal(): Internal = Internal(connector)
internal class Internal(val connector: RSocketConnector)
}

internal suspend fun HttpClient.connectRSocket(request: HttpRequestBuilder.() -> Unit): RSocket {
val config = attributes.getOrNull(RSocketSupportConfigKey)
?: error("Plugin `RSocketSupport` is not installed. Consider using `install(RSocketSupport)` in client config first.")

return config.connector.connect(RSocketSupportTarget(this, request))
}

private class RSocketSupportTarget(
private val client: HttpClient,
private val request: HttpRequestBuilder.() -> Unit,
) : RSocketClientTarget {
override val coroutineContext: CoroutineContext get() = client.coroutineContext

@RSocketTransportApi
override fun connectClient(handler: RSocketConnectionHandler): Job = launch {
client.webSocket(request) {
handler.handleKtorWebSocketConnection(this)
}
}
}
16 changes: 16 additions & 0 deletions ktor-plugins/ktor-server-rsocket/api/ktor-server-rsocket.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public final class io/rsocket/kotlin/ktor/server/RSocketSupportConfig {
public final fun server (Lio/rsocket/kotlin/core/RSocketServer;)V
public final fun server (Lkotlin/jvm/functions/Function1;)V
}

public final class io/rsocket/kotlin/ktor/server/RSocketSupportKt {
public static final fun getRSocketSupport ()Lio/ktor/server/application/ApplicationPlugin;
}

public final class io/rsocket/kotlin/ktor/server/RoutingKt {
public static final fun rSocket (Lio/ktor/server/routing/Route;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;)V
public static final fun rSocket (Lio/ktor/server/routing/Route;Ljava/lang/String;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;)V
public static synthetic fun rSocket$default (Lio/ktor/server/routing/Route;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;ILjava/lang/Object;)V
public static synthetic fun rSocket$default (Lio/ktor/server/routing/Route;Ljava/lang/String;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;ILjava/lang/Object;)V
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ kotlin {

sourceSets {
commonMain.dependencies {
api(projects.rsocketKtor)
implementation(projects.rsocketTransportKtorWebsocketInternal)
api(projects.rsocketCore)
api(libs.ktor.server.websockets)
}
commonTest.dependencies {
implementation(projects.rsocketKtorClient)
implementation(projects.rsocketTransportTests) //port provider
implementation(projects.ktorClientRsocket)
implementation(projects.rsocketTest)
implementation(libs.ktor.client.cio)
implementation(libs.ktor.server.cio)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.ktor.server

import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import io.ktor.util.*
import io.rsocket.kotlin.*
import io.rsocket.kotlin.core.*
import io.rsocket.kotlin.transport.*
import io.rsocket.kotlin.transport.ktor.websocket.internal.*

private val RSocketSupportConfigKey = AttributeKey<RSocketSupportConfig.Internal>("RSocketSupportConfig")

public val RSocketSupport: ApplicationPlugin<RSocketSupportConfig> = createApplicationPlugin("RSocketSupport", ::RSocketSupportConfig) {
application.pluginOrNull(WebSockets) ?: error("RSocket require WebSockets to work. You must install WebSockets plugin first.")
application.attributes.put(RSocketSupportConfigKey, pluginConfig.toInternal())
}

public class RSocketSupportConfig internal constructor() {
private var server: RSocketServer = RSocketServer()

public fun server(server: RSocketServer) {
this.server = server
}

public fun server(block: RSocketServerBuilder.() -> Unit) {
server = RSocketServer(block)
}


internal fun toInternal(): Internal = Internal(server)
internal class Internal(val server: RSocketServer)
}


@OptIn(RSocketTransportApi::class)
internal fun Route.rSocketHandler(acceptor: ConnectionAcceptor): suspend DefaultWebSocketServerSession.() -> Unit {
val config = application.attributes.getOrNull(RSocketSupportConfigKey)
?: error("Plugin RSocketSupport is not installed. Consider using `install(RSocketSupport)` in server config first.")

val handler = config.server.createHandler(acceptor)
return {
handler.handleKtorWebSocketConnection(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.ktor.server

import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import io.rsocket.kotlin.*

public fun Route.rSocket(protocol: String? = null, acceptor: ConnectionAcceptor) {
webSocket(protocol, rSocketHandler(acceptor))
}

public fun Route.rSocket(path: String, protocol: String? = null, acceptor: ConnectionAcceptor) {
webSocket(path, protocol, rSocketHandler(acceptor))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,11 +40,13 @@ class WebSocketConnectionTest : SuspendTest, TestWithLeakCheck {
private val client = HttpClient(ClientCIO) {
install(ClientWebSockets)
install(ClientRSocketSupport) {
connector = TestConnector {
connectionConfig {
keepAlive = KeepAlive(500)
connector(
TestConnector {
connectionConfig {
keepAlive = KeepAlive(500)
}
}
}
)
}
}

Expand All @@ -53,7 +55,7 @@ class WebSocketConnectionTest : SuspendTest, TestWithLeakCheck {
private val server = embeddedServer(ServerCIO, port = 0) {
install(ServerWebSockets)
install(ServerRSocketSupport) {
server = TestServer()
server(TestServer())
}
install(Routing) {
rSocket {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
id("rsocketbuild.multiplatform-library")
}

description = "rsocket-kotlin ktor integration"
description = "OLD ARTIFACT - migrate to ktor-client-rsocket"

kotlin {
jvmTarget()
Expand All @@ -29,9 +29,7 @@ kotlin {

sourceSets {
commonMain.dependencies {
api(projects.rsocketCore)
api(projects.rsocketTransportKtorWebsocketInternal)
//TODO ContentNegotiation will be here later
implementation(projects.ktorClientRsocket)
}
}
}
19 changes: 19 additions & 0 deletions ktor-plugins/rsocket-ktor-client/src/commonMain/kotlin/dummy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.deprecated

private val dummy = 0
Empty file.
34 changes: 34 additions & 0 deletions ktor-plugins/rsocket-ktor-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import rsocketbuild.*

plugins {
id("rsocketbuild.multiplatform-library")
}

description = "OLD ARTIFACT - migrate to ktor-server-rsocket"

kotlin {
jvmTarget()
nixTargets()

sourceSets {
commonMain.dependencies {
implementation(projects.ktorServerRsocket)
}
}
}
Loading

0 comments on commit 0a78bb9

Please sign in to comment.