package com.sigmob.sdk.videocache; import android.content.Context; import android.net.Uri; import com.czhj.sdk.logger.SigmobLog; import com.sigmob.sdk.videocache.file.f; import com.sigmob.sdk.videocache.file.g; import com.sigmob.sdk.videocache.sourcestorage.d; import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class h { private static final String a = "127.0.0.1"; private final Object b; private final ExecutorService c; private final Map<String, i> d; private final ServerSocket e; private final int f; private final Thread g; private final e h; private final m i; public static final class a { private static final long a = 536870912; private File b; private com.sigmob.sdk.videocache.file.c c = new f(); private com.sigmob.sdk.videocache.file.a d = new com.sigmob.sdk.videocache.file.h(536870912); private com.sigmob.sdk.videocache.sourcestorage.c e; private com.sigmob.sdk.videocache.headers.b f = new com.sigmob.sdk.videocache.headers.a(); public a(Context context) { this.e = d.a(context); this.b = u.a(context); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private e b() { return new e(this.b, this.c, this.d, this.e, this.f); } public a a(int i) { this.d = new g(i); return this; } public a a(long j) { this.d = new com.sigmob.sdk.videocache.file.h(j); return this; } public a a(com.sigmob.sdk.videocache.file.a aVar) { this.d = (com.sigmob.sdk.videocache.file.a) n.a(aVar); return this; } public a a(com.sigmob.sdk.videocache.file.c cVar) { this.c = (com.sigmob.sdk.videocache.file.c) n.a(cVar); return this; } public a a(com.sigmob.sdk.videocache.headers.b bVar) { this.f = (com.sigmob.sdk.videocache.headers.b) n.a(bVar); return this; } public a a(File file) { this.b = (File) n.a(file); return this; } public h a() { return new h(b()); } } /* access modifiers changed from: private */ public final class b implements Runnable { private final Socket b; public b(Socket socket) { this.b = socket; } public void run() { h.this.a((h) this.b); } } private final class c implements Runnable { private final CountDownLatch b; public c(CountDownLatch countDownLatch) { this.b = countDownLatch; } public void run() { this.b.countDown(); h.this.d(); } } public h(Context context) { this(new a(context).b()); } private h(e eVar) { this.b = new Object(); this.c = Executors.newFixedThreadPool(8); this.d = new ConcurrentHashMap(); this.h = (e) n.a(eVar); try { ServerSocket serverSocket = new ServerSocket(0, 8, InetAddress.getByName(a)); this.e = serverSocket; int localPort = serverSocket.getLocalPort(); this.f = localPort; k.a(a, localPort); CountDownLatch countDownLatch = new CountDownLatch(1); Thread thread = new Thread(new c(countDownLatch)); this.g = thread; thread.start(); countDownLatch.await(); this.i = new m(a, localPort); SigmobLog.i("Proxy cache server started. Is it alive? " + b()); } catch (IOException | InterruptedException e2) { this.c.shutdown(); throw new IllegalStateException("Error starting local proxy server", e2); } } private void a(File file) { try { this.h.c.a(file); } catch (IOException e2) { SigmobLog.e("Error touching file " + file, e2); } } private void a(Throwable th) { SigmobLog.e("HttpProxyCacheServer error", th); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void a(Socket socket) { StringBuilder sb; try { f a2 = f.a(socket.getInputStream()); SigmobLog.d("Request to cache proxy:" + a2); String c2 = q.c(a2.a); if (this.i.a(c2)) { this.i.a(socket); } else { f(c2).a(a2, socket); } b(socket); sb = new StringBuilder(); } catch (SocketException unused) { SigmobLog.d("Closing socket… Socket is closed by client."); b(socket); sb = new StringBuilder(); } catch (p | IOException e2) { a(new p("Error processing request", e2)); b(socket); sb = new StringBuilder(); } catch (Throwable th) { b(socket); SigmobLog.d("Opened connections: " + e()); throw th; } sb.append("Opened connections: "); sb.append(e()); SigmobLog.d(sb.toString()); } private void b(Socket socket) { c(socket); d(socket); e(socket); } private boolean b() { return this.i.a(3, 70); } private void c() { synchronized (this.b) { for (i iVar : this.d.values()) { iVar.a(); } this.d.clear(); } } private void c(Socket socket) { try { if (!socket.isInputShutdown()) { socket.shutdownInput(); } } catch (SocketException unused) { SigmobLog.d("Releasing input stream… Socket is closed by client."); } catch (IOException unused2) { } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void d() { while (!Thread.currentThread().isInterrupted()) { try { Socket accept = this.e.accept(); SigmobLog.d("Accept new socket " + accept); this.c.submit(new b(accept)); } catch (IOException e2) { a(new p("Error during waiting connection", e2)); return; } } } private void d(Socket socket) { try { if (!socket.isOutputShutdown()) { socket.shutdownOutput(); } } catch (IOException unused) { } } private int e() { int i2; synchronized (this.b) { i2 = 0; for (i iVar : this.d.values()) { i2 += iVar.b(); } } return i2; } private String e(String str) { return String.format(Locale.US, "http://%s:%d/%s", a, Integer.valueOf(this.f), q.b(str)); } private void e(Socket socket) { try { if (!socket.isClosed()) { socket.close(); } } catch (IOException unused) { } } private i f(String str) throws p { i iVar; synchronized (this.b) { iVar = this.d.get(str); if (iVar == null) { iVar = new i(str, this.h); this.d.put(str, iVar); } } return iVar; } public String a(String str) { return a(str, true); } public String a(String str, boolean z) { if (!z || !b(str)) { return b() ? e(str) : str; } File c2 = c(str); a(c2); return Uri.fromFile(c2).toString(); } public void a() { SigmobLog.i("Shutdown proxy server"); c(); this.h.d.a(); this.g.interrupt(); try { if (!this.e.isClosed()) { this.e.close(); } } catch (IOException e2) { a(new p("Error shutting down proxy server", e2)); } } public void a(d dVar) { n.a(dVar); synchronized (this.b) { for (i iVar : this.d.values()) { iVar.b(dVar); } } } public void a(d dVar, String str) { n.a(dVar, str); synchronized (this.b) { try { f(str).a(dVar); } catch (p e2) { SigmobLog.w("Error registering cache listener", e2); } } } public void b(d dVar, String str) { n.a(dVar, str); synchronized (this.b) { try { f(str).b(dVar); } catch (p e2) { SigmobLog.w("Error registering cache listener", e2); } } } public boolean b(String str) { n.a(str, "Url can't be null!"); return c(str).exists(); } public File c(String str) { return new File(this.h.a, this.h.b.a(str)); } public void d(String str) { synchronized (this.b) { i iVar = this.d.get(str); if (iVar != null) { iVar.a(); } } } }