Skip to content

Commit 1d5139b

Browse files
committed
Update jars
1 parent e90d7e7 commit 1d5139b

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

src/main/kotlin/org/athenian/kotlin_helloworld/withCR/HelloWorldClient.kt

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.athenian.kotlin_helloworld.withCR
22

3+
import io.grpc.ConnectivityState
34
import io.grpc.ManagedChannel
45
import io.grpc.ManagedChannelBuilder
5-
import kotlinx.coroutines.coroutineScope
66
import kotlinx.coroutines.delay
77
import kotlinx.coroutines.flow.flow
88
import kotlinx.coroutines.runBlocking
@@ -14,6 +14,7 @@ import org.athenian.kotlin_helloworld.msgs.Msgs.helloRequest
1414
import java.io.Closeable
1515
import java.util.concurrent.TimeUnit
1616
import kotlin.random.Random
17+
import kotlin.time.Duration.Companion.milliseconds
1718

1819
// https://github.com/GoogleCloudPlatform/kotlin-samples/blob/master/run/grpc-hello-world-streaming/src/main/kotlin/io/grpc/examples/helloworld/HelloWorldClient.kt
1920

@@ -23,52 +24,56 @@ class HelloWorldClient internal constructor(private val channel: ManagedChannel)
2324

2425
private val stub = GreeterCoroutineStub(channel)
2526

26-
suspend fun sayHello(name: String) =
27-
coroutineScope {
28-
val request = helloRequest { this.name = name }
29-
val response = stub.sayHello(request)
30-
println("sayHello response: ${response.message}")
27+
init {
28+
with(channel) {
29+
notifyWhenStateChanged(ConnectivityState.CONNECTING) { println("Connecting: ${getState(false)}") }
30+
notifyWhenStateChanged(ConnectivityState.READY) { println("Ready: ${getState(false)}") }
31+
notifyWhenStateChanged(ConnectivityState.IDLE) { println("Idle: ${getState(false)}") }
3132
}
33+
}
3234

33-
suspend fun sayHelloWithManyRequests(name: String) =
34-
coroutineScope {
35-
val requests =
36-
flow {
37-
repeat(5) {
38-
val request = helloRequest { this.name = "$name-$it" }
39-
emit(request)
40-
}
35+
suspend fun sayHello(name: String) {
36+
val request = helloRequest { this.name = name }
37+
val response = stub.sayHello(request)
38+
println("sayHello response: ${response.message}")
39+
}
40+
41+
suspend fun sayHelloWithManyRequests(name: String) {
42+
val requests =
43+
flow {
44+
repeat(5) {
45+
val request = helloRequest { this.name = "$name-$it" }
46+
emit(request)
4147
}
42-
val response = stub.sayHelloWithManyRequests(requests)
43-
println("sayHelloWithManyRequests() response: ${response.message}")
44-
}
48+
}
49+
val response = stub.sayHelloWithManyRequests(requests)
50+
println("sayHelloWithManyRequests() response: ${response.message}")
51+
}
4552

46-
suspend fun sayHelloWithManyReplies(name: String) =
47-
coroutineScope {
48-
val request = helloRequest { this.name = name }
49-
val replies = stub.sayHelloWithManyReplies(request)
50-
println("sayHelloWithManyReplies() replies:")
51-
replies.collect { reply -> println(reply.message) }
52-
println()
53-
}
53+
suspend fun sayHelloWithManyReplies(name: String) {
54+
val request = helloRequest { this.name = name }
55+
val replies = stub.sayHelloWithManyReplies(request)
56+
println("sayHelloWithManyReplies() replies:")
57+
replies.collect { reply -> println(reply.message) }
58+
println()
59+
}
5460

55-
suspend fun sayHelloWithManyRequestsAndReplies(name: String) =
56-
coroutineScope {
57-
val requests =
58-
flow {
59-
repeat(5) {
60-
delay(Random.nextLong(1_000))
61-
val request = HelloRequest(name = "$name-$it")
62-
println("sayHelloWithManyRequestsAndReplies() request: $request")
63-
emit(request.toProto())
64-
}
61+
suspend fun sayHelloWithManyRequestsAndReplies(name: String) {
62+
val requests =
63+
flow {
64+
repeat(5) {
65+
delay(Random.nextLong(1_000).milliseconds)
66+
val request = HelloRequest(name = "$name-$it")
67+
println("sayHelloWithManyRequestsAndReplies() request: $request")
68+
emit(request.toProto())
6569
}
66-
val replies = stub.sayHelloWithManyRequestsAndReplies(requests)
67-
replies.collect {
68-
delay(Random.nextLong(1_000))
69-
println("sayHelloWithManyRequestsAndReplies() response: ${it.toDataClass()}")
7070
}
71+
val replies = stub.sayHelloWithManyRequestsAndReplies(requests)
72+
replies.collect {
73+
delay(Random.nextLong(1_000).milliseconds)
74+
println("sayHelloWithManyRequestsAndReplies() response: ${it.toDataClass()}")
7175
}
76+
}
7277

7378
override fun close() {
7479
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
@@ -79,17 +84,17 @@ class HelloWorldClient internal constructor(private val channel: ManagedChannel)
7984
fun main(args: Array<String>) {
8085
val name = if (args.isNotEmpty()) args[0] else "world"
8186

82-
HelloWorldClient("localhost")
83-
.use { client ->
84-
runBlocking {
87+
runBlocking {
88+
HelloWorldClient("localhost")
89+
.use { client ->
8590
with(client) {
8691
sayHello(name)
8792
sayHelloWithManyRequests(name)
8893
sayHelloWithManyReplies(name)
8994
sayHelloWithManyRequestsAndReplies(name)
9095
}
9196
}
92-
}
97+
}
9398
}
9499
}
95100
}

src/main/kotlin/org/athenian/kotlin_helloworld/withoutCR/HelloWorldClient.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import java.io.Closeable
1212
import java.util.concurrent.CountDownLatch
1313
import java.util.concurrent.TimeUnit
1414

15-
1615
class HelloWorldClient internal constructor(private val channel: ManagedChannel) : Closeable {
1716
constructor(host: String, port: Int = 50051) :
1817
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build())

0 commit comments

Comments
 (0)