package com.danikula.videocache; import java.io.IOException; import java.io.OutputStream; import java.net.Proxy; import java.net.ProxySelector; import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* access modifiers changed from: package-private */ /* compiled from: Pinger */ public class m { /* renamed from: d reason: collision with root package name */ private static final String f1002d = "ping"; /* renamed from: e reason: collision with root package name */ private static final String f1003e = "ping ok"; private final ExecutorService a = Executors.newSingleThreadExecutor(); /* renamed from: b reason: collision with root package name */ private final String f1004b; /* renamed from: c reason: collision with root package name */ private final int f1005c; /* compiled from: Pinger */ private class b implements Callable<Boolean> { private b() { } @Override // java.util.concurrent.Callable public Boolean call() throws Exception { return Boolean.valueOf(m.this.c()); } } m(String str, int i) { this.f1004b = (String) n.a(str); this.f1005c = i; } private String b() { return String.format(Locale.US, "http://%s:%d/%s", this.f1004b, Integer.valueOf(this.f1005c), f1002d); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private boolean c() throws ProxyCacheException { k kVar = new k(b()); try { byte[] bytes = f1003e.getBytes(); kVar.a(0); byte[] bArr = new byte[bytes.length]; kVar.read(bArr); boolean equals = Arrays.equals(bytes, bArr); h.b("Ping response: `" + new String(bArr) + "`, pinged? " + equals); return equals; } catch (ProxyCacheException e2) { h.a("Error reading ping response", e2); return false; } finally { kVar.close(); } } /* access modifiers changed from: package-private */ public boolean a(int i, int i2) { n.a(i >= 1); n.a(i2 > 0); int i3 = i2; int i4 = 0; while (i4 < i) { try { if (((Boolean) this.a.submit(new b()).get((long) i3, TimeUnit.MILLISECONDS)).booleanValue()) { return true; } i4++; i3 *= 2; } catch (TimeoutException unused) { h.c("Error pinging server (attempt: " + i4 + ", timeout: " + i3 + "). "); } catch (InterruptedException | ExecutionException e2) { h.a("Error pinging server due to unexpected error", e2); } } String format = String.format(Locale.US, "Error pinging server (attempts: %d, max timeout: %d). If you see this message, please, report at https://github.com/danikula/AndroidVideoCache/issues/134. Default proxies are: %s", Integer.valueOf(i4), Integer.valueOf(i3 / 2), a()); h.a(format, new ProxyCacheException(format)); return false; } private List<Proxy> a() { try { return ProxySelector.getDefault().select(new URI(b())); } catch (URISyntaxException e2) { throw new IllegalStateException(e2); } } /* access modifiers changed from: package-private */ public boolean a(String str) { return f1002d.equals(str); } /* access modifiers changed from: package-private */ public void a(Socket socket) throws IOException { OutputStream outputStream = socket.getOutputStream(); outputStream.write("HTTP/1.1 200 OK\n\n".getBytes()); outputStream.write(f1003e.getBytes()); } }