package io.netty.resolver.dns;

import g.a.a.a.a;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.util.concurrent.ScheduledFuture;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes.dex */
public class DefaultDnsCache implements DnsCache {
    private final int maxTtl;
    private final int minTtl;
    private final int negativeTtl;
    private final ConcurrentMap<String, Entries> resolveCache;

    public static final class DefaultDnsCacheEntry implements DnsCacheEntry {
        public static final /* synthetic */ boolean $assertionsDisabled = false;
        private final InetAddress address;
        private final Throwable cause;
        private volatile ScheduledFuture<?> expirationFuture;
        private final String hostname;

        public DefaultDnsCacheEntry(String str, Throwable th) {
            this.hostname = (String) ObjectUtil.checkNotNull(str, "hostname");
            this.cause = (Throwable) ObjectUtil.checkNotNull(th, "cause");
            this.address = null;
        }

        public DefaultDnsCacheEntry(String str, InetAddress inetAddress) {
            this.hostname = (String) ObjectUtil.checkNotNull(str, "hostname");
            this.address = (InetAddress) ObjectUtil.checkNotNull(inetAddress, "address");
            this.cause = null;
        }

        @Override // io.netty.resolver.dns.DnsCacheEntry
        public InetAddress address() {
            return this.address;
        }

        public void cancelExpiration() {
            ScheduledFuture<?> scheduledFuture = this.expirationFuture;
            if (scheduledFuture != null) {
                scheduledFuture.cancel(false);
            }
        }

        @Override // io.netty.resolver.dns.DnsCacheEntry
        public Throwable cause() {
            return this.cause;
        }

        public String hostname() {
            return this.hostname;
        }

        public void scheduleExpiration(EventLoop eventLoop, Runnable runnable, long j2, TimeUnit timeUnit) {
            this.expirationFuture = eventLoop.schedule(runnable, j2, timeUnit);
        }

        public String toString() {
            if (this.cause == null) {
                return this.address.toString();
            }
            return this.hostname + '/' + this.cause;
        }
    }

    public static final class Entries extends AtomicReference<List<DefaultDnsCacheEntry>> {
        public static final /* synthetic */ boolean $assertionsDisabled = false;

        public Entries(DefaultDnsCacheEntry defaultDnsCacheEntry) {
            super(Collections.singletonList(defaultDnsCacheEntry));
        }

        private static void cancelExpiration(List<DefaultDnsCacheEntry> list) {
            int size = list.size();
            for (int i2 = 0; i2 < size; i2++) {
                list.get(i2).cancelExpiration();
            }
        }

        public void add(DefaultDnsCacheEntry defaultDnsCacheEntry) {
            if (defaultDnsCacheEntry.cause() != null) {
                cancelExpiration(getAndSet(Collections.singletonList(defaultDnsCacheEntry)));
                return;
            }
            while (true) {
                List<DefaultDnsCacheEntry> list = get();
                if (!list.isEmpty()) {
                    DefaultDnsCacheEntry defaultDnsCacheEntry2 = list.get(0);
                    if (defaultDnsCacheEntry2.cause() == null) {
                        ArrayList arrayList = new ArrayList(list.size() + 1);
                        arrayList.addAll(list);
                        arrayList.add(defaultDnsCacheEntry);
                        if (compareAndSet(list, arrayList)) {
                            return;
                        }
                    } else if (compareAndSet(list, Collections.singletonList(defaultDnsCacheEntry))) {
                        defaultDnsCacheEntry2.cancelExpiration();
                        return;
                    }
                } else if (compareAndSet(list, Collections.singletonList(defaultDnsCacheEntry))) {
                    return;
                }
            }
        }

        public boolean clearAndCancel() {
            List<DefaultDnsCacheEntry> andSet = getAndSet(Collections.emptyList());
            if (andSet.isEmpty()) {
                return false;
            }
            cancelExpiration(andSet);
            return true;
        }
    }

    public DefaultDnsCache() {
        this(0, Integer.MAX_VALUE, 0);
    }

    public DefaultDnsCache(int i2, int i3, int i4) {
        this.resolveCache = PlatformDependent.newConcurrentHashMap();
        this.minTtl = ObjectUtil.checkPositiveOrZero(i2, "minTtl");
        this.maxTtl = ObjectUtil.checkPositiveOrZero(i3, "maxTtl");
        if (i2 > i3) {
            throw new IllegalArgumentException(a.p("minTtl: ", i2, ", maxTtl: ", i3, " (expected: 0 <= minTtl <= maxTtl)"));
        }
        this.negativeTtl = ObjectUtil.checkPositiveOrZero(i4, "negativeTtl");
    }

