@@ -5,11 +5,16 @@ import java.util.concurrent.{CountDownLatch, Executors, TimeUnit}
5
5
6
6
import com .typesafe .scalalogging .LazyLogging
7
7
import io .github .wechaty .grpc .PuppetGrpc
8
- import io .github .wechaty .grpc .puppet .Base
8
+ import io .github .wechaty .grpc .puppet .Contact .ContactPayloadRequest
9
+ import io .github .wechaty .grpc .puppet .{Base , Contact }
9
10
import io .github .wechaty .grpc .puppet .Event .EventRequest
11
+ import io .grpc .stub .ClientCalls .asyncUnaryCall
10
12
import io .grpc .stub .StreamObserver
11
- import io .grpc .{ManagedChannel , ManagedChannelBuilder }
13
+ import io .grpc .{ClientCall , ManagedChannel , ManagedChannelBuilder }
12
14
import wechaty .hostie .PuppetHostie
15
+ import wechaty .puppet .schemas .Contact .ContactPayload
16
+
17
+ import scala .concurrent .{Future , Promise }
13
18
14
19
/**
15
20
*
@@ -23,9 +28,9 @@ trait GrpcSupport {
23
28
private val HEARTBEAT_COUNTER = new AtomicLong ()
24
29
private val HOSTIE_KEEPALIVE_TIMEOUT = 15 * 1000L
25
30
private val DEFAULT_WATCHDOG_TIMEOUT = 60L
26
- protected var grpcClient : PuppetGrpc .PuppetBlockingStub = _
27
- private var eventStream : PuppetGrpc .PuppetStub = _
28
- protected var channel : ManagedChannel = _
31
+ protected var grpcClient : PuppetGrpc .PuppetBlockingStub = _
32
+ protected var asyncGrpcClient : PuppetGrpc .PuppetStub = _
33
+ protected var channel : ManagedChannel = _
29
34
30
35
protected def startGrpc (endpoint : String ): Unit = {
31
36
initChannel(endpoint)
@@ -96,9 +101,9 @@ trait GrpcSupport {
96
101
}
97
102
98
103
private def startStream () {
99
- this .eventStream = PuppetGrpc .newStub(channel)
104
+ this .asyncGrpcClient = PuppetGrpc .newStub(channel)
100
105
val startRequest = EventRequest .newBuilder().build()
101
- this .eventStream .event(startRequest, this )
106
+ this .asyncGrpcClient .event(startRequest, this )
102
107
}
103
108
104
109
protected def stopGrpc (): Unit = {
@@ -120,7 +125,7 @@ trait GrpcSupport {
120
125
private def stopStream (): Unit = {
121
126
try {
122
127
val countDownLatch = new CountDownLatch (1 )
123
- this .eventStream .stop(Base .StopRequest .getDefaultInstance, new StreamObserver [Base .StopResponse ] {
128
+ this .asyncGrpcClient .stop(Base .StopRequest .getDefaultInstance, new StreamObserver [Base .StopResponse ] {
124
129
override def onNext (v : Base .StopResponse ): Unit = {}
125
130
126
131
override def onError (throwable : Throwable ): Unit = {}
@@ -133,4 +138,24 @@ trait GrpcSupport {
133
138
logger.warn(" fail to stop stream {}" , e.getMessage)
134
139
}
135
140
}
141
+
142
+ type ClientCall [ReqT ,RespT ]= (ReqT ,StreamObserver [RespT ])=> Unit
143
+ type ClientCallback [RespT ,T ]= RespT => T
144
+ protected def asyncCall [ReqT ,RespT ](call : ClientCall [ReqT , RespT ], req : ReqT ): Unit = {
145
+ asyncCallback(call,req)(resp=> resp)
146
+ }
147
+ protected def asyncCallback [ReqT ,RespT ,T ](call : ClientCall [ReqT , RespT ], req : ReqT )(callback: ClientCallback [RespT ,T ]): Future [T ]= {
148
+ val promise = Promise [T ]
149
+ call(req,new StreamObserver [RespT ] {
150
+ override def onNext (value : RespT ): Unit = {
151
+ val result = callback(value)
152
+ promise.success(result)
153
+ }
154
+ override def onError (t : Throwable ): Unit = promise.failure(t)
155
+ override def onCompleted (): Unit = {
156
+ if (! promise.isCompleted) promise.failure(new IllegalStateException (" server completed" ))
157
+ }
158
+ })
159
+ promise.future
160
+ }
136
161
}
0 commit comments