1
1
package org.athenian.kotlin_helloworld.withCR
2
2
3
+ import io.grpc.ConnectivityState
3
4
import io.grpc.ManagedChannel
4
5
import io.grpc.ManagedChannelBuilder
5
- import kotlinx.coroutines.coroutineScope
6
6
import kotlinx.coroutines.delay
7
7
import kotlinx.coroutines.flow.flow
8
8
import kotlinx.coroutines.runBlocking
@@ -14,6 +14,7 @@ import org.athenian.kotlin_helloworld.msgs.Msgs.helloRequest
14
14
import java.io.Closeable
15
15
import java.util.concurrent.TimeUnit
16
16
import kotlin.random.Random
17
+ import kotlin.time.Duration.Companion.milliseconds
17
18
18
19
// https://github.com/GoogleCloudPlatform/kotlin-samples/blob/master/run/grpc-hello-world-streaming/src/main/kotlin/io/grpc/examples/helloworld/HelloWorldClient.kt
19
20
@@ -23,52 +24,56 @@ class HelloWorldClient internal constructor(private val channel: ManagedChannel)
23
24
24
25
private val stub = GreeterCoroutineStub (channel)
25
26
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 ) } " ) }
31
32
}
33
+ }
32
34
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)
41
47
}
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
+ }
45
52
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
+ }
54
60
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())
65
69
}
66
- val replies = stub.sayHelloWithManyRequestsAndReplies(requests)
67
- replies.collect {
68
- delay(Random .nextLong(1_000 ))
69
- println (" sayHelloWithManyRequestsAndReplies() response: ${it.toDataClass()} " )
70
70
}
71
+ val replies = stub.sayHelloWithManyRequestsAndReplies(requests)
72
+ replies.collect {
73
+ delay(Random .nextLong(1_000 ).milliseconds)
74
+ println (" sayHelloWithManyRequestsAndReplies() response: ${it.toDataClass()} " )
71
75
}
76
+ }
72
77
73
78
override fun close () {
74
79
channel.shutdown().awaitTermination(5 , TimeUnit .SECONDS )
@@ -79,17 +84,17 @@ class HelloWorldClient internal constructor(private val channel: ManagedChannel)
79
84
fun main (args : Array <String >) {
80
85
val name = if (args.isNotEmpty()) args[0 ] else " world"
81
86
82
- HelloWorldClient ( " localhost " )
83
- .use { client ->
84
- runBlocking {
87
+ runBlocking {
88
+ HelloWorldClient ( " localhost " )
89
+ .use { client ->
85
90
with (client) {
86
91
sayHello(name)
87
92
sayHelloWithManyRequests(name)
88
93
sayHelloWithManyReplies(name)
89
94
sayHelloWithManyRequestsAndReplies(name)
90
95
}
91
96
}
92
- }
97
+ }
93
98
}
94
99
}
95
100
}
0 commit comments