package a.c.b.z.g;

import a.c.b.a0.c;
import a.c.b.k;
import a.c.b.s;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeoutException;

public class b {
    private static final String e;
    private static final b f = new b();

    /* renamed from: a */
    private volatile boolean f88a = false;
    private final DelayQueue<d<?>> b = new DelayQueue<>();
    private final Map<Integer, d<?>> c = new HashMap();
    private final Object d = new Object();

    private b() {
        Thread thread = new Thread(new Runnable(k.a()) {
            /* class a.c.b.z.g.$$Lambda$b$m58lPFgEytcwinhpwRTSW9mF23k */
            public final /* synthetic */ c f$1;

            {
                this.f$1 = r2;
            }

            public final void run() {
                b.lambda$m58lPFgEytcwinhpwRTSW9mF23k(b.this, this.f$1);
            }
        }, "req-timeout-task");
        thread.setDaemon(true);
        thread.start();
    }

    /* access modifiers changed from: public */
    private /* synthetic */ void a(c cVar) {
        this.f88a = true;
        while (this.f88a) {
            try {
                d<?> take = this.b.take();
                if (take != null && !take.isCancelled() && !take.isDone()) {
                    synchronized (this.d) {
                        if (this.c.remove(Integer.valueOf(take.b())) != null) {
                            String str = "request " + take.b() + " timeout: " + take.c();
                            cVar.b(e, str);
                            take.a(new TimeoutException(str));
                        }
                    }
                }
            } catch (InterruptedException unused) {
            }
        }
        cVar.a(e, "FutureRequest timeout thread exit.");
    }

    public static b b() {
        return f;
    }

    public void a() {
        synchronized (this.d) {
            for (Map.Entry<Integer, d<?>> entry : this.c.entrySet()) {
                k.a().d(e, "request id: " + entry.getKey() + " need force failed.");
                entry.getValue().a(new IOException("ucar device disconnected, we need force failed"));
            }
            this.c.clear();
        }
        this.b.clear();
    }

    public void a(int i) {
        synchronized (this.d) {
            this.c.remove(Integer.valueOf(i));
        }
    }

    public void a(s sVar) {
        d<?> remove;
        int m = sVar.m();
        synchronized (this.d) {
            remove = this.c.remove(Integer.valueOf(m));
        }
        if (remove != null) {
            remove.a(sVar);
        } else {
            k.a().d(e, "receive timeout or unknown response: " + m);
        }
    }

    public void a(d<?> dVar) {
        if (dVar != null) {
            int b2 = dVar.b();
            synchronized (this.d) {
                if (this.c.containsKey(Integer.valueOf(b2))) {
                    k.a().d(e, "request id: " + b2 + " repeat add.");
                }
                this.c.put(Integer.valueOf(b2), dVar);
            }
            this.b.put(dVar);
        }
    }

    public Map<Integer, d<?>> c() {
        return this.c;
    }

    public void d() {
        this.f88a = false;
    }
}
