Example usage for io.netty.channel AddressedEnvelope sender

List of usage examples for io.netty.channel AddressedEnvelope sender

Introduction

In this page you can find the example usage for io.netty.channel AddressedEnvelope sender.

Prototype

A sender();

Source Link

Document

Returns the address of the sender of this message.

Usage

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

License:Apache License

void onResponse(final DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
    try {// w w w  . j  av  a2 s  .c  o m
        final DnsResponse res = envelope.content();
        final DnsResponseCode code = res.code();
        if (code == DnsResponseCode.NOERROR) {
            final DnsRecordType type = question.type();
            if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
                onResponseAorAAAA(type, question, envelope);
            } else if (type == DnsRecordType.CNAME) {
                onResponseCNAME(question, envelope);
            }
            return;
        }

        if (traceEnabled) {
            addTrace(envelope.sender(), "response code: " + code + " with " + res.count(DnsSection.ANSWER)
                    + " answer(s) and " + res.count(DnsSection.AUTHORITY) + " authority resource(s)");
        }

        // Retry with the next server if the server did not tell us that the domain does not exist.
        if (code != DnsResponseCode.NXDOMAIN) {
            query(nameServerAddrs.next(), question);
        }
    } finally {
        ReferenceCountUtil.safeRelease(envelope);
    }
}

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

License:Apache License

private void onResponseAorAAAA(DnsRecordType qType, DnsQuestion question,
        AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {

    // We often get a bunch of CNAMES as well when we asked for A/AAAA.
    final DnsResponse response = envelope.content();
    final Map<String, String> cnames = buildAliasMap(response);
    final int answerCount = response.count(DnsSection.ANSWER);

    boolean found = false;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.A && type != DnsRecordType.AAAA) {
            continue;
        }/*from  ww  w  .j  a va  2 s  . c  o m*/

        final String qName = question.name().toLowerCase(Locale.US);
        final String rName = r.name().toLowerCase(Locale.US);

        // Make sure the record is for the questioned domain.
        if (!rName.equals(qName)) {
            // Even if the record's name is not exactly same, it might be an alias defined in the CNAME records.
            String resolved = qName;
            do {
                resolved = cnames.get(resolved);
                if (rName.equals(resolved)) {
                    break;
                }
            } while (resolved != null);

            if (resolved == null) {
                continue;
            }
        }

        if (!(r instanceof DnsRawRecord)) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        final int contentLen = content.readableBytes();
        if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
            continue;
        }

        final byte[] addrBytes = new byte[contentLen];
        content.getBytes(content.readerIndex(), addrBytes);

        final InetAddress resolved;
        try {
            resolved = InetAddress.getByAddress(hostname, addrBytes);
        } catch (UnknownHostException e) {
            // Should never reach here.
            throw new Error(e);
        }

        if (resolvedEntries == null) {
            resolvedEntries = new ArrayList<DnsCacheEntry>(8);
        }

        final DnsCacheEntry e = new DnsCacheEntry(hostname, resolved);
        resolveCache.cache(hostname, resolved, r.timeToLive(), parent.ch.eventLoop());
        resolvedEntries.add(e);
        found = true;

        // Note that we do not break from the loop here, so we decode/cache all A/AAAA records.
    }

    if (found) {
        return;
    }

    if (traceEnabled) {
        addTrace(envelope.sender(), "no matching " + qType + " record found");
    }

    // We aked for A/AAAA but we got only CNAME.
    if (!cnames.isEmpty()) {
        onResponseCNAME(question, envelope, cnames, false);
    }
}

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

License:Apache License

private void onResponseCNAME(DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> response,
        Map<String, String> cnames, boolean trace) {

    // Resolve the host name in the question into the real host name.
    final String name = question.name().toLowerCase(Locale.US);
    String resolved = name;/*w ww.  j av a 2s.  co  m*/
    boolean found = false;
    while (!cnames.isEmpty()) { // Do not attempt to call Map.remove() when the Map is empty
        // because it can be Collections.emptyMap()
        // whose remove() throws a UnsupportedOperationException.
        final String next = cnames.remove(resolved);
        if (next != null) {
            found = true;
            resolved = next;
        } else {
            break;
        }
    }

    if (found) {
        followCname(response.sender(), name, resolved);
    } else if (trace && traceEnabled) {
        addTrace(response.sender(), "no matching CNAME record found");
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        delegateDecoder.decode(ctx, (ByteBuf) msg.content(), out);
        byte[] content = (byte[]) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                content, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from w  w w  . j  ava 2  s .  co m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof byte[]) {
        delegateEncoder.encode(ctx, (byte[]) msg.content(), out);
        ByteBuf buf = (ByteBuf) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from w w  w . j  ava  2 s  . c om*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketDelimiterDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressEvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressEvelop);/*from   w  ww.j a va  2 s .com*/
    }

}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);//from  ww  w  .  j  a v  a2s  .  c  om
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof Serializable) {
        Serializable payload = (Serializable) msg.content();
        ByteBuf buf = ctx.alloc().heapBuffer();
        delegateObjectEncoder.encode(ctx, payload, buf);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);//from  ww w. jav a2 s  .c o m
    }

}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketStringDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                payload.toString(charset), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from  www  .j  ava  2  s  .c o m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketStringEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof CharSequence) {
        CharSequence payload = (CharSequence) msg.content();
        if (payload.length() == 0) {
            return;
        }/*from   w w w .  j  av  a2  s .c om*/
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(payload), charset), msg.recipient(),
                msg.sender());
        out.add(addressedEnvelop);
    }
}