Move network connecting into its own method.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:15:53 +0000 (12:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:15:53 +0000 (12:15 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index 2c68c76..a395537 100644 (file)
@@ -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<Server> 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<Server> 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
        //