From 7e64f9805dbaef5fa2084668748bcca4da37bae6 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 24 Aug 2015 23:39:32 -0400 Subject: [PATCH 1/3] Better exception propagation when failing to bind --- game/src/main/org/apollo/Server.java | 52 ++++++++++++++++++---------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/game/src/main/org/apollo/Server.java b/game/src/main/org/apollo/Server.java index 41673ecd..ce97141d 100644 --- a/game/src/main/org/apollo/Server.java +++ b/game/src/main/org/apollo/Server.java @@ -1,12 +1,6 @@ package org.apollo; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioServerSocketChannel; - +import java.net.BindException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.file.Paths; @@ -28,6 +22,13 @@ import org.apollo.net.release.Release; import com.google.common.base.Stopwatch; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + /** * The core class of the Apollo server. * @@ -97,25 +98,38 @@ public final class Server { * @param service The service address to bind to. * @param http The HTTP address to bind to. * @param jaggrab The JAGGRAB address to bind to. + * @throws BindException If the ServerBootstrap fails to bind to the SocketAddress for any + * reason. */ - public void bind(SocketAddress service, SocketAddress http, SocketAddress jaggrab) { - try { - logger.fine("Binding service listener to address: " + service + "..."); - serviceBootstrap.bind(service).sync(); + public void bind(SocketAddress service, SocketAddress http, SocketAddress jaggrab) throws BindException { + logger.info("Binding service listener to address: " + service + "..."); + bind(serviceBootstrap, service); - logger.fine("Binding HTTP listener to address: " + http + "..."); - httpBootstrap.bind(http).sync(); + logger.info("Binding HTTP listener to address: " + http + "..."); + bind(httpBootstrap, http); - logger.fine("Binding JAGGRAB listener to address: " + jaggrab + "..."); - jaggrabBootstrap.bind(jaggrab).sync(); - } catch (InterruptedException exception) { - logger.log(Level.SEVERE, "Binding to a port failed: ensure apollo isn't already running.", exception); - System.exit(1); - } + logger.info("Binding JAGGRAB listener to address: " + jaggrab + "..."); + bind(jaggrabBootstrap, jaggrab); logger.info("Ready for connections."); } + /** + * Attempts to bind the specified ServerBootstrap to the specified SocketAddress. + * + * @param bootstrap The ServerBootstrap. + * @param address The SocketAddress. + * @throws BindException If the ServerBootstrap fails to bind to the SocketAddress for any + * reason. + */ + private void bind(ServerBootstrap bootstrap, SocketAddress address) throws BindException { + try { + bootstrap.bind(address).sync(); + } catch (Exception cause) { + throw new BindException("Failed to bind to: " + address); + } + } + /** * Initialises the server. * From 64c99b22cdbbdd85638beb2d04e34ec8333f49d9 Mon Sep 17 00:00:00 2001 From: atomicint Date: Thu, 27 Aug 2015 17:06:56 -0400 Subject: [PATCH 2/3] Update dependencies --- pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 28626d87..29a33b97 100644 --- a/pom.xml +++ b/pom.xml @@ -47,13 +47,13 @@ org.apache.commons commons-compress - 1.9 + 1.10 org.jruby jruby-complete - 1.7.19 + 9.0.0.0 @@ -65,7 +65,7 @@ io.netty netty-all - 4.0.27.Final + 4.0.30.Final compile @@ -78,7 +78,7 @@ com.mchange c3p0 - 0.9.5 + 0.9.5.1 From 5bbd482a199152dae4134095ff55e6a4b078998d Mon Sep 17 00:00:00 2001 From: atomicint Date: Thu, 27 Aug 2015 17:08:41 -0400 Subject: [PATCH 3/3] Do not fail startup when HTTP cannot bind --- game/src/main/org/apollo/Server.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/game/src/main/org/apollo/Server.java b/game/src/main/org/apollo/Server.java index ce97141d..f1df758b 100644 --- a/game/src/main/org/apollo/Server.java +++ b/game/src/main/org/apollo/Server.java @@ -60,6 +60,7 @@ public final class Server { server.bind(service, http, jaggrab); } catch (Throwable t) { logger.log(Level.SEVERE, "Error whilst starting server.", t); + System.exit(0); } logger.fine("Starting apollo took " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms."); @@ -102,13 +103,17 @@ public final class Server { * reason. */ public void bind(SocketAddress service, SocketAddress http, SocketAddress jaggrab) throws BindException { - logger.info("Binding service listener to address: " + service + "..."); + logger.fine("Binding service listener to address: " + service + "..."); bind(serviceBootstrap, service); - logger.info("Binding HTTP listener to address: " + http + "..."); - bind(httpBootstrap, http); + try { + logger.fine("Binding HTTP listener to address: " + http + "..."); + bind(httpBootstrap, http); + } catch (Exception cause) { + logger.warning("Unable to bind to HTTP, JAGGRAB will be used as a fallback however this is not recommended."); + } - logger.info("Binding JAGGRAB listener to address: " + jaggrab + "..."); + logger.fine("Binding JAGGRAB listener to address: " + jaggrab + "..."); bind(jaggrabBootstrap, jaggrab); logger.info("Ready for connections.");