Skip to content

Commit dff5d82

Browse files
committed
Rename modules to match ktor plugins and migrate them to new ktor API
1 parent e9f1ff8 commit dff5d82

File tree

21 files changed

+278
-218
lines changed

21 files changed

+278
-218
lines changed

rsocket-ktor/rsocket-ktor-client/api/rsocket-ktor-client.api renamed to ktor-plugins/ktor-client-rsocket/api/ktor-client-rsocket.api

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ public final class io/rsocket/kotlin/ktor/client/BuildersKt {
66
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;
77
}
88

9-
public final class io/rsocket/kotlin/ktor/client/RSocketSupport {
10-
public static final field Plugin Lio/rsocket/kotlin/ktor/client/RSocketSupport$Plugin;
11-
public synthetic fun <init> (Lio/rsocket/kotlin/core/RSocketConnector;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
12-
}
13-
14-
public final class io/rsocket/kotlin/ktor/client/RSocketSupport$Config {
9+
public final class io/rsocket/kotlin/ktor/client/RSocketSupportConfig {
10+
public final fun connector (Lio/rsocket/kotlin/core/RSocketConnector;)V
1511
public final fun connector (Lkotlin/jvm/functions/Function1;)V
16-
public final fun getConnector ()Lio/rsocket/kotlin/core/RSocketConnector;
17-
public final fun setConnector (Lio/rsocket/kotlin/core/RSocketConnector;)V
1812
}
1913

20-
public final class io/rsocket/kotlin/ktor/client/RSocketSupport$Plugin : io/ktor/client/plugins/HttpClientPlugin {
21-
public fun getKey ()Lio/ktor/util/AttributeKey;
22-
public fun install (Lio/rsocket/kotlin/ktor/client/RSocketSupport;Lio/ktor/client/HttpClient;)V
23-
public synthetic fun install (Ljava/lang/Object;Lio/ktor/client/HttpClient;)V
24-
public fun prepare (Lkotlin/jvm/functions/Function1;)Lio/rsocket/kotlin/ktor/client/RSocketSupport;
25-
public synthetic fun prepare (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
14+
public final class io/rsocket/kotlin/ktor/client/RSocketSupportKt {
15+
public static final fun getRSocketSupport ()Lio/ktor/client/plugins/api/ClientPlugin;
2616
}
2717

rsocket-ktor/rsocket-ktor-client/build.gradle.kts renamed to ktor-plugins/ktor-client-rsocket/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ kotlin {
2929

3030
sourceSets {
3131
commonMain.dependencies {
32-
api(projects.rsocketKtor)
32+
implementation(projects.rsocketTransportKtorWebsocketInternal)
33+
api(projects.rsocketCore)
3334
api(libs.ktor.client.websockets)
3435
}
3536
}

rsocket-ktor/rsocket-ktor-client/src/commonMain/kotlin/io/rsocket/kotlin/ktor/client/Builders.kt renamed to ktor-plugins/ktor-client-rsocket/src/commonMain/kotlin/io/rsocket/kotlin/ktor/client/Builders.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@
1717
package io.rsocket.kotlin.ktor.client
1818

1919
import io.ktor.client.*
20-
import io.ktor.client.plugins.*
21-
import io.ktor.client.plugins.websocket.*
2220
import io.ktor.client.request.*
2321
import io.ktor.http.*
2422
import io.rsocket.kotlin.*
25-
import io.rsocket.kotlin.transport.*
26-
import io.rsocket.kotlin.transport.ktor.websocket.internal.*
27-
import kotlin.coroutines.*
2823

2924
public suspend fun HttpClient.rSocket(
3025
request: HttpRequestBuilder.() -> Unit,
31-
): RSocket = plugin(RSocketSupport).run {
32-
connector.connect(KtorClientTransport(this@rSocket, request))
33-
}
26+
): RSocket = connectRSocket(request)
3427

3528
public suspend fun HttpClient.rSocket(
3629
urlString: String,
@@ -59,13 +52,3 @@ public suspend fun HttpClient.rSocket(
5952
}
6053
request()
6154
}
62-
63-
private class KtorClientTransport(
64-
private val client: HttpClient,
65-
private val request: HttpRequestBuilder.() -> Unit,
66-
) : ClientTransport {
67-
override val coroutineContext: CoroutineContext get() = client.coroutineContext
68-
69-
@TransportApi
70-
override suspend fun connect(): Connection = WebSocketConnection(client.webSocketSession(request))
71-
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.ktor.client
18+
19+
import io.ktor.client.*
20+
import io.ktor.client.plugins.*
21+
import io.ktor.client.plugins.api.*
22+
import io.ktor.client.plugins.websocket.*
23+
import io.ktor.client.request.*
24+
import io.ktor.util.*
25+
import io.rsocket.kotlin.*
26+
import io.rsocket.kotlin.core.*
27+
import io.rsocket.kotlin.transport.*
28+
import io.rsocket.kotlin.transport.ktor.websocket.internal.*
29+
import kotlinx.coroutines.*
30+
import kotlin.coroutines.*
31+
32+
private val RSocketSupportConfigKey = AttributeKey<RSocketSupportConfig.Internal>("RSocketSupportConfig")
33+
34+
public val RSocketSupport: ClientPlugin<RSocketSupportConfig> = createClientPlugin("RSocketSupport", ::RSocketSupportConfig) {
35+
client.pluginOrNull(WebSockets) ?: error("RSocket require WebSockets to work. You must install WebSockets plugin first.")
36+
client.attributes.put(RSocketSupportConfigKey, pluginConfig.toInternal())
37+
}
38+
39+
public class RSocketSupportConfig internal constructor() {
40+
private var connector = RSocketConnector()
41+
42+
public fun connector(connector: RSocketConnector) {
43+
this.connector = connector
44+
}
45+
46+
public fun connector(block: RSocketConnectorBuilder.() -> Unit) {
47+
this.connector = RSocketConnector(block)
48+
}
49+
50+
internal fun toInternal(): Internal = Internal(connector)
51+
internal class Internal(val connector: RSocketConnector)
52+
}
53+
54+
internal suspend fun HttpClient.connectRSocket(request: HttpRequestBuilder.() -> Unit): RSocket {
55+
val config = attributes.getOrNull(RSocketSupportConfigKey)
56+
?: error("Plugin `RSocketSupport` is not installed. Consider using `install(RSocketSupport)` in client config first.")
57+
58+
return config.connector.connect(RSocketSupportTarget(this, request))
59+
}
60+
61+
@OptIn(RSocketTransportApi::class)
62+
private class RSocketSupportTarget(
63+
private val client: HttpClient,
64+
private val request: HttpRequestBuilder.() -> Unit,
65+
) : RSocketClientTarget {
66+
override val coroutineContext: CoroutineContext get() = client.coroutineContext
67+
68+
@RSocketTransportApi
69+
override fun connectClient(handler: RSocketConnectionHandler): Job = launch {
70+
client.webSocket(request) {
71+
handler.handleKtorWebSocketConnection(this)
72+
}
73+
}
74+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public final class io/rsocket/kotlin/ktor/server/RSocketSupportConfig {
2+
public final fun server (Lio/rsocket/kotlin/core/RSocketServer;)V
3+
public final fun server (Lkotlin/jvm/functions/Function1;)V
4+
}
5+
6+
public final class io/rsocket/kotlin/ktor/server/RSocketSupportKt {
7+
public static final fun getRSocketSupport ()Lio/ktor/server/application/ApplicationPlugin;
8+
}
9+
10+
public final class io/rsocket/kotlin/ktor/server/RoutingKt {
11+
public static final fun rSocket (Lio/ktor/server/routing/Route;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;)V
12+
public static final fun rSocket (Lio/ktor/server/routing/Route;Ljava/lang/String;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;)V
13+
public static synthetic fun rSocket$default (Lio/ktor/server/routing/Route;Ljava/lang/String;Lio/rsocket/kotlin/ConnectionAcceptor;ILjava/lang/Object;)V
14+
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
15+
}
16+

rsocket-ktor/rsocket-ktor-server/build.gradle.kts renamed to ktor-plugins/ktor-server-rsocket/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ kotlin {
2828

2929
sourceSets {
3030
commonMain.dependencies {
31-
api(projects.rsocketKtor)
31+
implementation(projects.rsocketTransportKtorWebsocketInternal)
32+
api(projects.rsocketCore)
3233
api(libs.ktor.server.websockets)
3334
}
3435
commonTest.dependencies {
35-
implementation(projects.rsocketKtorClient)
36-
implementation(projects.rsocketTransportTests) //port provider
36+
implementation(projects.ktorClientRsocket)
37+
implementation(projects.rsocketTest)
3738
implementation(libs.ktor.client.cio)
3839
implementation(libs.ktor.server.cio)
3940
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.ktor.server
18+
19+
import io.ktor.server.application.*
20+
import io.ktor.server.routing.*
21+
import io.ktor.server.websocket.*
22+
import io.ktor.util.*
23+
import io.rsocket.kotlin.*
24+
import io.rsocket.kotlin.core.*
25+
import io.rsocket.kotlin.transport.*
26+
import io.rsocket.kotlin.transport.ktor.websocket.internal.*
27+
28+
private val RSocketSupportConfigKey = AttributeKey<RSocketSupportConfig.Internal>("RSocketSupportConfig")
29+
30+
public val RSocketSupport: ApplicationPlugin<RSocketSupportConfig> = createApplicationPlugin("RSocketSupport", ::RSocketSupportConfig) {
31+
application.pluginOrNull(WebSockets) ?: error("RSocket require WebSockets to work. You must install WebSockets plugin first.")
32+
application.attributes.put(RSocketSupportConfigKey, pluginConfig.toInternal())
33+
}
34+
35+
public class RSocketSupportConfig internal constructor() {
36+
private var server: RSocketServer = RSocketServer()
37+
38+
public fun server(server: RSocketServer) {
39+
this.server = server
40+
}
41+
42+
public fun server(block: RSocketServerBuilder.() -> Unit) {
43+
server = RSocketServer(block)
44+
}
45+
46+
47+
internal fun toInternal(): Internal = Internal(server)
48+
internal class Internal(val server: RSocketServer)
49+
}
50+
51+
52+
@OptIn(RSocketTransportApi::class)
53+
internal fun Route.rSocketHandler(acceptor: ConnectionAcceptor): suspend DefaultWebSocketServerSession.() -> Unit {
54+
val config = application.attributes.getOrNull(RSocketSupportConfigKey)
55+
?: error("Plugin RSocketSupport is not installed. Consider using `install(RSocketSupport)` in server config first.")
56+
57+
val handler = config.server.createHandler(acceptor)
58+
return {
59+
handler.handleKtorWebSocketConnection(this)
60+
}
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.ktor.server
18+
19+
import io.ktor.server.routing.*
20+
import io.ktor.server.websocket.*
21+
import io.rsocket.kotlin.*
22+
23+
public fun Route.rSocket(protocol: String? = null, acceptor: ConnectionAcceptor) {
24+
webSocket(protocol, rSocketHandler(acceptor))
25+
}
26+
27+
public fun Route.rSocket(path: String, protocol: String? = null, acceptor: ConnectionAcceptor) {
28+
webSocket(path, protocol, rSocketHandler(acceptor))
29+
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,11 +40,13 @@ class WebSocketConnectionTest : SuspendTest, TestWithLeakCheck {
4040
private val client = HttpClient(ClientCIO) {
4141
install(ClientWebSockets)
4242
install(ClientRSocketSupport) {
43-
connector = TestConnector {
44-
connectionConfig {
45-
keepAlive = KeepAlive(500)
43+
connector(
44+
TestConnector {
45+
connectionConfig {
46+
keepAlive = KeepAlive(500)
47+
}
4648
}
47-
}
49+
)
4850
}
4951
}
5052

@@ -53,7 +55,7 @@ class WebSocketConnectionTest : SuspendTest, TestWithLeakCheck {
5355
private val server = embeddedServer(ServerCIO, port = 0) {
5456
install(ServerWebSockets)
5557
install(ServerRSocketSupport) {
56-
server = TestServer()
58+
server(TestServer())
5759
}
5860
install(Routing) {
5961
rSocket {

rsocket-ktor/build.gradle.kts renamed to ktor-plugins/rsocket-ktor-client/build.gradle.kts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020
id("rsocketbuild.multiplatform-library")
2121
}
2222

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

2525
kotlin {
2626
jvmTarget()
@@ -29,9 +29,7 @@ kotlin {
2929

3030
sourceSets {
3131
commonMain.dependencies {
32-
api(projects.rsocketCore)
33-
api(projects.rsocketTransportKtorWebsocketInternal)
34-
//TODO ContentNegotiation will be here later
32+
implementation(projects.ktorClientRsocket)
3533
}
3634
}
3735
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.kotlin.deprecated
18+
19+
private val dummy = 0

ktor-plugins/rsocket-ktor-server/api/rsocket-ktor-server.api

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import rsocketbuild.*
18+
19+
plugins {
20+
id("rsocketbuild.multiplatform-library")
21+
}
22+
23+
description = "OLD ARTIFACT - migrate to ktor-server-rsocket"
24+
25+
kotlin {
26+
jvmTarget()
27+
nixTargets()
28+
29+
sourceSets {
30+
commonMain.dependencies {
31+
implementation(projects.ktorServerRsocket)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)