Remove all network-related data if the network is disconnected.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:17:36 +0000 (12:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:17:36 +0000 (12:17 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index 1f15af0..7013883 100644 (file)
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -38,6 +39,7 @@ import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
 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.DccAcceptReceived;
 import net.pterodactylus.irc.event.DccDownloadFailed;
@@ -64,6 +66,7 @@ import net.pterodactylus.xdcc.data.Server;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -326,6 +329,45 @@ public class Core extends AbstractExecutionThreadService {
        }
 
        /**
+        * Remove all data stored for a network if the connection is closed.
+        *
+        * @param connectionClosed
+        *              The connection closed event
+        */
+       @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)));
+
+               /* now remove the network. */
+               networkConnections.remove(network.get());
+       }
+
+       /**
         * Shows a message when a channel was joined by us.
         *
         * @param channelJoined