From aac6e30ec9f425daabac6e27222264b68adff372 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 25 Jan 2019 16:15:11 +0100 Subject: [PATCH] DiscoverHost now throws IoException when host discovery fails --- src/dorkbox/network/Broadcast.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/dorkbox/network/Broadcast.java b/src/dorkbox/network/Broadcast.java index 6f1fc5b9..760e8481 100644 --- a/src/dorkbox/network/Broadcast.java +++ b/src/dorkbox/network/Broadcast.java @@ -15,6 +15,7 @@ */ package dorkbox.network; +import java.io.IOException; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -73,7 +74,7 @@ class Broadcast { * @return the first server found, or null if no server responded. */ public static - BroadcastResponse discoverHost(int udpPort, int discoverTimeoutMillis) { + BroadcastResponse discoverHost(int udpPort, int discoverTimeoutMillis) throws IOException { BroadcastResponse discoverHost = discoverHostAddress(udpPort, discoverTimeoutMillis); if (discoverHost != null) { return discoverHost; @@ -92,7 +93,7 @@ class Broadcast { * @return the first server found, or null if no server responded. */ public static - BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) { + BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) throws IOException { List servers = discoverHosts0(logger, udpPort, discoverTimeoutMillis, false); if (servers.isEmpty()) { return null; @@ -113,13 +114,13 @@ class Broadcast { * @return the list of found servers (if they responded) */ public static - List discoverHosts(int udpPort, int discoverTimeoutMillis) { + List discoverHosts(int udpPort, int discoverTimeoutMillis) throws IOException { return discoverHosts0(logger, udpPort, discoverTimeoutMillis, true); } static - List discoverHosts0(Logger logger, int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) { + List discoverHosts0(Logger logger, int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) throws IOException { // fetch a buffer that contains the serialized object. ByteBuf buffer = Unpooled.buffer(1); buffer.writeByte(MagicBytes.broadcastID); @@ -133,7 +134,7 @@ class Broadcast { if (logger != null) { logger.error("Host discovery failed.", e); } - return new ArrayList(0); + throw new IOException("Host discovery failed. No interfaces found."); } @@ -190,14 +191,14 @@ class Broadcast { if (logger != null) { logger.error("Could not bind to random UDP address on the server.", e.getCause()); } - throw new IllegalArgumentException("Could not bind to random UDP address on the server."); + throw new IOException("Could not bind to random UDP address on the server."); } if (!future.isSuccess()) { if (logger != null) { logger.error("Could not bind to random UDP address on the server.", future.cause()); } - throw new IllegalArgumentException("Could not bind to random UDP address on the server."); + throw new IOException("Could not bind to random UDP address on the server."); } Channel channel1 = future.channel();