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. */
}
//
+ // 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
//