Handle connection creation failure.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 May 2013 19:12:58 +0000 (21:12 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 May 2013 19:12:58 +0000 (21:12 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index 6af2f73..2da9c73 100644 (file)
@@ -43,6 +43,7 @@ import net.pterodactylus.irc.event.ChannelMessageReceived;
 import net.pterodactylus.irc.event.ClientQuit;
 import net.pterodactylus.irc.event.ConnectionClosed;
 import net.pterodactylus.irc.event.ConnectionEstablished;
+import net.pterodactylus.irc.event.ConnectionFailed;
 import net.pterodactylus.irc.event.DccAcceptReceived;
 import net.pterodactylus.irc.event.DccDownloadFailed;
 import net.pterodactylus.irc.event.DccDownloadFinished;
@@ -343,6 +344,44 @@ public class Core extends AbstractExecutionThreadService {
                }
        }
 
+       /**
+        * Removes the given connection and all its channels and bots.
+        *
+        * @param connection
+        *              The connection to remove
+        */
+       private void removeConnection(Connection connection) {
+               Optional<Network> network = getNetwork(connection);
+               if (!network.isPresent()) {
+                       return;
+               }
+
+               /* find all channels that need to be removed. */
+               for (Collection<Channel> channels : ImmutableList.of(joinedChannels, extraChannels)) {
+                       for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
+                               Channel joinedChannel = channelIterator.next();
+                               if (!joinedChannel.network().equals(network.get())) {
+                                       continue;
+                               }
+
+                               channelIterator.remove();
+                       }
+               }
+
+               /* now remove all bots for that network. */
+               Map<String, Bot> bots = networkBots.row(network.get());
+               int botCount = bots.size();
+               int packCount = 0;
+               for (Bot bot : bots.values()) {
+                       packCount += bot.packs().size();
+               }
+               bots.clear();
+               eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
+
+               /* now remove the network. */
+               networkConnections.remove(network.get());
+       }
+
        //
        // EVENT HANDLERS
        //
@@ -385,35 +424,17 @@ public class Core extends AbstractExecutionThreadService {
         */
        @Subscribe
        public void connectionClosed(ConnectionClosed connectionClosed) {
-               Optional<Network> network = getNetwork(connectionClosed.connection());
-               if (!network.isPresent()) {
-                       return;
-               }
-
-               /* find all channels that need to be removed. */
-               for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) {
-                       for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
-                               Channel joinedChannel = channelIterator.next();
-                               if (!joinedChannel.network().equals(network.get())) {
-                                       continue;
-                               }
-
-                               channelIterator.remove();
-                       }
-               }
-
-               /* now remove all bots for that network. */
-               Map<String, Bot> bots = networkBots.row(network.get());
-               int botCount = bots.size();
-               int packCount = 0;
-               for (Bot bot : bots.values()) {
-                       packCount += bot.packs().size();
-               }
-               bots.clear();
-               eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
+               removeConnection(connectionClosed.connection());
+       }
 
-               /* now remove the network. */
-               networkConnections.remove(network.get());
+       /**
+        * Remove all data stored for a network if the connection fails.
+        *
+        * @param connectionFailed
+        *              The connection failed event
+        */
+       public void connectionFailed(ConnectionFailed connectionFailed) {
+               removeConnection(connectionFailed.connection());
        }
 
        /**