From: David ‘Bombe’ Roden Date: Thu, 2 May 2013 19:12:58 +0000 (+0200) Subject: Handle connection creation failure. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=aebda4d27b2079541ca88f92b5a05b67a837d2bc;p=xudocci.git Handle connection creation failure. --- diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 6af2f73..2da9c73 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -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 = getNetwork(connection); + if (!network.isPresent()) { + return; + } + + /* find all channels that need to be removed. */ + for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) { + for (Iterator 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 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 = getNetwork(connectionClosed.connection()); - if (!network.isPresent()) { - return; - } - - /* find all channels that need to be removed. */ - for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) { - for (Iterator 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 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()); } /**