-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
87 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import android.content.Context; | ||
import android.os.SystemClock; | ||
import android.util.Log; | ||
|
||
import com.danikula.videocache.file.DiskUsage; | ||
import com.danikula.videocache.file.FileNameGenerator; | ||
|
@@ -12,6 +11,9 @@ | |
import com.danikula.videocache.sourcestorage.SourceInfoStorage; | ||
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
@@ -33,7 +35,6 @@ | |
|
||
import static com.danikula.videocache.Preconditions.checkAllNotNull; | ||
import static com.danikula.videocache.Preconditions.checkNotNull; | ||
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
/** | ||
|
@@ -57,6 +58,8 @@ | |
*/ | ||
public class HttpProxyCacheServer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger("HttpProxyCacheServer"); | ||
|
||
private static final String PROXY_HOST = "127.0.0.1"; | ||
private static final String PING_REQUEST = "ping"; | ||
private static final String PING_RESPONSE = "ping ok"; | ||
|
@@ -84,7 +87,7 @@ private HttpProxyCacheServer(Config config) { | |
this.waitConnectionThread = new Thread(new WaitRequestsRunnable(startSignal)); | ||
this.waitConnectionThread.start(); | ||
startSignal.await(); // freeze thread, wait for server starts | ||
Log.i(LOG_TAG, "Proxy cache server started. Ping it..."); | ||
LOG.info("Proxy cache server started. Ping it..."); | ||
makeSureServerWorks(); | ||
} catch (IOException | InterruptedException e) { | ||
socketProcessor.shutdown(); | ||
|
@@ -105,12 +108,12 @@ private void makeSureServerWorks() { | |
} | ||
SystemClock.sleep(delay); | ||
} catch (InterruptedException | ExecutionException | TimeoutException e) { | ||
Log.e(LOG_TAG, "Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e); | ||
LOG.error("Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. ", e); | ||
} | ||
pingAttempts++; | ||
delay *= 2; | ||
} | ||
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " + | ||
LOG.error("Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " + | ||
"If you see this message, please, email me [email protected]"); | ||
shutdown(); | ||
} | ||
|
@@ -124,10 +127,10 @@ private boolean pingServer() throws ProxyCacheException { | |
byte[] response = new byte[expectedResponse.length]; | ||
source.read(response); | ||
boolean pingOk = Arrays.equals(expectedResponse, response); | ||
Log.d(LOG_TAG, "Ping response: `" + new String(response) + "`, pinged? " + pingOk); | ||
LOG.info("Ping response: `" + new String(response) + "`, pinged? " + pingOk); | ||
return pingOk; | ||
} catch (ProxyCacheException e) { | ||
Log.e(LOG_TAG, "Error reading ping response", e); | ||
LOG.error("Error reading ping response", e); | ||
return false; | ||
} finally { | ||
source.close(); | ||
|
@@ -136,7 +139,7 @@ private boolean pingServer() throws ProxyCacheException { | |
|
||
public String getProxyUrl(String url) { | ||
if (!pinged) { | ||
Log.e(LOG_TAG, "Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me [email protected]"); | ||
LOG.error("Proxy server isn't pinged. Caching doesn't work. If you see this message, please, email me [email protected]"); | ||
} | ||
return pinged ? appendToProxyUrl(url) : url; | ||
} | ||
|
@@ -151,7 +154,7 @@ public void registerCacheListener(CacheListener cacheListener, String url) { | |
try { | ||
getClients(url).registerCacheListener(cacheListener); | ||
} catch (ProxyCacheException e) { | ||
Log.d(LOG_TAG, "Error registering cache listener", e); | ||
LOG.warn("Error registering cache listener", e); | ||
} | ||
} | ||
} | ||
|
@@ -162,7 +165,7 @@ public void unregisterCacheListener(CacheListener cacheListener, String url) { | |
try { | ||
getClients(url).unregisterCacheListener(cacheListener); | ||
} catch (ProxyCacheException e) { | ||
Log.d(LOG_TAG, "Error registering cache listener", e); | ||
LOG.warn("Error registering cache listener", e); | ||
} | ||
} | ||
} | ||
|
@@ -191,7 +194,7 @@ public boolean isCached(String url) { | |
} | ||
|
||
public void shutdown() { | ||
Log.i(LOG_TAG, "Shutdown proxy server"); | ||
LOG.info("Shutdown proxy server"); | ||
|
||
shutdownClients(); | ||
|
||
|
@@ -220,7 +223,7 @@ private void waitForRequest() { | |
try { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
Socket socket = serverSocket.accept(); | ||
Log.d(LOG_TAG, "Accept new socket " + socket); | ||
LOG.debug("Accept new socket " + socket); | ||
socketProcessor.submit(new SocketProcessorRunnable(socket)); | ||
} | ||
} catch (IOException e) { | ||
|
@@ -231,7 +234,7 @@ private void waitForRequest() { | |
private void processSocket(Socket socket) { | ||
try { | ||
GetRequest request = GetRequest.read(socket.getInputStream()); | ||
Log.i(LOG_TAG, "Request to cache proxy:" + request); | ||
LOG.debug("Request to cache proxy:" + request); | ||
String url = ProxyCacheUtils.decode(request.uri); | ||
if (PING_REQUEST.equals(url)) { | ||
responseToPing(socket); | ||
|
@@ -242,12 +245,12 @@ private void processSocket(Socket socket) { | |
} catch (SocketException e) { | ||
// There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458 | ||
// So just to prevent log flooding don't log stacktrace | ||
Log.d(LOG_TAG, "Closing socket… Socket is closed by client."); | ||
LOG.debug("Closing socket… Socket is closed by client."); | ||
} catch (ProxyCacheException | IOException e) { | ||
onError(new ProxyCacheException("Error processing request", e)); | ||
} finally { | ||
releaseSocket(socket); | ||
Log.d(LOG_TAG, "Opened connections: " + getClientsCount()); | ||
LOG.debug("Opened connections: " + getClientsCount()); | ||
} | ||
} | ||
|
||
|
@@ -292,7 +295,7 @@ private void closeSocketInput(Socket socket) { | |
} catch (SocketException e) { | ||
// There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458 | ||
// So just to prevent log flooding don't log stacktrace | ||
Log.d(LOG_TAG, "Releasing input stream… Socket is closed by client."); | ||
LOG.debug("Releasing input stream… Socket is closed by client."); | ||
} catch (IOException e) { | ||
onError(new ProxyCacheException("Error closing socket input stream", e)); | ||
} | ||
|
@@ -319,7 +322,7 @@ private void closeSocket(Socket socket) { | |
} | ||
|
||
private void onError(Throwable e) { | ||
Log.e(LOG_TAG, "HttpProxyCacheServer error", e); | ||
LOG.error("HttpProxyCacheServer error", e); | ||
} | ||
|
||
private final class WaitRequestsRunnable implements Runnable { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.