From: David ‘Bombe’ Roden Date: Sun, 14 Apr 2013 10:15:53 +0000 (+0200) Subject: Move network connecting into its own method. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f6942b34cf66267bc67d673cf7852f420ab06dfd;p=xudocci.git Move network connecting into its own method. --- diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 2c68c76..a395537 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -227,15 +227,7 @@ public class Core extends AbstractIdleService { protected void startUp() { for (Channel channel : channels) { logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name())); - if (!networkConnections.containsKey(channel.network())) { - /* select a random server. */ - List servers = Lists.newArrayList(channel.network().servers()); - Server server = servers.get((int) (Math.random() * servers.size())); - Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build(); - connection.username(RandomNickname.get()).realName(RandomNickname.get()); - networkConnections.put(channel.network(), connection); - connection.start(); - } + connectNetwork(channel.network()); } /* notify listeners. */ @@ -247,6 +239,33 @@ public class Core extends AbstractIdleService { } // + // PRIVATE METHODS + // + + /** + * Starts a new connection for the given network if no such connection exists + * already. + * + * @param network + * The network to connect to + */ + private void connectNetwork(Network network) { + if (!networkConnections.containsKey(network)) { + /* select a random server. */ + List servers = Lists.newArrayList(network.servers()); + if (servers.isEmpty()) { + eventBus.post(new GenericError(String.format("Network %s does not have any servers.", network.name()))); + return; + } + Server server = servers.get((int) (Math.random() * servers.size())); + Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build(); + connection.username(RandomNickname.get()).realName(RandomNickname.get()); + networkConnections.put(network, connection); + connection.start(); + } + } + + // // EVENT HANDLERS //