private HttpProxyCacheServer(Config config) { this.config = checkNotNull(config); try { InetAddress inetAddress = InetAddress.getByName(PROXY_HOST); this.serverSocket = new ServerSocket(0, 8, inetAddress); this.port = serverSocket.getLocalPort(); IgnoreHostProxySelector.install(PROXY_HOST, port); CountDownLatch startSignal = new CountDownLatch(1); this.waitConnectionThread = new Thread(new WaitRequestsRunnable(startSignal)); this.waitConnectionThread.start(); startSignal.await(); // freeze thread, wait for server starts this.pinger = new Pinger(PROXY_HOST, port); HttpProxyCacheDebuger.printfLog("Proxy cache server started. Is it alive? " + isAlive()); } catch (IOException | InterruptedException e) { socketProcessor.shutdown(); throw new IllegalStateException("Error starting local proxy server", e); } }
/** * Returns url that wrap original url and should be used for client (MediaPlayer, ExoPlayer, etc). * <p> * If parameter {@code allowCachedFileUri} is {@code true} and file for this url is fully cached * (it means method {@link #isCached(String)} returns {@code true}) then file:// uri to cached file will be returned. * * @param url a url to file that should be cached. * @param allowCachedFileUri {@code true} if allow to return file:// uri if url is fully cached * @return a wrapped by proxy url if file is not fully cached or url pointed to cache file otherwise (if {@code allowCachedFileUri} is {@code true}). */ public String getProxyUrl(String url, boolean allowCachedFileUri) { if (allowCachedFileUri && isCached(url)) { File cacheFile = getCacheFile(url); touchFileSafely(cacheFile); return Uri.fromFile(cacheFile).toString(); } return isAlive() ? appendToProxyUrl(url) : url; }