X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fcore%2FCore.java;h=2da9c73ec3849b60362b61b65f4346bda5ce36df;hb=aebda4d27b2079541ca88f92b5a05b67a837d2bc;hp=607246c4d439dc01bf9d59122425714263ac0c6e;hpb=7356600f3a82060f49deb252d3adde1ed3f062fb;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 607246c..2da9c73 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -43,13 +43,14 @@ 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; import net.pterodactylus.irc.event.DccSendReceived; import net.pterodactylus.irc.event.NicknameChanged; -import net.pterodactylus.irc.event.PrivateNoticeReceived; import net.pterodactylus.irc.event.PrivateMessageReceived; +import net.pterodactylus.irc.event.PrivateNoticeReceived; import net.pterodactylus.irc.util.MessageCleaner; import net.pterodactylus.irc.util.RandomNickname; import net.pterodactylus.xdcc.core.event.BotAdded; @@ -147,6 +148,15 @@ public class Core extends AbstractExecutionThreadService { // /** + * Returns all currently known connections. + * + * @return All currently known connections + */ + public Collection connections() { + return networkConnections.values(); + } + + /** * Returns all configured channels. Due to various circumstances, configured * channels might not actually be joined. * @@ -220,6 +230,25 @@ public class Core extends AbstractExecutionThreadService { return; } + /* check if we are already downloading the file? */ + if (downloads.containsKey(pack.name())) { + Collection packDownloads = downloads.get(pack.name()); + Collection runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet(); + if (!runningDownloads.isEmpty()) { + Download download = runningDownloads.iterator().next(); + eventBus.post(new GenericMessage(String.format("File %s is already downloading from %s (%s).", pack.name(), download.bot().name(), download.bot().network().name()))); + return; + } + StringBuilder bots = new StringBuilder(); + for (Download download : packDownloads) { + if (bots.length() > 0) { + bots.append(", "); + } + bots.append(download.bot().name()).append(" (").append(download.bot().network().name()).append(')'); + } + eventBus.post(new GenericMessage(String.format("File %s is already requested from %d bots (%s).", pack.name(), packDownloads.size(), bots.toString()))); + } + Download download = new Download(bot, pack); downloads.put(pack.name(), download); @@ -230,6 +259,20 @@ public class Core extends AbstractExecutionThreadService { } } + /** + * Closes the given connection. + * + * @param connection + * The connection to close + */ + public void closeConnection(Connection connection) { + try { + connection.close(); + } catch (IOException ioe1) { + /* TODO */ + } + } + // // ABSTRACTIDLESERVICE METHODS // @@ -301,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 // @@ -343,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()); } /** @@ -515,7 +578,7 @@ public class Core extends AbstractExecutionThreadService { Bot bot; synchronized (networkBots) { if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) { - bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get()); + bot = new Bot(network.get(), channelMessageReceived.source().nick().get()); networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot); eventBus.post(new BotAdded(bot)); } else { @@ -552,7 +615,7 @@ public class Core extends AbstractExecutionThreadService { return; } - eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source(), network.get(), privateNoticeReceived.text()))); + eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source().get(), network.get(), privateNoticeReceived.text()))); } /**