    private void cache0(DefaultDnsCacheEntry defaultDnsCacheEntry, int i2, EventLoop eventLoop) {
        if (this.resolveCache.get(defaultDnsCacheEntry.hostname()) == null) {
            Entries entriesPutIfAbsent = this.resolveCache.putIfAbsent(defaultDnsCacheEntry.hostname(), new Entries(defaultDnsCacheEntry));
            if (entriesPutIfAbsent != null) {
                entriesPutIfAbsent.add(defaultDnsCacheEntry);
            }
        }
        scheduleCacheExpiration(defaultDnsCacheEntry, i2, eventLoop);
    }

    private static boolean emptyAdditionals(DnsRecord[] dnsRecordArr) {
        return dnsRecordArr == null || dnsRecordArr.length == 0;
    }

    private void scheduleCacheExpiration(final DefaultDnsCacheEntry defaultDnsCacheEntry, int i2, EventLoop eventLoop) {
        defaultDnsCacheEntry.scheduleExpiration(eventLoop, new Runnable() { // from class: io.netty.resolver.dns.DefaultDnsCache.1
            @Override // java.lang.Runnable
            public void run() {
                Entries entries = (Entries) DefaultDnsCache.this.resolveCache.remove(defaultDnsCacheEntry.hostname);
                if (entries != null) {
                    entries.clearAndCancel();
                }
            }
        }, i2, TimeUnit.SECONDS);
    }

    @Override // io.netty.resolver.dns.DnsCache
    public DnsCacheEntry cache(String str, DnsRecord[] dnsRecordArr, Throwable th, EventLoop eventLoop) {
        ObjectUtil.checkNotNull(str, "hostname");
        ObjectUtil.checkNotNull(th, "cause");
        ObjectUtil.checkNotNull(eventLoop, "loop");
        DefaultDnsCacheEntry defaultDnsCacheEntry = new DefaultDnsCacheEntry(str, th);
        if (this.negativeTtl != 0 && emptyAdditionals(dnsRecordArr)) {
            cache0(defaultDnsCacheEntry, this.negativeTtl, eventLoop);
        }
        return defaultDnsCacheEntry;
    }

    @Override // io.netty.resolver.dns.DnsCache
    public DnsCacheEntry cache(String str, DnsRecord[] dnsRecordArr, InetAddress inetAddress, long j2, EventLoop eventLoop) {
        ObjectUtil.checkNotNull(str, "hostname");
        ObjectUtil.checkNotNull(inetAddress, "address");
        ObjectUtil.checkNotNull(eventLoop, "loop");
        DefaultDnsCacheEntry defaultDnsCacheEntry = new DefaultDnsCacheEntry(str, inetAddress);
        if (this.maxTtl != 0 && emptyAdditionals(dnsRecordArr)) {
            cache0(defaultDnsCacheEntry, Math.max(this.minTtl, (int) Math.min(this.maxTtl, j2)), eventLoop);
        }
        return defaultDnsCacheEntry;
    }

    @Override // io.netty.resolver.dns.DnsCache
    public void clear() {
        while (!this.resolveCache.isEmpty()) {
            Iterator<Map.Entry<String, Entries>> it = this.resolveCache.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Entries> next = it.next();
                it.remove();
                next.getValue().clearAndCancel();
            }
        }
    }

    @Override // io.netty.resolver.dns.DnsCache
    public boolean clear(String str) {
        ObjectUtil.checkNotNull(str, "hostname");
        Entries entriesRemove = this.resolveCache.remove(str);
        return entriesRemove != null && entriesRemove.clearAndCancel();
    }

    @Override // io.netty.resolver.dns.DnsCache
    public List<? extends DnsCacheEntry> get(String str, DnsRecord[] dnsRecordArr) {
        ObjectUtil.checkNotNull(str, "hostname");
        if (!emptyAdditionals(dnsRecordArr)) {
            return Collections.emptyList();
        }
        Entries entries = this.resolveCache.get(str);
        if (entries == null) {
            return null;
        }
        return entries.get();
    }

    public int maxTtl() {
        return this.maxTtl;
    }

    public int minTtl() {
        return this.minTtl;
    }

    public int negativeTtl() {
        return this.negativeTtl;
    }

    public String toString() {
        StringBuilder sbF = a.F("DefaultDnsCache(minTtl=");
        sbF.append(this.minTtl);
        sbF.append(", maxTtl=");
        sbF.append(this.maxTtl);
        sbF.append(", negativeTtl=");
        sbF.append(this.negativeTtl);
        sbF.append(", cached resolved hostname=");
        sbF.append(this.resolveCache.size());
        sbF.append(")");
        return sbF.toString();
    }
}
