1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.example.http2.helloworld.frame.client;
16
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.channel.SimpleChannelInboundHandler;
19 import io.netty.handler.codec.http2.Http2DataFrame;
20 import io.netty.handler.codec.http2.Http2HeadersFrame;
21 import io.netty.handler.codec.http2.Http2StreamFrame;
22
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25
26
27
28
29
30
31 public final class Http2ClientStreamFrameResponseHandler extends SimpleChannelInboundHandler<Http2StreamFrame> {
32
33 private final CountDownLatch latch = new CountDownLatch(1);
34
35 @Override
36 protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame msg) throws Exception {
37 System.out.println("Received HTTP/2 'stream' frame: " + msg);
38
39
40 if (msg instanceof Http2DataFrame && ((Http2DataFrame) msg).isEndStream()) {
41 latch.countDown();
42 } else if (msg instanceof Http2HeadersFrame && ((Http2HeadersFrame) msg).isEndStream()) {
43 latch.countDown();
44 }
45 }
46
47
48
49
50
51
52 public boolean responseSuccessfullyCompleted() {
53 try {
54 return latch.await(5, TimeUnit.SECONDS);
55 } catch (InterruptedException ie) {
56 System.err.println("Latch exception: " + ie.getMessage());
57 return false;
58 }
59 }
60
61 }