1818import io .netty .bootstrap .Bootstrap ;
1919import io .netty .channel .Channel ;
2020import io .netty .channel .ChannelHandlerContext ;
21- import io .netty .channel .ChannelInboundHandlerAdapter ;
2221import io .netty .channel .ChannelInitializer ;
2322import io .netty .channel .ChannelOption ;
2423import io .netty .channel .EventLoopGroup ;
24+ import io .netty .channel .SimpleChannelInboundHandler ;
2525import io .netty .channel .nio .NioEventLoopGroup ;
2626import io .netty .channel .socket .nio .NioDatagramChannel ;
2727
@@ -45,7 +45,7 @@ public void sendQuery() throws Exception {
4545 .handler (new Initializer ());
4646 Channel ch = b .connect (address ).sync ().channel ();
4747 DnsQuery query = new DnsQuery (15305 );
48- query .addQuestion (new Question ("1.0.0.127 .in-addr.arpa" , DnsEntry .TYPE_PTR ));
48+ query .addQuestion (new Question ("206.226.125.74 .in-addr.arpa" , DnsEntry .TYPE_PTR )); // Google IP
4949 Assert .assertEquals ("Invalid question count, expected 1." , 1 , query .getHeader ().questionCount ());
5050 Assert .assertEquals ("Invalid answer count, expected 0." , 0 , query .getHeader ().answerCount ());
5151 Assert .assertEquals ("Invalid authority resource record count, expected 0." , 0 , query .getHeader ()
@@ -54,7 +54,7 @@ public void sendQuery() throws Exception {
5454 .additionalResourceCount ());
5555 Assert .assertEquals ("Invalid type, should be TYPE_QUERY (0)" , DnsHeader .TYPE_QUERY , query .getHeader ()
5656 .getType ());
57- ch .write (query ).sync ();
57+ ch .writeAndFlush (query ).sync ();
5858 if (!ch .closeFuture ().await (5000 )) {
5959 System .err .println ("DNS request timed out." );
6060 }
@@ -72,29 +72,26 @@ protected void initChannel(NioDatagramChannel ch) throws Exception {
7272 }
7373 }
7474
75- class Handler extends ChannelInboundHandlerAdapter {
75+ class Handler extends SimpleChannelInboundHandler < DnsResponse > {
7676
7777 @ Override
78- public void channelRead (ChannelHandlerContext ctx , Object msg ) throws Exception {
78+ public void channelRead0 (ChannelHandlerContext ctx , DnsResponse response ) throws Exception {
7979 try {
80- if (msg instanceof DnsResponse ) {
81- DnsResponse response = (DnsResponse ) msg ;
82- DnsResponseHeader header = response .getHeader ();
83- Assert .assertEquals ("Invalid response code, expected TYPE_RESPONSE (1)." , DnsHeader .TYPE_RESPONSE ,
84- header .getType ());
85- Assert .assertFalse ("Server response was truncated." , header .isTruncated ());
86- Assert .assertTrue ("Inconsistency between recursion" + "desirability and availability." ,
87- header .isRecursionDesired () == header .isRecursionAvailable ());
88- Assert .assertEquals ("Invalid ID returned from server." , 15305 , response .getHeader ().getId ());
89- Assert .assertEquals ("Question count in response not 1." , 1 , response .getHeader ().questionCount ());
90- Assert .assertTrue ("Server didn't send any resources." , response .getHeader ().answerCount ()
91- + response .getHeader ().authorityResourceCount ()
92- + response .getHeader ().additionalResourceCount () > 0 );
93- List <Resource > answers = response .getAnswers ();
94- for (Resource answer : answers ) {
95- if (answer .type () == DnsEntry .TYPE_PTR ) {
96- System .out .println (DnsResponseDecoder .readName (answer .content ()));
97- }
80+ DnsResponseHeader header = response .getHeader ();
81+ Assert .assertEquals ("Invalid response code, expected TYPE_RESPONSE (1)." , DnsHeader .TYPE_RESPONSE ,
82+ header .getType ());
83+ Assert .assertFalse ("Server response was truncated." , header .isTruncated ());
84+ Assert .assertTrue ("Inconsistency between recursion" + "desirability and availability." ,
85+ header .isRecursionDesired () == header .isRecursionAvailable ());
86+ Assert .assertEquals ("Invalid ID returned from server." , 15305 , response .getHeader ().getId ());
87+ Assert .assertEquals ("Question count in response not 1." , 1 , response .getHeader ().questionCount ());
88+ Assert .assertTrue ("Server didn't send any resources." , response .getHeader ().answerCount ()
89+ + response .getHeader ().authorityResourceCount ()
90+ + response .getHeader ().additionalResourceCount () > 0 );
91+ List <Resource > answers = response .getAnswers ();
92+ for (Resource answer : answers ) {
93+ if (answer .type () == DnsEntry .TYPE_PTR ) {
94+ System .out .println (DnsResponseDecoder .readName (answer .content ()));
9895 }
9996 }
10097 } finally {
0 commit comments