aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/Discovery.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/mygram/droid/Discovery.java')
-rw-r--r--app/src/main/java/mygram/droid/Discovery.java66
1 files changed, 65 insertions, 1 deletions
diff --git a/app/src/main/java/mygram/droid/Discovery.java b/app/src/main/java/mygram/droid/Discovery.java
index 1d3e0be..56777f3 100644
--- a/app/src/main/java/mygram/droid/Discovery.java
+++ b/app/src/main/java/mygram/droid/Discovery.java
@@ -12,14 +12,33 @@
* for more details.
*/
-
package mygram.droid;
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+
import org.json.JSONException;
import org.json.JSONObject;
+import java.io.IOException;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
/** Finds Foldergram instances on the LAN. */
+// ponytail: probes only the TV's /24 on the default port; manual URL entry covers proxies and other ports.
final class Discovery {
+ static final int PORT = 4141;
+
private Discovery() {
}
@@ -32,4 +51,49 @@ final class Discovery {
return false;
}
}
+
+ /** The TV's own IPv4 address, or null when offline. */
+ static String localIpv4(Context c) {
+ ConnectivityManager cm = c.getSystemService(ConnectivityManager.class);
+ LinkProperties lp = cm.getLinkProperties(cm.getActiveNetwork());
+ if (lp != null)
+ for (LinkAddress a : lp.getLinkAddresses())
+ if (a.getAddress() instanceof Inet4Address)
+ return a.getAddress().getHostAddress();
+ return null;
+ }
+
+ /** Base URLs of instances answering on the /24 around self. Blocks a few seconds. */
+ static List<String> scan(String self) throws InterruptedException {
+ String prefix = self.substring(0, self.lastIndexOf('.') + 1);
+ ExecutorService pool = Executors.newFixedThreadPool(64);
+ List<String> found = Collections.synchronizedList(new ArrayList<>());
+ for (int i = 1; i < 255; i++) {
+ String base = "http://" + prefix + i + ":" + PORT;
+ pool.execute(() -> {
+ try {
+ if (isFoldergram(Api.request(base, "GET", "/health", null, 800)))
+ found.add(base);
+ } catch (IOException e) {
+ // nothing listening there
+ }
+ });
+ }
+ pool.shutdown();
+ pool.awaitTermination(20, TimeUnit.SECONDS);
+ List<String> out = new ArrayList<>(found);
+ Collections.sort(out);
+ return out;
+ }
+
+ /** Reverse-DNS name plus IP when there is a name, else the IP. */
+ static String label(String base) {
+ String ip = URI.create(base).getHost();
+ try {
+ String name = InetAddress.getByName(ip).getCanonicalHostName();
+ return name.equals(ip) ? ip : name + " " + ip;
+ } catch (UnknownHostException e) {
+ return ip;
+ }
+ }
}