From a09a2b500e2e7b63c5837958eda8730f19b4cc00 Mon Sep 17 00:00:00 2001 From: olme04 <86063649+olme04@users.noreply.github.com> Date: Thu, 14 Apr 2022 15:20:15 +0300 Subject: [PATCH] actualize sample for 0.15.4 (#223) * add support for running server on nodejs TCP * commonize server starting and client creating * create common Servers object with all possible variations of started servers --- samples/chat/README.md | 24 +++++---- samples/chat/api/build.gradle.kts | 18 ++++++- .../chat/api/src/commonMain/kotlin/ChatApi.kt | 16 ++++++ .../api/src/commonMain/kotlin/MessageApi.kt | 16 ++++++ .../src/commonMain/kotlin/Serialization.kt | 16 ++++++ .../chat/api/src/commonMain/kotlin/Servers.kt | 49 +++++++++++++++++++ .../chat/api/src/commonMain/kotlin/UserApi.kt | 16 ++++++ samples/chat/client/build.gradle.kts | 40 +++++++++++++-- .../chat/client/src/browserMain/kotlin/App.kt | 33 +++++++++++++ .../src/browserMain/kotlin/clientTransport.kt | 31 ++++++++++++ .../resources/index.html | 8 +-- .../client/src/commonMain/kotlin/ApiClient.kt | 31 ++++++++++++ .../src/commonMain/kotlin/ChatApiClient.kt | 16 ++++++ .../src/commonMain/kotlin/MessageApiClient.kt | 16 ++++++ .../src/commonMain/kotlin/UserApiClient.kt | 16 ++++++ .../kotlin/clientTransport.common.kt | 29 +++++++++++ .../client/src/commonMain/kotlin/connect.kt | 32 ------------ .../client/src/commonMain/kotlin/usage.kt | 32 ++++++++---- samples/chat/client/src/jsMain/kotlin/App.kt | 5 -- samples/chat/client/src/jvmMain/kotlin/App.kt | 37 ++++++++------ .../src/jvmMain/kotlin/clientTransport.kt | 32 ++++++++++++ .../chat/client/src/nativeMain/kotlin/App.kt | 32 ++++++++---- .../src/nativeMain/kotlin/clientTransport.kt | 32 ++++++++++++ .../chat/client/src/nodejsMain/kotlin/App.kt | 32 ++++++++++++ .../src/nodejsMain/kotlin/clientTransport.kt | 32 ++++++++++++ samples/chat/gradle.properties | 34 ++++++++++--- .../gradle/wrapper/gradle-wrapper.properties | 2 +- samples/chat/server/build.gradle.kts | 39 +++++++++++++-- .../server/src/commonMain/kotlin/ChatApi.kt | 16 ++++++ .../server/src/commonMain/kotlin/Chats.kt | 16 ++++++ .../src/commonMain/kotlin/MessageApi.kt | 22 +++++++-- .../server/src/commonMain/kotlin/Messages.kt | 16 ++++++ .../server/src/commonMain/kotlin/Session.kt | 16 ++++++ .../server/src/commonMain/kotlin/UserApi.kt | 16 ++++++ .../server/src/commonMain/kotlin/Users.kt | 16 ++++++ .../server/src/commonMain/kotlin/acceptor.kt | 37 +++++++++++++- .../server/src/commonMain/kotlin/expects.kt | 18 ++++++- .../kotlin/serverTransport.common.kt | 29 +++++++++++ samples/chat/server/src/jsMain/kotlin/App.kt | 23 +++++++++ .../server/src/jsMain/kotlin/expects.js.kt | 26 ++++++++++ .../src/jsMain/kotlin/serverTransport.kt | 30 ++++++++++++ samples/chat/server/src/jvmMain/kotlin/App.kt | 46 +++++++---------- .../server/src/jvmMain/kotlin/expects.jvm.kt | 16 ++++++ .../src/jvmMain/kotlin/serverTransport.kt | 32 ++++++++++++ .../chat/server/src/nativeMain/kotlin/App.kt | 34 +++++++------ .../src/nativeMain/kotlin/expects.native.kt | 16 ++++++ .../src/nativeMain/kotlin/serverTransport.kt | 32 ++++++++++++ samples/chat/settings.gradle.kts | 16 ++++++ 48 files changed, 1038 insertions(+), 151 deletions(-) create mode 100644 samples/chat/api/src/commonMain/kotlin/Servers.kt create mode 100644 samples/chat/client/src/browserMain/kotlin/App.kt create mode 100644 samples/chat/client/src/browserMain/kotlin/clientTransport.kt rename samples/chat/client/src/{jsMain => browserMain}/resources/index.html (82%) create mode 100644 samples/chat/client/src/commonMain/kotlin/clientTransport.common.kt delete mode 100644 samples/chat/client/src/commonMain/kotlin/connect.kt delete mode 100644 samples/chat/client/src/jsMain/kotlin/App.kt create mode 100644 samples/chat/client/src/jvmMain/kotlin/clientTransport.kt create mode 100644 samples/chat/client/src/nativeMain/kotlin/clientTransport.kt create mode 100644 samples/chat/client/src/nodejsMain/kotlin/App.kt create mode 100644 samples/chat/client/src/nodejsMain/kotlin/clientTransport.kt create mode 100644 samples/chat/server/src/commonMain/kotlin/serverTransport.common.kt create mode 100644 samples/chat/server/src/jsMain/kotlin/App.kt create mode 100644 samples/chat/server/src/jsMain/kotlin/expects.js.kt create mode 100644 samples/chat/server/src/jsMain/kotlin/serverTransport.kt create mode 100644 samples/chat/server/src/jvmMain/kotlin/serverTransport.kt create mode 100644 samples/chat/server/src/nativeMain/kotlin/serverTransport.kt diff --git a/samples/chat/README.md b/samples/chat/README.md index f83991ef5..6d9b3fc39 100644 --- a/samples/chat/README.md +++ b/samples/chat/README.md @@ -1,13 +1,17 @@ # chat * api - shared chat API for both client and server -* client - client API implementation as requests to RSocket with Protobuf serialization. Works on JVM(TCP/WS), JS(WS), - Native(TCP). Tasks for running sample clients: - * JVM: `run` - * Native: `runDebugExecutableNative` / `runReleaseExecutableNative` - * NodeJs: `jsNodeRun` - * Browser: `jsBrowserRun` -* server - server API implementation with storage in ordinary concurrent map and exposing it through RSocket with - Protobuf serialization. Can be started on JVM(TCP+WS) and Native(TCP). Tasks for running sample servers: - * JVM: `run` - * Native: `runDebugExecutableNative` / `runReleaseExecutableNative` +* client - client API implementation via requesting to RSocket with Protobuf serialization. + Works on JVM(TCP/WS), Native(TCP/WS), NodeJS(WS/TCP), Browser(WS). + Tasks for running clients: + * JVM: `run` + * Native: `runDebugExecutableNative` / `runReleaseExecutableNative` + * NodeJs: `nodejsNodeRun` / `nodejsNodeDevelopmentRun` / `nodejsNodeProductionRun` + * Browser: `browserBrowserRun` / `browserBrowserDevelopmentRun` / `browserBrowserProductionRun` +* server - server API implementation with storage in concurrent map + and exposing it through RSocket with Protobuf serialization. + Can be started on JVM(TCP/WS), Native(TCP/WS), NodeJS(TCP). + Tasks for running servers: + * JVM: `run` + * Native: `runDebugExecutableNative` / `runReleaseExecutableNative` + * NodeJs: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun` diff --git a/samples/chat/api/build.gradle.kts b/samples/chat/api/build.gradle.kts index 22eb57613..e0df9283c 100644 --- a/samples/chat/api/build.gradle.kts +++ b/samples/chat/api/build.gradle.kts @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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 org.jetbrains.kotlin.konan.target.* plugins { @@ -10,7 +26,7 @@ val kotlinxSerializationVersion: String by rootProject kotlin { jvm() - js(IR) { + js { browser() nodejs() } diff --git a/samples/chat/api/src/commonMain/kotlin/ChatApi.kt b/samples/chat/api/src/commonMain/kotlin/ChatApi.kt index 2ed70347a..f7d700b14 100644 --- a/samples/chat/api/src/commonMain/kotlin/ChatApi.kt +++ b/samples/chat/api/src/commonMain/kotlin/ChatApi.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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.samples.chat.api import kotlinx.serialization.* diff --git a/samples/chat/api/src/commonMain/kotlin/MessageApi.kt b/samples/chat/api/src/commonMain/kotlin/MessageApi.kt index c69709709..c30797b2f 100644 --- a/samples/chat/api/src/commonMain/kotlin/MessageApi.kt +++ b/samples/chat/api/src/commonMain/kotlin/MessageApi.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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.samples.chat.api import kotlinx.coroutines.flow.* diff --git a/samples/chat/api/src/commonMain/kotlin/Serialization.kt b/samples/chat/api/src/commonMain/kotlin/Serialization.kt index 8515da20d..f590e9302 100644 --- a/samples/chat/api/src/commonMain/kotlin/Serialization.kt +++ b/samples/chat/api/src/commonMain/kotlin/Serialization.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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.samples.chat.api import io.ktor.utils.io.core.* diff --git a/samples/chat/api/src/commonMain/kotlin/Servers.kt b/samples/chat/api/src/commonMain/kotlin/Servers.kt new file mode 100644 index 000000000..efb6f09b2 --- /dev/null +++ b/samples/chat/api/src/commonMain/kotlin/Servers.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2015-2022 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.samples.chat.api + +enum class TransportType { TCP, WS } + +data class ServerAddress(val port: Int, val type: TransportType) + +object Servers { + object JVM { + val TCP = ServerAddress(port = 8001, type = TransportType.TCP) + val WS = ServerAddress(port = 8002, type = TransportType.WS) + } + + object JS { + val TCP = ServerAddress(port = 7001, type = TransportType.TCP) + } + + object Native { + val TCP = ServerAddress(port = 9001, type = TransportType.TCP) + val WS = ServerAddress(port = 9002, type = TransportType.WS) + } + + val WS = setOf( + JVM.WS, + Native.WS + ) + val TCP = setOf( + JVM.TCP, + JS.TCP, + Native.TCP + ) + + val ALL = WS + TCP +} diff --git a/samples/chat/api/src/commonMain/kotlin/UserApi.kt b/samples/chat/api/src/commonMain/kotlin/UserApi.kt index 05ba6c64f..3d92a1a33 100644 --- a/samples/chat/api/src/commonMain/kotlin/UserApi.kt +++ b/samples/chat/api/src/commonMain/kotlin/UserApi.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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.samples.chat.api import kotlinx.serialization.* diff --git a/samples/chat/client/build.gradle.kts b/samples/chat/client/build.gradle.kts index b02aaa553..0aef81492 100644 --- a/samples/chat/client/build.gradle.kts +++ b/samples/chat/client/build.gradle.kts @@ -1,3 +1,19 @@ +/* + * Copyright 2015-2022 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 org.jetbrains.kotlin.konan.target.* plugins { @@ -17,10 +33,12 @@ kotlin { jvm { withJava() } - js(IR) { + js("browser") { browser { binaries.executable() } + } + js("nodejs") { nodejs { binaries.executable() } @@ -33,7 +51,6 @@ kotlin { }?.binaries { executable { entryPoint = "io.rsocket.kotlin.samples.chat.client.main" - freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis" //TODO } } @@ -41,17 +58,30 @@ kotlin { commonMain { dependencies { implementation(project(":api")) - implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:$rsocketVersion") + implementation("io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:$rsocketVersion") } } val jvmMain by getting { dependencies { + implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion") implementation("io.ktor:ktor-client-cio:$ktorVersion") } } - val jsMain by getting { + findByName("nativeMain")?.apply { + dependencies { + implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion") + implementation("io.ktor:ktor-client-cio:$ktorVersion") + } + } + val browserMain by getting { + dependencies { + implementation("io.ktor:ktor-client-js:$ktorVersion") + } + } + val nodejsMain by getting { dependencies { - implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-js:$ktorVersion") + implementation("io.rsocket.kotlin:rsocket-transport-nodejs-tcp:$rsocketVersion") } } } diff --git a/samples/chat/client/src/browserMain/kotlin/App.kt b/samples/chat/client/src/browserMain/kotlin/App.kt new file mode 100644 index 000000000..cd54f5fed --- /dev/null +++ b/samples/chat/client/src/browserMain/kotlin/App.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2015-2022 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.samples.chat.client + +import io.rsocket.kotlin.samples.chat.api.* +import kotlinx.coroutines.* + +suspend fun main() { + coroutineScope { + //only WS is supported on browser JS + // native WS server is incompatible with js WS client + (Servers.WS - Servers.Native.WS).forEach { + val client = ApiClient(it, "Yuri") + launch { + client.use(it, "RSocket is awesome! (from browser)") + } + } + } +} diff --git a/samples/chat/client/src/browserMain/kotlin/clientTransport.kt b/samples/chat/client/src/browserMain/kotlin/clientTransport.kt new file mode 100644 index 000000000..e18a78b7c --- /dev/null +++ b/samples/chat/client/src/browserMain/kotlin/clientTransport.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2015-2022 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.samples.chat.client + +import io.ktor.client.engine.js.* +import io.rsocket.kotlin.samples.chat.api.* +import io.rsocket.kotlin.transport.* +import io.rsocket.kotlin.transport.ktor.websocket.client.* + +internal actual fun clientTransport( + type: TransportType, + host: String, + port: Int +): ClientTransport = when (type) { + TransportType.TCP -> error("TCP is not supported") + TransportType.WS -> WebSocketClientTransport(Js, host, port) +} diff --git a/samples/chat/client/src/jsMain/resources/index.html b/samples/chat/client/src/browserMain/resources/index.html similarity index 82% rename from samples/chat/client/src/jsMain/resources/index.html rename to samples/chat/client/src/browserMain/resources/index.html index 6b9fe7dff..f98970589 100644 --- a/samples/chat/client/src/jsMain/resources/index.html +++ b/samples/chat/client/src/browserMain/resources/index.html @@ -1,5 +1,5 @@