海角 v1.5.2版本的 MD5 值为:66a890a3993572bcf201f98e8ab772a0

以下内容为反编译后的 Pinger.java 源代码,内容仅作参考


package com.danikula.videocache;

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;
public class 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 class PingCallable implements Callable<Boolean> {
        private PingCallable() {
        }

        @Override
        public Boolean call() {
            return Boolean.valueOf(Pinger.this.pingServer());
        }
    }

    public Pinger(String str, int i2) {
        this.host = (String) Preconditions.checkNotNull(str);
        this.port = i2;
    }

    private List<Proxy> getDefaultProxies() {
        try {
            return ProxySelector.getDefault().select(new URI(getPingUrl()));
        } catch (URISyntaxException e2) {
            throw new IllegalStateException(e2);
        }
    }

    private String getPingUrl() {
        return String.format(Locale.US, "http://%s:%d/%s", this.host, Integer.valueOf(this.port), PING_REQUEST);
    }

    public boolean pingServer() {
        HttpUrlSource httpUrlSource = new HttpUrlSource(getPingUrl());
        try {
            byte[] bytes = PING_RESPONSE.getBytes();
            httpUrlSource.open(0L);
            byte[] bArr = new byte[bytes.length];
            httpUrlSource.read(bArr);
            boolean equals = Arrays.equals(bytes, bArr);
            HttpProxyCacheDebuger.printfLog("Ping response: `" + new String(bArr) + "`, pinged? " + equals);
            return equals;
        } catch (ProxyCacheException e2) {
            HttpProxyCacheDebuger.printfError("Error reading ping response", e2);
            return false;
        } finally {
            httpUrlSource.close();
        }
    }

    public boolean isPingRequest(String str) {
        return PING_REQUEST.equals(str);
    }

    public boolean ping(int i2, int i3) {
        Preconditions.checkArgument(i2 >= 1);
        Preconditions.checkArgument(i3 > 0);
        int i4 = 0;
        while (i4 < i2) {
            try {
            } catch (InterruptedException e2) {
                e = e2;
                HttpProxyCacheDebuger.printfError("Error pinging server due to unexpected error", e);
            } catch (ExecutionException e3) {
                e = e3;
                HttpProxyCacheDebuger.printfError("Error pinging server due to unexpected error", e);
            } catch (TimeoutException unused) {
                HttpProxyCacheDebuger.printfWarning("Error pinging server (attempt: " + i4 + ", timeout: " + i3 + "). ");
            }
            if (((Boolean) this.pingExecutor.submit(new PingCallable()).get(i3, TimeUnit.MILLISECONDS)).booleanValue()) {
                return true;
            }
            i4++;
            i3 *= 2;
        }
        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), getDefaultProxies());
        HttpProxyCacheDebuger.printfError(format, new ProxyCacheException(format));
        return false;
    }

    public void responseToPing(Socket socket) {
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("HTTP/1.1 200 OK\n\n".getBytes());
        outputStream.write(PING_RESPONSE.getBytes());
    }
}