Skip to content

Commit

Permalink
more reliable mDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobeeeef committed Jul 29, 2024
1 parent 57df432 commit b45f559
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/kobe/xbot/Client/XTablesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ public XTablesClient(int MAX_THREADS, boolean useCache) {
*/
public XTablesClient(String name, int MAX_THREADS, boolean useCache) {
try {
InetAddress addr = Utilities.getLocalInetAddress();
InetAddress addr = null;
while (addr == null) {
addr = Utilities.getLocalInetAddress();
if(addr == null) {
logger.severe("No non-loopback IPv4 address found. Trying again in 1 second...");
Thread.sleep(1000);
}
}
try (JmDNS jmdns = JmDNS.create(addr)) {
CountDownLatch serviceLatch = new CountDownLatch(1);
final boolean[] serviceFound = {false};
Expand All @@ -107,7 +114,7 @@ public XTablesClient(String name, int MAX_THREADS, boolean useCache) {
@Override
public void serviceAdded(ServiceEvent event) {
logger.info("Service found: " + event.getName());
jmdns.requestServiceInfo(event.getType(), event.getName());
jmdns.requestServiceInfo(event.getType(), event.getName(), true);
}

@Override
Expand Down Expand Up @@ -152,6 +159,11 @@ public void serviceResolved(ServiceEvent event) {
});
if (name == null) logger.info("Listening for first instance of XTABLES service on port 5353...");
else logger.info("Listening for '" + name + "' XTABLES services on port 5353...");
while(serviceLatch.getCount() > 0 && !serviceFound[0] && !Thread.currentThread().isInterrupted()) {
try {
jmdns.requestServiceInfo("_xtables._tcp.local.", "XTablesService", true, 1000);
} catch (Exception ignored) {}
}
serviceLatch.await();
logger.info("Service latch released, proceeding to close mDNS services...");
jmdns.close();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/kobe/xbot/Utilities/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static InetAddress getLocalInetAddress() {
return findNonLoopbackAddress();
}
return localHost;
} catch (UnknownHostException | SocketException e) {
e.printStackTrace();
} catch (UnknownHostException | SocketException ignored) {
}
return null;
}
Expand Down

0 comments on commit b45f559

Please sign in to comment.