Made logger optional for Host discovery.
This commit is contained in:
parent
463433ef45
commit
1d3d794f99
|
@ -93,7 +93,7 @@ class Broadcast {
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) {
|
BroadcastResponse discoverHostAddress(int udpPort, int discoverTimeoutMillis) {
|
||||||
List<BroadcastResponse> servers = discoverHost0(udpPort, discoverTimeoutMillis, false);
|
List<BroadcastResponse> servers = discoverHosts0(logger, udpPort, discoverTimeoutMillis, false);
|
||||||
if (servers.isEmpty()) {
|
if (servers.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -114,25 +114,25 @@ class Broadcast {
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
List<BroadcastResponse> discoverHosts(int udpPort, int discoverTimeoutMillis) {
|
List<BroadcastResponse> discoverHosts(int udpPort, int discoverTimeoutMillis) {
|
||||||
return discoverHost0(udpPort, discoverTimeoutMillis, true);
|
return discoverHosts0(logger, udpPort, discoverTimeoutMillis, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static
|
static
|
||||||
List<BroadcastResponse> discoverHost0(int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) {
|
List<BroadcastResponse> discoverHosts0(Logger logger, int udpPort, int discoverTimeoutMillis, boolean fetchAllServers) {
|
||||||
// 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);
|
||||||
|
|
||||||
List<BroadcastResponse> servers = new ArrayList<BroadcastResponse>();
|
List<BroadcastResponse> servers = new ArrayList<BroadcastResponse>();
|
||||||
|
|
||||||
Logger logger2 = logger;
|
|
||||||
|
|
||||||
Enumeration<NetworkInterface> networkInterfaces;
|
Enumeration<NetworkInterface> networkInterfaces;
|
||||||
try {
|
try {
|
||||||
networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger2.error("Host discovery failed.", e);
|
if (logger != null) {
|
||||||
|
logger.error("Host discovery failed.", e);
|
||||||
|
}
|
||||||
return new ArrayList<BroadcastResponse>(0);
|
return new ArrayList<BroadcastResponse>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,16 +145,20 @@ class Broadcast {
|
||||||
|
|
||||||
// don't use IPv6!
|
// don't use IPv6!
|
||||||
if (address instanceof Inet6Address) {
|
if (address instanceof Inet6Address) {
|
||||||
if (logger2.isInfoEnabled()) {
|
if (logger != null) {
|
||||||
logger2.info("Not using IPv6 address: {}", address);
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info("Not using IPv6 address: {}", address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (logger2.isInfoEnabled()) {
|
if (logger != null) {
|
||||||
logger2.info("Searching for host on [{}:{}]", address.getHostAddress(), udpPort);
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info("Searching for host on [{}:{}]", address.getHostAddress(), udpPort);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventLoopGroup group;
|
EventLoopGroup group;
|
||||||
|
@ -183,13 +187,17 @@ class Broadcast {
|
||||||
future = udpBootstrap.bind();
|
future = udpBootstrap.bind();
|
||||||
future.await();
|
future.await();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger2.error("Could not bind to random UDP address on the server.", e.getCause());
|
if (logger != null) {
|
||||||
throw new IllegalArgumentException();
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!future.isSuccess()) {
|
if (!future.isSuccess()) {
|
||||||
logger2.error("Could not bind to random UDP address on the server.", future.cause());
|
if (logger != null) {
|
||||||
throw new IllegalArgumentException();
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel channel1 = future.channel();
|
Channel channel1 = future.channel();
|
||||||
|
@ -199,10 +207,11 @@ class Broadcast {
|
||||||
channel1.writeAndFlush(new DatagramPacket(buffer, new InetSocketAddress(broadcast, udpPort)));
|
channel1.writeAndFlush(new DatagramPacket(buffer, new InetSocketAddress(broadcast, udpPort)));
|
||||||
|
|
||||||
// response is received. If the channel is not closed within 5 seconds, move to the next one.
|
// response is received. If the channel is not closed within 5 seconds, move to the next one.
|
||||||
if (!channel1.closeFuture()
|
if (!channel1.closeFuture().awaitUninterruptibly(discoverTimeoutMillis)) {
|
||||||
.awaitUninterruptibly(discoverTimeoutMillis)) {
|
if (logger != null) {
|
||||||
if (logger2.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger2.info("Host discovery timed out.");
|
logger.info("Host discovery timed out.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -213,10 +222,8 @@ class Broadcast {
|
||||||
|
|
||||||
// keep going if we want to fetch all servers. Break if we found one.
|
// keep going if we want to fetch all servers. Break if we found one.
|
||||||
if (!(fetchAllServers || servers.isEmpty())) {
|
if (!(fetchAllServers || servers.isEmpty())) {
|
||||||
channel1.close()
|
channel1.close().await();
|
||||||
.await();
|
group.shutdownGracefully().await();
|
||||||
group.shutdownGracefully()
|
|
||||||
.await();
|
|
||||||
break scan;
|
break scan;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -235,10 +242,11 @@ class Broadcast {
|
||||||
|
|
||||||
|
|
||||||
// response is received. If the channel is not closed within 5 seconds, move to the next one.
|
// response is received. If the channel is not closed within 5 seconds, move to the next one.
|
||||||
if (!channel1.closeFuture()
|
if (!channel1.closeFuture().awaitUninterruptibly(discoverTimeoutMillis)) {
|
||||||
.awaitUninterruptibly(discoverTimeoutMillis)) {
|
if (logger != null) {
|
||||||
if (logger2.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger2.info("Host discovery timed out.");
|
logger.info("Host discovery timed out.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -253,8 +261,7 @@ class Broadcast {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
channel1.close()
|
channel1.close().sync();
|
||||||
.sync();
|
|
||||||
group.shutdownGracefully(0, discoverTimeoutMillis, TimeUnit.MILLISECONDS);
|
group.shutdownGracefully(0, discoverTimeoutMillis, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
|
@ -268,8 +275,7 @@ class Broadcast {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (logger != null && logger.isInfoEnabled() && !servers.isEmpty()) {
|
||||||
if (logger2.isInfoEnabled() && !servers.isEmpty()) {
|
|
||||||
StringBuilder stringBuilder = new StringBuilder(256);
|
StringBuilder stringBuilder = new StringBuilder(256);
|
||||||
|
|
||||||
if (fetchAllServers) {
|
if (fetchAllServers) {
|
||||||
|
@ -293,7 +299,7 @@ class Broadcast {
|
||||||
stringBuilder.append(udpPort);
|
stringBuilder.append(udpPort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger2.info(stringBuilder.toString());
|
logger.info(stringBuilder.toString());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
BroadcastResponse server = servers.get(0);
|
BroadcastResponse server = servers.get(0);
|
||||||
|
@ -311,7 +317,7 @@ class Broadcast {
|
||||||
stringBuilder.append(udpPort);
|
stringBuilder.append(udpPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger2.info("Discovered server [{}]", stringBuilder.toString());
|
logger.info("Discovered server [{}]", stringBuilder.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -429,7 +429,7 @@ class Server<C extends Connection> extends EndPointServer {
|
||||||
|
|
||||||
// use Broadcast to see if there is a UDP server connected
|
// use Broadcast to see if there is a UDP server connected
|
||||||
if (config.udpPort > 0) {
|
if (config.udpPort > 0) {
|
||||||
List<BroadcastResponse> broadcastResponses = Broadcast.discoverHosts(config.udpPort, 500);
|
List<BroadcastResponse> broadcastResponses = Broadcast.discoverHosts0(null, config.udpPort, 500, true);
|
||||||
return !broadcastResponses.isEmpty();
|
return !broadcastResponses.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user