Only store the failed downloads.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 6 Jan 2014 01:09:51 +0000 (02:09 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 6 Jan 2014 01:09:51 +0000 (02:09 +0100)
src/main/java/net/pterodactylus/xdcc/main/Main.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailure.java [deleted file]
src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailures.java [deleted file]
src/main/java/net/pterodactylus/xdcc/ui/stdin/FailedDownloadsCommand.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/ResearchCommand.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/RestartCommand.java
src/test/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailuresTest.java [deleted file]

index 6d6d849..0033247 100644 (file)
@@ -20,6 +20,9 @@ package net.pterodactylus.xdcc.main;
 import java.io.File;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.Executors;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Formatter;
@@ -30,12 +33,11 @@ import java.util.logging.Logger;
 import net.pterodactylus.irc.Connection;
 import net.pterodactylus.xdcc.core.Core;
 import net.pterodactylus.xdcc.data.Channel;
+import net.pterodactylus.xdcc.data.Download;
 import net.pterodactylus.xdcc.data.Network;
 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
-import net.pterodactylus.xdcc.ui.stdin.DownloadFailure;
-import net.pterodactylus.xdcc.ui.stdin.DownloadFailures;
 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
 
 import com.google.common.eventbus.AsyncEventBus;
@@ -97,13 +99,13 @@ public class Main {
                        }
                }
 
-               DownloadFailures downloadFailures = new DownloadFailures();
+               Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
 
-               CommandReader commandReader = new CommandReader(core, new InputStreamReader(System.in, "UTF-8"), new OutputStreamWriter(System.out, "UTF-8"), downloadFailures);
+               CommandReader commandReader = new CommandReader(core, new InputStreamReader(System.in, "UTF-8"), new OutputStreamWriter(System.out, "UTF-8"), failedDownloads);
                commandReader.start();
                eventBus.register(commandReader);
 
-               NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, downloadFailures), configuration.getTelnetPort());
+               NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
                networkAcceptor.start();
 
                core.start();
