Example usage for io.netty.handler.codec.dns DnsSection QUESTION

List of usage examples for io.netty.handler.codec.dns DnsSection QUESTION

Introduction

In this page you can find the example usage for io.netty.handler.codec.dns DnsSection QUESTION.

Prototype

DnsSection QUESTION

To view the source code for io.netty.handler.codec.dns DnsSection QUESTION.

Click Source Link

Document

The section that contains DnsQuestion s.

Usage

From source file:io.vertx.core.dns.impl.DnsClientImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
private <T> void lookup(String name, Future<List<T>> result, DnsRecordType... types) {
    Objects.requireNonNull(name, "no null name accepted");
    bootstrap.connect(dnsServer).addListener(new RetryChannelFutureListener(result) {
        @Override/*from  w w w . j  a  v a  2 s. c om*/
        public void onSuccess(ChannelFuture future) throws Exception {
            DatagramDnsQuery query = new DatagramDnsQuery(null, dnsServer,
                    ThreadLocalRandom.current().nextInt());
            for (DnsRecordType type : types) {
                query.addRecord(DnsSection.QUESTION, new DefaultDnsQuestion(name, type, DnsRecord.CLASS_IN));
            }
            future.channel().writeAndFlush(query).addListener(new RetryChannelFutureListener(result) {
                @Override
                public void onSuccess(ChannelFuture future) throws Exception {
                    future.channel().pipeline().addLast(new SimpleChannelInboundHandler<DnsResponse>() {
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, DnsResponse msg)
                                throws Exception {
                            DnsResponseCode code = DnsResponseCode.valueOf(msg.code().intValue());

                            if (code == DnsResponseCode.NOERROR) {

                                int count = msg.count(DnsSection.ANSWER);

                                List<T> records = new ArrayList<>(count);
                                for (int idx = 0; idx < count; idx++) {
                                    DnsRecord a = msg.recordAt(DnsSection.ANSWER, idx);
                                    T record = RecordDecoder.decode(a);
                                    if (isRequestedType(a.type(), types)) {
                                        records.add(record);
                                    }
                                }

                                if (records.size() > 0 && (records.get(0) instanceof MxRecordImpl
                                        || records.get(0) instanceof SrvRecordImpl)) {
                                    Collections.sort((List) records);
                                }

                                setResult(result, records);
                            } else {
                                setFailure(result, new DnsException(code));
                            }
                            ctx.close();
                        }

                        private boolean isRequestedType(DnsRecordType dnsRecordType, DnsRecordType[] types) {
                            for (DnsRecordType t : types) {
                                if (t.equals(dnsRecordType)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                                throws Exception {
                            setFailure(result, cause);
                            ctx.close();
                        }
                    });
                }
            });
        }
    });
}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

void query() {
    final DnsQuestion question = question();
    final InetSocketAddress nameServerAddr = nameServerAddr();
    final DatagramDnsQuery query = new DatagramDnsQuery(null, nameServerAddr, id);

    query.setRecursionDesired(recursionDesired);

    query.addRecord(DnsSection.QUESTION, question);

    for (DnsRecord record : additional) {
        query.addRecord(DnsSection.ADDITIONAL, record);
    }//w w w. j  a v  a 2  s  .co m
    if (optResource != null) {
        query.addRecord(DnsSection.ADDITIONAL, optResource);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("{} WRITE: [{}: {}], {}", parent.ch, id, nameServerAddr, question);
    }

    sendQuery(query);
}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

void finish(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
    final DnsResponse res = envelope.content();
    if (res.count(DnsSection.QUESTION) != 1) {
        logger.warn("Received a DNS response with invalid number of questions: {}", envelope);
        return;/*from ww w  . j av  a 2  s  .c  o  m*/
    }

    if (!question().equals(res.recordAt(DnsSection.QUESTION))) {
        logger.warn("Received a mismatching DNS response: {}", envelope);
        return;
    }

    setSuccess(envelope);
}