幻维数码 v1.0.0版本的 MD5 值为:a00cf1c0e4d480351a0fa17a0605cbcd
以下内容为反编译后的 Pinger.java 源代码,内容仅作参考
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.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Pinger {
private static final Logger LOG = LoggerFactory.getLogger("Pinger");
private static final String PING_REQUEST = "ping";
private static final String PING_RESPONSE = "ping ok";
private final String host;
private final ExecutorService pingExecutor = Executors.newSingleThreadExecutor();
private final int port;
public Pinger(String host, int port) {
this.host = (String) Preconditions.checkNotNull(host);
this.port = port;
}
public boolean ping(int maxAttempts, int startTimeout) {
boolean pinged;
Preconditions.checkArgument(maxAttempts >= 1);
Preconditions.checkArgument(startTimeout > 0);
int timeout = startTimeout;
int attempts = 0;
while (attempts < maxAttempts) {
try {
Future<Boolean> pingFuture = this.pingExecutor.submit(new PingCallable());
pinged = pingFuture.get(timeout, TimeUnit.MILLISECONDS).booleanValue();
} catch (InterruptedException e) {
e = e;
LOG.error("Error pinging server due to unexpected error", (Throwable) e);
} catch (ExecutionException e2) {
e = e2;
LOG.error("Error pinging server due to unexpected error", (Throwable) e);
} catch (TimeoutException e3) {
Logger logger = LOG;
logger.warn("Error pinging server (attempt: " + attempts + ", timeout: " + timeout + "). ");
}
if (pinged) {
return true;
}
attempts++;
timeout *= 2;
}
String error = 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(attempts), Integer.valueOf(timeout / 2), getDefaultProxies());
LOG.error(error, (Throwable) new ProxyCacheException(error));
return false;
}
private List<Proxy> getDefaultProxies() {
try {
ProxySelector defaultProxySelector = ProxySelector.getDefault();
return defaultProxySelector.select(new URI(getPingUrl()));
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}
public boolean isPingRequest(String request) {
return PING_REQUEST.equals(request);
}
public void responseToPing(Socket socket) throws IOException {
OutputStream out = socket.getOutputStream();
out.write("HTTP/1.1 200 OK\n\n".getBytes());
out.write(PING_RESPONSE.getBytes());
}
public boolean pingServer() throws ProxyCacheException {
String pingUrl = getPingUrl();
HttpUrlSource source = new HttpUrlSource(pingUrl);
try {
byte[] expectedResponse = PING_RESPONSE.getBytes();
source.open(0L);
byte[] response = new byte[expectedResponse.length];
source.read(response);
boolean pingOk = Arrays.equals(expectedResponse, response);
Logger logger = LOG;
logger.info("Ping response: `" + new String(response) + "`, pinged? " + pingOk);
return pingOk;
} catch (ProxyCacheException e) {
LOG.error("Error reading ping response", (Throwable) e);
return false;
} finally {
source.close();
}
}
private String getPingUrl() {
return String.format(Locale.US, "http://%s:%d/%s", this.host, Integer.valueOf(this.port), PING_REQUEST);
}
private class PingCallable implements Callable<Boolean> {
private PingCallable() {
}
@Override
public Boolean call() throws Exception {
return Boolean.valueOf(Pinger.this.pingServer());
}
}
}