index 83c9802..fc1a48f 100644 (file)
@@ -62,7 +62,7 @@ public class CommandReader extends AbstractExecutionThreadService {
 
        /** The writer to write the results to. */
        private final Writer writer;
-       private final DownloadFailures downloadFailures;
+       private final Collection<Download> failedDownloads;
 
        /**
         * Creates a new command reader.
@@ -74,10 +74,10 @@ public class CommandReader extends AbstractExecutionThreadService {
         * @param writer
         *              The write to write results to
         */
-       public CommandReader(Core core, Reader reader, Writer writer, DownloadFailures downloadFailures) {
+       public CommandReader(Core core, Reader reader, Writer writer, Collection<Download> failedDownloads) {
                this.reader = new BufferedReader(reader);
                this.writer = writer;
-               this.downloadFailures = downloadFailures;
+               this.failedDownloads = failedDownloads;
 
                /* initialize commands. */
                ImmutableList.Builder<Command> commandBuilder = ImmutableList.builder();
@@ -88,8 +88,8 @@ public class CommandReader extends AbstractExecutionThreadService {
                commandBuilder.add(new ListConnectionsCommand(core));
                commandBuilder.add(new AbortDownloadCommand(core));
                commandBuilder.add(new DisconnectCommand(core));
-               commandBuilder.add(new FailedDownloadsCommand(downloadFailures));
-               commandBuilder.add(new RestartCommand(core, downloadFailures));
+               commandBuilder.add(new FailedDownloadsCommand(failedDownloads));
+               commandBuilder.add(new RestartCommand(core, failedDownloads));
                commandBuilder.add(new ResearchCommand(core));
                commands = commandBuilder.build();
        }
@@ -170,7 +170,7 @@ public class CommandReader extends AbstractExecutionThreadService {
        @Subscribe
        public void downloadFailed(DownloadFailed downloadFailed) {
                Download download = downloadFailed.download();
-               downloadFailures.addFailedDownload(download, System.currentTimeMillis());
+               failedDownloads.add(download);
                try {
                        writeLine(red(String.format("Download of %s (from %s, %s) has failed at %.1f%% and %s/s.", download.filename(), download.bot().name(), download.bot().network().name(), download.dccReceiver().progress() * 100.0 / download.dccReceiver().size(), f(download.dccReceiver().overallRate()))));
                } catch (IOException ioe1) {
diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailure.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailure.java
deleted file mode 100644 (file)
index 2177067..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * XdccDownloader - DownloadFailure.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.xdcc.ui.stdin;
-
-import net.pterodactylus.xdcc.data.Download;
-
-/**
- * TODO
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DownloadFailure {
-
-       private final Download download;
-       private final long failureTime;
-
-       public DownloadFailure(Download download, long failureTime) {
-               this.download = download;
-               this.failureTime = failureTime;
-       }
-
-       public Download getDownload() {
-               return download;
-       }
-
-       public long getFailureTime() {
-               return failureTime;
-       }
-
-       @Override
-       public int hashCode() {
-               return (int) (download.hashCode() ^ (failureTime & 0xffffffff) ^ (failureTime >> 32));
-       }
-
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof DownloadFailure)) {
-                       return false;
-               }
-               DownloadFailure downloadFailure = (DownloadFailure) object;
-               return downloadFailure.getDownload().equals(getDownload()) && (downloadFailure.getFailureTime() == getFailureTime());
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailures.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailures.java
deleted file mode 100644 (file)
index da4ee40..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * XdccDownloader - DownloadFailures.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.xdcc.ui.stdin;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.function.Function;
-
-import net.pterodactylus.xdcc.data.Download;
-
-/**
- * TODO
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DownloadFailures implements Iterable<DownloadFailure> {
-
-       private final Map<Download, Long> downloadFailures = new HashMap<>();
-
-       public void addFailedDownload(Download download, long failureTime) {
-               synchronized (downloadFailures) {
-                       downloadFailures.put(download, failureTime);
-               }
-       }
-
-       public void removeFailedDownload(Download download) {
-               synchronized (downloadFailures) {
-                       downloadFailures.remove(download);
-               }
-       }
-
-       @Override
-       public Iterator<DownloadFailure> iterator() {
-               synchronized (downloadFailures) {
-                       return downloadFailures.entrySet().stream().map(new Function<Entry<Download, Long>, DownloadFailure>() {
-                               @Override
-                               public DownloadFailure apply(Entry<Download, Long> downloadEntry) {
-                                       return new DownloadFailure(downloadEntry.getKey(), downloadEntry.getValue());
-                               }
-                       }).iterator();
-               }
-       }
-
-}
index 929c2f3..7a4869b 100644 (file)
@@ -29,16 +29,16 @@ import java.util.List;
 import net.pterodactylus.xdcc.data.Download;
 
 /**
- * TODO
+ * Lists all failed downloads.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class FailedDownloadsCommand implements Command {
 
-       private final DownloadFailures downloadFailures;
+       private final Collection<Download> failedDownloads;
 
-       public FailedDownloadsCommand(DownloadFailures downloadFailures) {
-               this.downloadFailures = downloadFailures;
+       public FailedDownloadsCommand(Collection<Download> failedDownloads) {
+               this.failedDownloads = failedDownloads;
        }
 
        @Override
@@ -54,15 +54,12 @@ public class FailedDownloadsCommand implements Command {
        @Override
        public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
                int downloadIndex = 0;
-               List<Download> failedDownloads = new ArrayList<>();
-               for (DownloadFailure downloadFailure : downloadFailures) {
-                       Download download = downloadFailure.getDownload();
-                       failedDownloads.add(download);
+               for (Download download : failedDownloads) {
                        outputWriter.write(format("[%d] %s from %s\n", downloadIndex, download.filename(), download.bot().name()));
                        downloadIndex++;
                }
                outputWriter.write("End of failed downloads.\n");
-               return state.setLastFailedDownloads(failedDownloads);
+               return state.setLastFailedDownloads(new ArrayList<>(failedDownloads));
        }
 
 }
index 5f9e57a..1955769 100644 (file)
@@ -31,7 +31,7 @@ import net.pterodactylus.xdcc.data.Download;
 import com.google.common.primitives.Ints;
 
 /**
- * TODO
+ * Command that runs a search for the exact filename of a failed download.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
index 84a7d2e..bd8fac8 100644 (file)
@@ -30,16 +30,16 @@ import net.pterodactylus.xdcc.data.Download;
 import com.google.common.primitives.Ints;
 
 /**
- * TODO
+ * Command that can restart a failed download by rerequesting the same pack from the same bot.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class RestartCommand implements Command {
 
        private final Core core;
-       private final DownloadFailures downloadFailures;
+       private final Collection<Download> downloadFailures;
 
-       public RestartCommand(Core core, DownloadFailures downloadFailures) {
+       public RestartCommand(Core core, Collection<Download> downloadFailures) {
                this.core = core;
                this.downloadFailures = downloadFailures;
        }
@@ -61,7 +61,7 @@ public class RestartCommand implements Command {
                if ((index != null) && (index < lastFailedDownloads.size())) {
                        Download failedDownload = lastFailedDownloads.get(index);
                        core.fetch(failedDownload.bot(), failedDownload.pack());
-                       downloadFailures.removeFailedDownload(failedDownload);
+                       downloadFailures.remove(failedDownload);
                }
                return state;
        }
diff --git a/src/test/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailuresTest.java b/src/test/java/net/pterodactylus/xdcc/ui/stdin/DownloadFailuresTest.java
deleted file mode 100644 (file)
index 9f55e4e..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * XdccDownloader - DownloadFailuresTest.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.xdcc.ui.stdin;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
-import static org.hamcrest.Matchers.emptyIterable;
-import static org.mockito.Mockito.mock;
-
-import net.pterodactylus.xdcc.data.Download;
-
-import org.junit.Test;
-
-/**
- * TODO
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DownloadFailuresTest {
-
-       @Test
-       public void canRemoveDownloadFailures() {
-               long failureTime = System.currentTimeMillis();
-               DownloadFailures downloadFailures = new DownloadFailures();
-               Download download = mock(Download.class);
-               downloadFailures.addFailedDownload(download, failureTime);
-               assertThat(downloadFailures, contains(new DownloadFailure(download, failureTime)));
-               downloadFailures.removeFailedDownload(download);
-               assertThat(downloadFailures, emptyIterable());
-       }
-
-}