Skip to content

to build kts #120

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions examples/build.gradle

This file was deleted.

125 changes: 125 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.ofSourceSet
import com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc

val grpcVersion = "1.30.0"
val grpcKotlinVersion = "0.1.3"
val protobufVersion = "3.12.2"
val coroutinesVersion = "1.3.7"

plugins {
application
kotlin("jvm") version "1.3.72"
id("com.google.protobuf") version "0.8.12"
id("org.jlleitschuh.gradle.ktlint") version "9.2.1"
}

repositories {
mavenLocal()
google()
jcenter()
mavenCentral()
}

dependencies {
implementation(kotlin("stdlib"))
implementation("javax.annotation:javax.annotation-api:1.2")
implementation("com.google.protobuf:protobuf-java-util:$protobufVersion")
implementation("io.grpc:grpc-protobuf:$grpcVersion")
implementation("io.grpc:grpc-stub:$grpcVersion")
implementation("io.grpc:grpc-kotlin-stub:$grpcKotlinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
runtimeOnly("io.grpc:grpc-netty-shaded:$grpcVersion")
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
}
id("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpcKotlinVersion"
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins {
id("grpc")
id("grpckt")
}
}
}
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
}

application {
mainClassName = "io.grpc.examples.helloworld.HelloWorldServerKt"
}

tasks.register<JavaExec>("HelloWorldServer") {
dependsOn("classes")
classpath = sourceSets["main"].runtimeClasspath
main = "io.grpc.examples.helloworld.HelloWorldServerKt"
}

tasks.register<JavaExec>("HelloWorldClient") {
dependsOn("classes")
classpath = sourceSets["main"].runtimeClasspath
main = "io.grpc.examples.helloworld.HelloWorldClientKt"
}

tasks.register<JavaExec>("RouteGuideServerKt") {
dependsOn("classes")
classpath = sourceSets["main"].runtimeClasspath
main = "io.grpc.examples.routeguide.RouteGuideServerKt"
}

tasks.register<JavaExec>("RouteGuideClientKt") {
dependsOn("classes")
classpath = sourceSets["main"].runtimeClasspath
main = "io.grpc.examples.routeguide.RouteGuideClientKt"
}

val helloWorldServerStartScripts = tasks.register<CreateStartScripts>("helloWorldServerStartScripts") {
mainClassName = "io.grpc.examples.helloworld.HelloWorldServerKt"
applicationName = "hello-world-server"
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
}

val helloWorldClientStartScripts = tasks.register<CreateStartScripts>("helloWorldClientStartScripts") {
mainClassName = "io.grpc.examples.helloworld.HelloWorldClientKt"
applicationName = "hello-world-client"
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
}

val routeGuideServerStartScripts = tasks.register<CreateStartScripts>("routeGuideServerStartScripts") {
mainClassName = "io.grpc.examples.routeguide.RouteGuideServerKt"
applicationName = "route-guide-server"
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
}

val routeGuideClientStartScripts = tasks.register<CreateStartScripts>("routeGuideClientStartScripts") {
mainClassName = "io.grpc.examples.routeguide.RouteGuideClientKt"
applicationName = "route-guide-client"
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
}

tasks.named("startScripts") {
dependsOn(helloWorldServerStartScripts)
dependsOn(helloWorldClientStartScripts)
dependsOn(routeGuideServerStartScripts)
dependsOn(routeGuideClientStartScripts)
}
1 change: 0 additions & 1 deletion examples/settings.gradle

This file was deleted.

1 change: 1 addition & 0 deletions examples/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "examples"
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,41 @@ package io.grpc.examples.helloworld
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import io.grpc.examples.helloworld.GreeterGrpcKt.GreeterCoroutineStub
import java.io.Closeable
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking
import java.io.Closeable
import java.util.concurrent.TimeUnit

class HelloWorldClient constructor(
private val channel: ManagedChannel
) : Closeable {
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)
class HelloWorldClient constructor(private val channel: ManagedChannel) : Closeable {
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)

suspend fun greet(name: String) = coroutineScope {
val request = HelloRequest.newBuilder().setName(name).build()
val response = async { stub.sayHello(request) }
println("Received: ${response.await().message}")
}
suspend fun greet(name: String) = coroutineScope {
val request = HelloRequest.newBuilder().setName(name).build()
val response = async { stub.sayHello(request) }
println("Received: ${response.await().message}")
}

override fun close() {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
}
override fun close() {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
}
}


/**
* Greeter, uses first argument as name to greet if present;
* greets "world" otherwise.
*/
fun main(args: Array<String>) = runBlocking {
val port = 50051
val port = 50051

val client = HelloWorldClient(
ManagedChannelBuilder.forAddress("localhost", port)
.usePlaintext()
.executor(Dispatchers.Default.asExecutor())
.build())
val client = HelloWorldClient(
ManagedChannelBuilder.forAddress("localhost", port)
.usePlaintext()
.executor(Dispatchers.Default.asExecutor())
.build())

val user = args.singleOrNull() ?: "world"
client.greet(user)
val user = args.singleOrNull() ?: "world"
client.greet(user)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,43 @@ package io.grpc.examples.helloworld
import io.grpc.Server
import io.grpc.ServerBuilder

class HelloWorldServer constructor(
private val port: Int
) {
val server: Server = ServerBuilder
.forPort(port)
.addService(HelloWorldService())
.build()

fun start() {
server.start()
println("Server started, listening on $port")
Runtime.getRuntime().addShutdownHook(
Thread {
println("*** shutting down gRPC server since JVM is shutting down")
[email protected]()
println("*** server shut down")
}
)
}

private fun stop() {
server.shutdown()
}

fun blockUntilShutdown() {
server.awaitTermination()
}

private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
override suspend fun sayHello(request: HelloRequest) = HelloReply
.newBuilder()
.setMessage("Hello ${request.name}")
.build()
}
class HelloWorldServer constructor(private val port: Int) {
val server: Server = ServerBuilder
.forPort(port)
.addService(HelloWorldService())
.build()

fun start() {
server.start()
println("Server started, listening on $port")
Runtime.getRuntime().addShutdownHook(
Thread {
println("*** shutting down gRPC server since JVM is shutting down")
[email protected]()
println("*** server shut down")
}
)
}

private fun stop() {
server.shutdown()
}

fun blockUntilShutdown() {
server.awaitTermination()
}

private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
override suspend fun sayHello(request: HelloRequest) = HelloReply
.newBuilder()
.setMessage("Hello ${request.name}")
.build()
}
}

fun main() {
val port = 50051
val server = HelloWorldServer(port)
server.start()
server.blockUntilShutdown()
val port = 50051
val server = HelloWorldServer(port)
server.start()
server.blockUntilShutdown()
}
Loading