Package io.netty.handler.codec.dns

Examples of io.netty.handler.codec.dns.DnsResponse


    private final class DnsResponseHandler extends ChannelHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            try {
                final DnsResponse res = (DnsResponse) msg;
                final int queryId = res.header().id();

                if (logger.isDebugEnabled()) {
                    logger.debug("{} RECEIVED: [{}: {}], {}", ch, queryId, res.sender(), res);
                }

                final DnsQueryContext qCtx = promises.get(queryId);

                if (qCtx == null) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Received a DNS response with an unknown ID: {}", queryId);
                    }
                    return;
                }

                final List<DnsQuestion> questions = res.questions();
                if (questions.size() != 1) {
                    logger.warn("Received a DNS response with invalid number of questions: {}", res);
                    return;
                }

                final DnsQuestion q = qCtx.question();
                if (!q.equals(questions.get(0))) {
                    logger.warn("Received a mismatching DNS response: {}", res);
                    return;
                }

                // Cancel the timeout task.
                final ScheduledFuture<?> timeoutFuture = qCtx.timeoutFuture();
                if (timeoutFuture != null) {
                    timeoutFuture.cancel(false);
                }

                if (res.header().responseCode() == DnsResponseCode.NOERROR) {
                    cache(q, res);
                    promises.set(queryId, null);

                    Promise<DnsResponse> qPromise = qCtx.promise();
                    if (qPromise.setUncancellable()) {
                        qPromise.setSuccess(res.retain());
                    }
                } else {
                    qCtx.retry(res.sender(),
                               "response code: " + res.header().responseCode() +
                               " with " + res.answers().size() + " answer(s) and " +
                               res.authorityResources().size() + " authority resource(s)");
                }
            } finally {
                ReferenceCountUtil.safeRelease(msg);
            }
        }
View Full Code Here


            this.cause = cause;
            response = null;
        }

        void release() {
            DnsResponse response = this.response;
            if (response != null) {
                ReferenceCountUtil.safeRelease(response);
            }

            ScheduledFuture<?> expirationFuture = this.expirationFuture;
View Full Code Here

            queryMx(futures, name);
        }

        for (Entry<String, Future<DnsResponse>> e: futures.entrySet()) {
            String hostname = e.getKey();
            DnsResponse response = e.getValue().sync().getNow();

            assertThat(response.header().responseCode(), is(DnsResponseCode.NOERROR));
            List<DnsResource> mxList = new ArrayList<DnsResource>();
            for (DnsResource r: response.answers()) {
                if (r.type() == DnsType.MX) {
                    mxList.add(r);
                }
            }

            assertThat(mxList.size(), is(greaterThan(0)));
            StringBuilder buf = new StringBuilder();
            for (DnsResource r: mxList) {
                buf.append(StringUtil.NEWLINE);
                buf.append('\t');
                buf.append(r.name());
                buf.append(' ');
                buf.append(r.type());
                buf.append(' ');
                buf.append(r.content().readUnsignedShort());
                buf.append(' ');
                buf.append(DnsNameResolverContext.decodeDomainName(r.content()));
            }

            logger.info("{} has the following MX records:{}", hostname, buf);
            response.release();
        }
    }
View Full Code Here

TOP

Related Classes of io.netty.handler.codec.dns.DnsResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.