DiscoverHost now throws IoException when host discovery fails

This commit is contained in:
nathan 2019-01-25 16:15:11 +01:00
parent e6cc3fd851
commit aac6e30ec9

View File

@ -15,6 +15,7 @@
*/ */
package dorkbox.network; package dorkbox.network;
import java.io.IOException;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -73,7 +74,7 @@ class Broadcast {
* @return the first server found, or null if no server responded. * @return the first server found, or null if no server responded.
*/ */
public static public static
BroadcastResponse discoverHost(int udpPort, int discoverTimeoutMillis) { BroadcastResponse discoverHost(int udpPort, int discoverTimeoutMillis) throws IOException {
BroadcastResponse discoverHost = discoverHostAddress(udpPort, discoverTimeoutMillis); BroadcastResponse discoverHost = discoverHostAddress(udpPort, discoverTimeoutMillis);
if (discoverHost != null) { if (discoverHost != null) {
return discoverHost; return discoverHost;
@ -92,7 +93,7 @@ class Broadcast {
* @return the first server found, or null if no server responded. * @return the first server found, or null if no server responded.
*/ */
public static public static
BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) { BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) throws IOException {
List<BroadcastResponse> servers = discoverHosts0(logger, udpPort, discoverTimeoutMillis, false); List<BroadcastResponse> servers = discoverHosts0(logger, udpPort, discoverTimeoutMillis, false);
if (servers.isEmpty()) { if (servers.isEmpty()) {
return null; return null;
@ -113,13 +114,13 @@ class Broadcast {
* @return the list of found servers (if they responded) * @return the list of found servers (if they responded)
*/ */
public static public static
List<BroadcastResponse> discoverHosts(int udpPort, int discoverTimeoutMillis) { List<BroadcastResponse> discoverHosts(int udpPort, int discoverTimeoutMillis) throws IOException {
return discoverHosts0(logger, udpPort, discoverTimeoutMillis, true); return discoverHosts0(logger, udpPort, discoverTimeoutMillis, true);
} }
static static
List<BroadcastResponse> discoverHosts0(Logger logger, int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) { List<BroadcastResponse> discoverHosts0(Logger logger, int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) throws IOException {
// fetch a buffer that contains the serialized object. // fetch a buffer that contains the serialized object.
ByteBuf buffer = Unpooled.buffer(1); ByteBuf buffer = Unpooled.buffer(1);
buffer.writeByte(MagicBytes.broadcastID); buffer.writeByte(MagicBytes.broadcastID);
@ -133,7 +134,7 @@ class Broadcast {
if (logger != null) { if (logger != null) {
logger.error("Host discovery failed.", e); logger.error("Host discovery failed.", e);
} }
return new ArrayList<BroadcastResponse>(0); throw new IOException("Host discovery failed. No interfaces found.");
} }
@ -190,14 +191,14 @@ class Broadcast {
if (logger != null) { if (logger != null) {
logger.error("Could not bind to random UDP address on the server.", e.getCause()); 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 (!future.isSuccess()) {
if (logger != null) { if (logger != null) {
logger.error("Could not bind to random UDP address on the server.", future.cause()); 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(); Channel channel1 = future.channel();