create testable LocalHTTPDManager for controlling the webserver

The RxJava tricks were a nightmare...
This commit is contained in:
Hans-Christoph Steiner
2019-05-17 14:36:25 +02:00
parent 5b610798c2
commit 79e7e78e7f
9 changed files with 810 additions and 86 deletions

View File

@@ -63,6 +63,9 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -887,4 +890,26 @@ public final class Utils {
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
swipeLayout.setColorSchemeColors(typedValue.data);
}
public static boolean canConnectToSocket(String host, int port) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 5);
socket.close();
return true;
} catch (IOException e) {
// Could not connect.
return false;
}
}
public static boolean isServerSocketInUse(int port) {
try {
(new ServerSocket(port)).close();
return false;
} catch (IOException e) {
// Could not connect.
return true;
}
}
}