package io.netty.resolver.dns;
import static io.netty.resolver.dns.DnsAddressDecoder.decodeAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.dns.DnsRecordType;
final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
private final DnsCache resolveCache;
DnsAddressResolveContext(DnsNameResolver parent, String hostname, DnsRecord[] additionals,
DnsServerAddressStream nameServerAddrs, DnsCache resolveCache) {
super(parent, hostname, DnsRecord.CLASS_IN, parent.resolveRecordTypes(), additionals, nameServerAddrs);
this.resolveCache = resolveCache;
}
@Override
DnsResolveContext<InetAddress> newResolverContext(DnsNameResolver parent, String hostname,
int dnsClass, DnsRecordType[] expectedTypes,
DnsRecord[] additionals,
DnsServerAddressStream nameServerAddrs) {
return new DnsAddressResolveContext(parent, hostname, additionals, nameServerAddrs, resolveCache);
}
@Override
InetAddress convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
return decodeAddress(record, hostname, parent.isDecodeIdn());
}
@Override
List<InetAddress> filterResults(List<InetAddress> unfiltered) {
final Class<? extends InetAddress> inetAddressType = parent.preferredAddressType().addressType();
final int size = unfiltered.size();
int numExpected = 0;
for (int i = 0; i < size; i++) {
InetAddress address = unfiltered.get(i);
if (inetAddressType.isInstance(address)) {
numExpected++;
}
}
if (numExpected == size || numExpected == 0) {
return unfiltered;
}
List<InetAddress> filtered = new ArrayList<InetAddress>(numExpected);
for (int i = 0; i < size; i++) {
InetAddress address = unfiltered.get(i);
if (inetAddressType.isInstance(address)) {
filtered.add(address);
}
}
return filtered;
}
@Override
void cache(String hostname, DnsRecord[] additionals,
DnsRecord result, InetAddress convertedResult) {
resolveCache.cache(hostname, additionals, convertedResult, result.timeToLive(), parent.ch.eventLoop());
}
@Override
void cache(String hostname, DnsRecord[] additionals, UnknownHostException cause) {
resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
}
}