diff --git a/grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.kt b/grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.kt index 28f7d8136..c9f8ebc1e 100644 --- a/grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.kt +++ b/grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.kt @@ -189,7 +189,8 @@ internal fun grpc_status_code.toKotlin(): StatusCode = when (this) { grpc_status_code.GRPC_STATUS_UNAVAILABLE -> StatusCode.UNAVAILABLE grpc_status_code.GRPC_STATUS_DATA_LOSS -> StatusCode.DATA_LOSS grpc_status_code.GRPC_STATUS_UNAUTHENTICATED -> StatusCode.UNAUTHENTICATED - grpc_status_code.GRPC_STATUS__DO_NOT_USE -> error("Invalid status code: ${this.ordinal}") + grpc_status_code.GRPC_STATUS__DO_NOT_USE -> error("Invalid status code: $this") + else -> error("Invalid status code: $this") } internal fun StatusCode.toRawCallAllocation(): grpc_status_code = when (this) { diff --git a/protobuf/protobuf-core/build.gradle.kts b/protobuf/protobuf-core/build.gradle.kts index 820c2a2ce..d421c7f0a 100644 --- a/protobuf/protobuf-core/build.gradle.kts +++ b/protobuf/protobuf-core/build.gradle.kts @@ -86,3 +86,14 @@ tasks.withType().configureEach { outputDirectory = generatedCodeDir } } + +// TODO: What is the correct way to declare this dependency? (KRPC-223) +// (without it fails when executing "publishAllPublicationsToBuildRepository")" +val bufGenerateCommonMain = tasks.named("bufGenerateCommonMain") + +tasks.withType().configureEach { + // Only for sources jars + if (archiveClassifier.orNull == "sources" || name.endsWith("SourcesJar")) { + dependsOn(bufGenerateCommonMain) + } +} diff --git a/samples/grpc-kmp-app/.gitignore b/samples/grpc-kmp-app/.gitignore new file mode 100644 index 000000000..7d9c0e482 --- /dev/null +++ b/samples/grpc-kmp-app/.gitignore @@ -0,0 +1,18 @@ +*.iml +.kotlin +.gradle +**/build/ +xcuserdata +!src/**/build/ +local.properties +.idea +.DS_Store +captures +.externalNativeBuild +.cxx +*.xcodeproj/* +!*.xcodeproj/project.pbxproj +!*.xcodeproj/xcshareddata/ +!*.xcodeproj/project.xcworkspace/ +!*.xcworkspace/contents.xcworkspacedata +**/xcshareddata/WorkspaceSettings.xcsettings diff --git a/samples/grpc-kmp-app/README.md b/samples/grpc-kmp-app/README.md new file mode 100644 index 000000000..cb9d61afb --- /dev/null +++ b/samples/grpc-kmp-app/README.md @@ -0,0 +1,45 @@ +This is a Kotlin Multiplatform project targeting iOS, Desktop (JVM), Server, demonstrating the `kotlinx.rpc` library +for gRPC. + +* [/composeApp](./composeApp/src) is for code that will be shared across Compose Multiplatform applications. + - [commonMain](./composeApp/src/commonMain/kotlin) contains all the relevant gRPC client application code for all platforms. + +* [/server](./server/src/main/kotlin) is for the gRPC server application. + +* [/shared](./shared/src) is for the code that will be shared between all targets in the project. + It contains the proto files and the generated code used by both, the server and the client applications. + +### Build and Run Desktop (JVM) Application + +To build and run the development version of the desktop app, use the run configuration from the run widget +in your IDE’s toolbar or run it directly from the terminal: +- on macOS/Linux + ```shell + ./gradlew :composeApp:run + ``` +- on Windows + ```shell + .\gradlew.bat :composeApp:run + ``` + +### Build and Run Server + +To build and run the server, use the run configuration from the run widget +in your IDE’s toolbar or run it directly from the terminal: +- on macOS/Linux + ```shell + ./gradlew :server:run + ``` +- on Windows + ```shell + .\gradlew.bat :server:run + ``` + +### Build and Run iOS Application + +To build and run the development version of the iOS app, use the run configuration from the run widget +in your IDE’s toolbar or open the [/iosApp](./iosApp) directory in Xcode and run it from there. + +--- + +Learn more about [Kotlin Multiplatform](https://www.jetbrains.com/help/kotlin-multiplatform-dev/get-started.html)… \ No newline at end of file diff --git a/samples/grpc-kmp-app/build.gradle.kts b/samples/grpc-kmp-app/build.gradle.kts new file mode 100644 index 000000000..74742711e --- /dev/null +++ b/samples/grpc-kmp-app/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + // this is necessary to avoid the plugins to be loaded multiple times + // in each subproject's classloader + alias(libs.plugins.composeHotReload) apply false + alias(libs.plugins.composeMultiplatform) apply false + alias(libs.plugins.composeCompiler) apply false + alias(libs.plugins.kotlinJvm) apply false + alias(libs.plugins.kotlinMultiplatform) apply false + alias(libs.plugins.kotlinxRpc) apply false +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/composeApp/build.gradle.kts b/samples/grpc-kmp-app/composeApp/build.gradle.kts new file mode 100644 index 000000000..c94d08db8 --- /dev/null +++ b/samples/grpc-kmp-app/composeApp/build.gradle.kts @@ -0,0 +1,60 @@ +import org.jetbrains.compose.desktop.application.dsl.TargetFormat + +plugins { + alias(libs.plugins.kotlinMultiplatform) + alias(libs.plugins.composeMultiplatform) + alias(libs.plugins.composeCompiler) + alias(libs.plugins.composeHotReload) +} + +kotlin { + listOf( + iosArm64(), + iosSimulatorArm64() + ).forEach { iosTarget -> + iosTarget.binaries.framework { + baseName = "ComposeApp" + isStatic = true + } + } + + jvm() + + sourceSets { + commonMain.dependencies { + implementation(compose.runtime) + implementation(compose.foundation) + implementation(compose.material3) + implementation(compose.ui) + implementation(compose.components.resources) + implementation(compose.components.uiToolingPreview) + implementation(libs.androidx.lifecycle.viewmodelCompose) + implementation(libs.androidx.lifecycle.runtimeCompose) + implementation(libs.kotlinx.datetime) + implementation(libs.kotlinx.rpc.protobuf.core) + implementation(libs.kotlinx.rpc.grpc.core) + implementation(libs.grpc.netty) + implementation(projects.shared) + } + commonTest.dependencies { + implementation(libs.kotlin.test) + } + jvmMain.dependencies { + implementation(compose.desktop.currentOs) + implementation(libs.kotlinx.coroutinesSwing) + } + } +} + + +compose.desktop { + application { + mainClass = "kotlinx.rpc.sample.MainKt" + + nativeDistributions { + targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) + packageName = "kotlinx.rpc.sample" + packageVersion = "1.0.0" + } + } +} diff --git a/samples/grpc-kmp-app/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml b/samples/grpc-kmp-app/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml new file mode 100644 index 000000000..1ffc948c2 --- /dev/null +++ b/samples/grpc-kmp-app/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/grpc-kmp-app/composeApp/src/commonMain/kotlin/kotlinx/rpc/sample/App.kt b/samples/grpc-kmp-app/composeApp/src/commonMain/kotlin/kotlinx/rpc/sample/App.kt new file mode 100644 index 000000000..f359da193 --- /dev/null +++ b/samples/grpc-kmp-app/composeApp/src/commonMain/kotlin/kotlinx/rpc/sample/App.kt @@ -0,0 +1,200 @@ +package kotlinx.rpc.sample + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.safeDrawing +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.layout.windowInsetsPadding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import kotlinx.coroutines.launch +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toLocalDateTime +import kotlinx.rpc.grpc.GrpcClient +import kotlinx.rpc.internal.utils.ExperimentalRpcApi +import kotlinx.rpc.sample.messages.ChatEntry +import kotlinx.rpc.sample.messages.MessageService +import kotlinx.rpc.sample.messages.ReceiveMessagesRequest +import kotlinx.rpc.sample.messages.SendMessageRequest +import kotlinx.rpc.sample.messages.invoke +import kotlinx.rpc.withService +import org.jetbrains.compose.ui.tooling.preview.Preview +import kotlin.time.Clock +import kotlin.time.ExperimentalTime +import kotlin.time.Instant + +@Composable +@Preview +@OptIn(ExperimentalRpcApi::class) +fun App() { + + val grpcClient = remember { + GrpcClient("localhost", 8080) { + usePlaintext() + } + } + + val service = remember { grpcClient.withService() } + + MaterialTheme() { + ChatScreen(service) + } +} + +@Composable +@OptIn(ExperimentalTime::class) +private fun ChatScreen(service: MessageService) { + val scope = rememberCoroutineScope() + + var me by remember { mutableStateOf("me") } + var input by remember { mutableStateOf("") } + val messages = remember { mutableStateListOf() } + + fun sendMessage() { + scope.launch { + val result = service.SendMessage( + SendMessageRequest{ + user = me + text = input + } + ) + if (result.success) { + messages += ChatEntry { + user = me + text = input + tsMillis = Clock.System.now().toEpochMilliseconds() + } + input = "" + } + } + } + + LaunchedEffect(me) { + messages.clear() + val req = ReceiveMessagesRequest { user = me } + service.ReceiveMessages(req).collect { msg -> + messages += msg + } + } + + Column(Modifier + .fillMaxSize() + .windowInsetsPadding(WindowInsets.safeDrawing) + .padding(16.dp) + ) { + OutlinedTextField( + value = me, + onValueChange = { me = it }, + label = { Text("Your name") }, + singleLine = true, + modifier = Modifier.fillMaxWidth() + ) + + Spacer(Modifier.height(12.dp)) + + LazyColumn( + modifier = Modifier.weight(1f).fillMaxWidth(), + contentPadding = PaddingValues(8.dp) + ) { + items(messages) { m -> + MessageBubble(m, me) + } + } + + Spacer(Modifier.height(8.dp)) + + Row(Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp), + verticalAlignment = Alignment.CenterVertically + ) { + OutlinedTextField( + value = input, + onValueChange = { input = it }, + label = { Text("Message") }, + singleLine = true, + modifier = Modifier.weight(1f) + ) + Button(onClick = ::sendMessage) { Text("Send") } + } + } +} + +@Composable +@OptIn(ExperimentalTime::class) +fun MessageBubble( + message: ChatEntry, + me: String, + modifier: Modifier = Modifier +) { + val isMe = message.user == me + + val bubbleColor = if (isMe) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant + val textColor = if (isMe) Color.White else Color.Black + + val local = Instant.fromEpochMilliseconds(message.tsMillis) + .toLocalDateTime(TimeZone.currentSystemDefault()) + // e.g. "14:05" + val timeText = "${local.hour.toString().padStart(2, '0')}:${local.minute.toString().padStart(2, '0')}" + + Row( + modifier = modifier + .fillMaxWidth() + .padding(vertical = 4.dp, horizontal = 8.dp), + horizontalArrangement = if (isMe) Arrangement.End else Arrangement.Start + ) { + Column( + modifier = Modifier + .widthIn(max = 250.dp) + .background(color = bubbleColor, shape = RoundedCornerShape(12.dp)) + .padding(horizontal = 12.dp, vertical = 8.dp), + horizontalAlignment = if (isMe) Alignment.End else Alignment.Start + ) { + if (!isMe) { + Text( + text = message.user, + style = MaterialTheme.typography.bodySmall, // small username line + color = if (isMe) textColor.copy(alpha = 0.9f) else textColor.copy(alpha = 0.9f) + ) + } + Spacer(Modifier.height(2.dp)) + + Text( + text = message.text, + style = MaterialTheme.typography.bodyMedium, + color = textColor + ) + Spacer(Modifier.height(4.dp)) + Text( + text = timeText, + style = MaterialTheme.typography.bodySmall, + color = textColor.copy(alpha = 0.7f) + ) + } + } +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/composeApp/src/iosMain/kotlin/kotlinx/rpc/sample/MainViewController.kt b/samples/grpc-kmp-app/composeApp/src/iosMain/kotlin/kotlinx/rpc/sample/MainViewController.kt new file mode 100644 index 000000000..05a68c3f9 --- /dev/null +++ b/samples/grpc-kmp-app/composeApp/src/iosMain/kotlin/kotlinx/rpc/sample/MainViewController.kt @@ -0,0 +1,5 @@ +package kotlinx.rpc.sample + +import androidx.compose.ui.window.ComposeUIViewController + +fun MainViewController() = ComposeUIViewController { App() } \ No newline at end of file diff --git a/samples/grpc-kmp-app/composeApp/src/jvmMain/kotlin/kotlinx/rpc/sample/main.kt b/samples/grpc-kmp-app/composeApp/src/jvmMain/kotlin/kotlinx/rpc/sample/main.kt new file mode 100644 index 000000000..f2b51a225 --- /dev/null +++ b/samples/grpc-kmp-app/composeApp/src/jvmMain/kotlin/kotlinx/rpc/sample/main.kt @@ -0,0 +1,13 @@ +package kotlinx.rpc.sample + +import androidx.compose.ui.window.Window +import androidx.compose.ui.window.application + +fun main() = application { + Window( + onCloseRequest = ::exitApplication, + title = "grpc-kmp-app", + ) { + App() + } +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/gradle.properties b/samples/grpc-kmp-app/gradle.properties new file mode 100644 index 000000000..534bef60f --- /dev/null +++ b/samples/grpc-kmp-app/gradle.properties @@ -0,0 +1,8 @@ +#Kotlin +kotlin.code.style=official +kotlin.daemon.jvmargs=-Xmx3072M + +#Gradle +org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8 +org.gradle.configuration-cache=true +org.gradle.caching=true \ No newline at end of file diff --git a/samples/grpc-kmp-app/gradle/libs.versions.toml b/samples/grpc-kmp-app/gradle/libs.versions.toml new file mode 100644 index 000000000..772fc90b5 --- /dev/null +++ b/samples/grpc-kmp-app/gradle/libs.versions.toml @@ -0,0 +1,38 @@ +[versions] +androidx-lifecycle = "2.9.3" +composeHotReload = "1.0.0-beta06" +composeMultiplatform = "1.8.2" +junit = "4.13.2" +kotlin = "2.2.10" +kotlinx-coroutines = "1.10.2" +ktor = "3.2.3" +logback = "1.5.18" +# TODO: Set correct version once we publish grpc-common +kotlinx-rpc = "0.10.0-SNAPSHOT" +grpc = "1.75.0" +kotlinx-datatime = "0.7.1" + +[libraries] +kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } +kotlin-testJunit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" } +junit = { module = "junit:junit", version.ref = "junit" } +androidx-lifecycle-viewmodelCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" } +androidx-lifecycle-runtimeCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } +kotlinx-coroutinesSwing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" } +logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } +ktor-serverCore = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" } +ktor-serverNetty = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor" } +ktor-serverTestHost = { module = "io.ktor:ktor-server-test-host-jvm", version.ref = "ktor" } +kotlinx-rpc-grpc-core = { module = "org.jetbrains.kotlinx:kotlinx-rpc-grpc-core", version.ref = "kotlinx-rpc" } +kotlinx-rpc-protobuf-core = { module = "org.jetbrains.kotlinx:kotlinx-rpc-protobuf-core", version.ref = "kotlinx-rpc" } +grpc-netty = { module = "io.grpc:grpc-netty", version.ref = "grpc" } +kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datatime" } + +[plugins] +composeHotReload = { id = "org.jetbrains.compose.hot-reload", version.ref = "composeHotReload" } +composeMultiplatform = { id = "org.jetbrains.compose", version.ref = "composeMultiplatform" } +composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } +kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +ktor = { id = "io.ktor.plugin", version.ref = "ktor" } +kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +kotlinxRpc = { id = "org.jetbrains.kotlinx.rpc.plugin", version.ref = "kotlinx-rpc" } \ No newline at end of file diff --git a/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.jar b/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..1b33c55ba Binary files /dev/null and b/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.jar differ diff --git a/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.properties b/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..d4081da47 --- /dev/null +++ b/samples/grpc-kmp-app/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/samples/grpc-kmp-app/gradlew b/samples/grpc-kmp-app/gradlew new file mode 100755 index 000000000..23d15a936 --- /dev/null +++ b/samples/grpc-kmp-app/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original 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 +# +# https://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. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/samples/grpc-kmp-app/gradlew.bat b/samples/grpc-kmp-app/gradlew.bat new file mode 100644 index 000000000..db3a6ac20 --- /dev/null +++ b/samples/grpc-kmp-app/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/samples/grpc-kmp-app/iosApp/Configuration/Config.xcconfig b/samples/grpc-kmp-app/iosApp/Configuration/Config.xcconfig new file mode 100644 index 000000000..820069a12 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/Configuration/Config.xcconfig @@ -0,0 +1,7 @@ +TEAM_ID= + +PRODUCT_NAME=grpc-kmp-app +PRODUCT_BUNDLE_IDENTIFIER=kotlinx.rpc.sample.grpc-kmp-app$(TEAM_ID) + +CURRENT_PROJECT_VERSION=1 +MARKETING_VERSION=1.0 \ No newline at end of file diff --git a/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.pbxproj b/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.pbxproj new file mode 100644 index 000000000..992aa7127 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.pbxproj @@ -0,0 +1,373 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXFileReference section */ + 87324A31FB2CE5E5D53D0A9D /* grpc-kmp-app.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = grpc-kmp-app.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ + 5B8F6E40E0E5F6335EDAE28B /* Exceptions for "iosApp" folder in "iosApp" target */ = { + isa = PBXFileSystemSynchronizedBuildFileExceptionSet; + membershipExceptions = ( + Info.plist, + ); + target = 50A98D70C2F97ADD2B57337E /* iosApp */; + }; +/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + FA7BAF6E300A9D5651BB6FF6 /* iosApp */ = { + isa = PBXFileSystemSynchronizedRootGroup; + exceptions = ( + 5B8F6E40E0E5F6335EDAE28B /* Exceptions for "iosApp" folder in "iosApp" target */, + ); + path = iosApp; + sourceTree = ""; + }; + FC6695C8246A311C6FD0E469 /* Configuration */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = Configuration; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5FF46A9B0E6A3A64C070127E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + D5D86844EBEE041E58F51522 = { + isa = PBXGroup; + children = ( + FC6695C8246A311C6FD0E469 /* Configuration */, + FA7BAF6E300A9D5651BB6FF6 /* iosApp */, + 1F83CA6A3886799B2208889B /* Products */, + ); + sourceTree = ""; + }; + 1F83CA6A3886799B2208889B /* Products */ = { + isa = PBXGroup; + children = ( + 87324A31FB2CE5E5D53D0A9D /* grpc-kmp-app.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 50A98D70C2F97ADD2B57337E /* iosApp */ = { + isa = PBXNativeTarget; + buildConfigurationList = AA00FA0D8C676DF0719090F3 /* Build configuration list for PBXNativeTarget "iosApp" */; + buildPhases = ( + 9122FD85FF0E5CB8476B4ED8 /* Compile Kotlin Framework */, + 2356BA94CCBF498283626E22 /* Sources */, + 5FF46A9B0E6A3A64C070127E /* Frameworks */, + 0C95B709775B0C1A5A6E122B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + FA7BAF6E300A9D5651BB6FF6 /* iosApp */, + ); + name = iosApp; + packageProductDependencies = ( + ); + productName = iosApp; + productReference = 87324A31FB2CE5E5D53D0A9D /* grpc-kmp-app.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + D6110565AC1905246E5F8B0C /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1620; + LastUpgradeCheck = 1620; + TargetAttributes = { + 50A98D70C2F97ADD2B57337E = { + CreatedOnToolsVersion = 16.2; + }; + }; + }; + buildConfigurationList = C540BF2878CBFF1C82E39B73 /* Build configuration list for PBXProject "iosApp" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = D5D86844EBEE041E58F51522; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 1F83CA6A3886799B2208889B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 50A98D70C2F97ADD2B57337E /* iosApp */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 0C95B709775B0C1A5A6E122B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXShellScriptBuildPhase section */ + 9122FD85FF0E5CB8476B4ED8 /* Compile Kotlin Framework */ = { + isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Compile Kotlin Framework"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if [ \"YES\" = \"$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED\" ]; then\n echo \"Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \\\"YES\\\"\"\n exit 0\nfi\ncd \"$SRCROOT/..\"\n./gradlew :composeApp:embedAndSignAppleFrameworkForXcode\n"; + }; +/* End PBXShellScriptBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 2356BA94CCBF498283626E22 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + F260BFA9510B8C1966ED5B23 /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReferenceAnchor = FC6695C8246A311C6FD0E469 /* Configuration */; + baseConfigurationReferenceRelativePath = Config.xcconfig; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 18.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 8E457A8A54D98C31FC1D7541 /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReferenceAnchor = FC6695C8246A311C6FD0E469 /* Configuration */; + baseConfigurationReferenceRelativePath = Config.xcconfig; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 18.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + A09D1988F7C819CC0BA392DF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = arm64; + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\""; + DEVELOPMENT_TEAM = "${TEAM_ID}"; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = iosApp/Info.plist; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + C56E652383489F2F7970CE8F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = arm64; + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\""; + DEVELOPMENT_TEAM = "${TEAM_ID}"; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = iosApp/Info.plist; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + C540BF2878CBFF1C82E39B73 /* Build configuration list for PBXProject "iosApp" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F260BFA9510B8C1966ED5B23 /* Debug */, + 8E457A8A54D98C31FC1D7541 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + AA00FA0D8C676DF0719090F3 /* Build configuration list for PBXNativeTarget "iosApp" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A09D1988F7C819CC0BA392DF /* Debug */, + C56E652383489F2F7970CE8F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = D6110565AC1905246E5F8B0C /* Project object */; +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..919434a62 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AccentColor.colorset/Contents.json b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 000000000..eb8789700 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/Contents.json b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 000000000..4e8d485bf --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,36 @@ +{ + "images" : [ + { + "filename" : "app-icon-1024.png", + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/app-icon-1024.png b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/app-icon-1024.png new file mode 100644 index 000000000..53fc536fb Binary files /dev/null and b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/AppIcon.appiconset/app-icon-1024.png differ diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/Contents.json b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/Contents.json new file mode 100644 index 000000000..73c00596a --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/samples/grpc-kmp-app/iosApp/iosApp/ContentView.swift b/samples/grpc-kmp-app/iosApp/iosApp/ContentView.swift new file mode 100644 index 000000000..c765ff2a5 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/ContentView.swift @@ -0,0 +1,21 @@ +import UIKit +import SwiftUI +import ComposeApp + +struct ComposeView: UIViewControllerRepresentable { + func makeUIViewController(context: Context) -> UIViewController { + MainViewControllerKt.MainViewController() + } + + func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} +} + +struct ContentView: View { + var body: some View { + ComposeView() + .ignoresSafeArea() + } +} + + + diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Info.plist b/samples/grpc-kmp-app/iosApp/iosApp/Info.plist new file mode 100644 index 000000000..11845e1da --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/Info.plist @@ -0,0 +1,8 @@ + + + + + CADisableMinimumFrameDurationOnPhone + + + diff --git a/samples/grpc-kmp-app/iosApp/iosApp/Preview Content/Preview Assets.xcassets/Contents.json b/samples/grpc-kmp-app/iosApp/iosApp/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 000000000..73c00596a --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/samples/grpc-kmp-app/iosApp/iosApp/iOSApp.swift b/samples/grpc-kmp-app/iosApp/iosApp/iOSApp.swift new file mode 100644 index 000000000..d83dca611 --- /dev/null +++ b/samples/grpc-kmp-app/iosApp/iosApp/iOSApp.swift @@ -0,0 +1,10 @@ +import SwiftUI + +@main +struct iOSApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/server/build.gradle.kts b/samples/grpc-kmp-app/server/build.gradle.kts new file mode 100644 index 000000000..e188e3c64 --- /dev/null +++ b/samples/grpc-kmp-app/server/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + alias(libs.plugins.kotlinJvm) + application +} + +group = "kotlinx.rpc.sample" +version = "1.0.0" +application { + mainClass.set("kotlinx.rpc.sample.ApplicationKt") + + val isDevelopment: Boolean = project.ext.has("development") + applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") +} + +dependencies { + implementation(projects.shared) + implementation(libs.logback) + implementation(libs.kotlinx.rpc.protobuf.core) + implementation(libs.kotlinx.rpc.grpc.core) + implementation(libs.grpc.netty) +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/Application.kt b/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/Application.kt new file mode 100644 index 000000000..d3ea1284e --- /dev/null +++ b/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/Application.kt @@ -0,0 +1,18 @@ +package kotlinx.rpc.sample + +import kotlinx.coroutines.runBlocking +import kotlinx.rpc.grpc.GrpcServer +import kotlinx.rpc.internal.utils.ExperimentalRpcApi +import kotlinx.rpc.registerService +import kotlinx.rpc.sample.messages.MessageService + +@OptIn(ExperimentalRpcApi::class) +fun main(): Unit = runBlocking { + val grpcServer = GrpcServer(8080) { + registerService { MessageServiceImpl() } + } + + grpcServer.start() + println("Server listening on port ${grpcServer.port}") + grpcServer.awaitTermination() +} diff --git a/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/MessageServiceImpl.kt b/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/MessageServiceImpl.kt new file mode 100644 index 000000000..a2ed3a2a4 --- /dev/null +++ b/samples/grpc-kmp-app/server/src/main/kotlin/kotlinx/rpc/sample/MessageServiceImpl.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.sample + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.filter +import kotlinx.rpc.sample.messages.ChatEntry +import kotlinx.rpc.sample.messages.MessageService +import kotlinx.rpc.sample.messages.ReceiveMessagesRequest +import kotlinx.rpc.sample.messages.SendMessageRequest +import kotlinx.rpc.sample.messages.SendMessageResponse +import kotlinx.rpc.sample.messages.invoke + +class MessageServiceImpl: MessageService { + private val bus = MutableSharedFlow(extraBufferCapacity = 64, replay = 0) + + override suspend fun SendMessage(message: SendMessageRequest): SendMessageResponse { + val entry = ChatEntry { + user = message.user + text = message.text + tsMillis = System.currentTimeMillis() + } + val ok = bus.tryEmit(entry) + return SendMessageResponse { success = ok } + } + + override fun ReceiveMessages(message: ReceiveMessagesRequest): Flow { + return bus.asSharedFlow() + .filter { it.user != message.user } + } +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/server/src/main/resources/logback.xml b/samples/grpc-kmp-app/server/src/main/resources/logback.xml new file mode 100644 index 000000000..3e11d7811 --- /dev/null +++ b/samples/grpc-kmp-app/server/src/main/resources/logback.xml @@ -0,0 +1,12 @@ + + + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/samples/grpc-kmp-app/settings.gradle.kts b/samples/grpc-kmp-app/settings.gradle.kts new file mode 100644 index 000000000..254f8e764 --- /dev/null +++ b/samples/grpc-kmp-app/settings.gradle.kts @@ -0,0 +1,45 @@ +rootProject.name = "grpc-kmp-app" +enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") + +pluginManagement { + repositories { + // TODO: Remove once we published grpc-common + mavenLocal { + url = uri("../../build/repo") + } + google { + mavenContent { + includeGroupAndSubgroups("androidx") + includeGroupAndSubgroups("com.android") + includeGroupAndSubgroups("com.google") + } + } + mavenCentral() + gradlePluginPortal() + } +} + +dependencyResolutionManagement { + repositories { + // TODO: Remove once we published grpc-common + mavenLocal { + url = uri("../../build/repo") + } + google { + mavenContent { + includeGroupAndSubgroups("androidx") + includeGroupAndSubgroups("com.android") + includeGroupAndSubgroups("com.google") + } + } + mavenCentral() + } +} + +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" +} + +include(":composeApp") +include(":server") +include(":shared") \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/build.gradle.kts b/samples/grpc-kmp-app/shared/build.gradle.kts new file mode 100644 index 000000000..b6f908821 --- /dev/null +++ b/samples/grpc-kmp-app/shared/build.gradle.kts @@ -0,0 +1,27 @@ + +plugins { + alias(libs.plugins.kotlinMultiplatform) + alias(libs.plugins.kotlinxRpc) +} + +kotlin { + iosArm64() + iosSimulatorArm64() + + jvm() + + sourceSets { + commonMain.dependencies { + implementation(libs.kotlinx.rpc.grpc.core) + implementation(libs.kotlinx.rpc.protobuf.core) + } + commonTest.dependencies { + implementation(libs.kotlin.test) + } + } +} + +rpc { + protoc() +} + diff --git a/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Constants.kt b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Constants.kt new file mode 100644 index 000000000..b60b5b862 --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Constants.kt @@ -0,0 +1,3 @@ +package kotlinx.rpc.sample + +const val SERVER_PORT = 8080 \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Greeting.kt b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Greeting.kt new file mode 100644 index 000000000..d285499dc --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Greeting.kt @@ -0,0 +1,9 @@ +package kotlinx.rpc.sample + +class Greeting { + private val platform = getPlatform() + + fun greet(): String { + return "Hello, ${platform.name}!" + } +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Platform.kt b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Platform.kt new file mode 100644 index 000000000..33d2a0ecd --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/commonMain/kotlin/kotlinx/rpc/sample/Platform.kt @@ -0,0 +1,7 @@ +package kotlinx.rpc.sample + +interface Platform { + val name: String +} + +expect fun getPlatform(): Platform \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/src/commonMain/proto/chat.proto b/samples/grpc-kmp-app/shared/src/commonMain/proto/chat.proto new file mode 100644 index 000000000..0fa5ea2b0 --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/commonMain/proto/chat.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package kotlinx.rpc.sample.messages; + +message ChatEntry { + string user = 1; + string text = 2; + int64 ts_millis = 3; +} + +message SendMessageRequest { + string user = 1; + string text = 2; +} + +message SendMessageResponse { + bool success = 1; +} + +message ReceiveMessagesRequest { + string user = 1; +} + +service MessageService { + rpc SendMessage(SendMessageRequest) returns (SendMessageResponse); + rpc ReceiveMessages(ReceiveMessagesRequest) returns (stream ChatEntry); +} \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/src/iosMain/kotlin/kotlinx/rpc/sample/Platform.ios.kt b/samples/grpc-kmp-app/shared/src/iosMain/kotlin/kotlinx/rpc/sample/Platform.ios.kt new file mode 100644 index 000000000..ca9714954 --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/iosMain/kotlin/kotlinx/rpc/sample/Platform.ios.kt @@ -0,0 +1,9 @@ +package kotlinx.rpc.sample + +import platform.UIKit.UIDevice + +class IOSPlatform: Platform { + override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion +} + +actual fun getPlatform(): Platform = IOSPlatform() \ No newline at end of file diff --git a/samples/grpc-kmp-app/shared/src/jvmMain/kotlin/kotlinx/rpc/sample/Platform.jvm.kt b/samples/grpc-kmp-app/shared/src/jvmMain/kotlin/kotlinx/rpc/sample/Platform.jvm.kt new file mode 100644 index 000000000..80188ffb9 --- /dev/null +++ b/samples/grpc-kmp-app/shared/src/jvmMain/kotlin/kotlinx/rpc/sample/Platform.jvm.kt @@ -0,0 +1,7 @@ +package kotlinx.rpc.sample + +class JVMPlatform: Platform { + override val name: String = "Java ${System.getProperty("java.version")}" +} + +actual fun getPlatform(): Platform = JVMPlatform() \ No newline at end of file