2 * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
29 import net.pterodactylus.irc.DccReceiver;
30 import net.pterodactylus.xdcc.core.Core;
31 import net.pterodactylus.xdcc.core.event.DownloadFailed;
32 import net.pterodactylus.xdcc.core.event.DownloadFinished;
33 import net.pterodactylus.xdcc.core.event.DownloadStarted;
34 import net.pterodactylus.xdcc.data.Bot;
35 import net.pterodactylus.xdcc.data.Download;
36 import net.pterodactylus.xdcc.data.Pack;
38 import com.google.common.collect.Lists;
39 import com.google.common.collect.Sets;
40 import com.google.common.eventbus.Subscribe;
41 import com.google.common.primitives.Ints;
42 import com.google.common.util.concurrent.AbstractExecutionThreadService;
45 * Command interface for arbitrary {@link Reader}s.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class CommandReader extends AbstractExecutionThreadService {
51 /** The core being controlled. */
52 private final Core core;
54 /** The reader to read commands from. */
55 private final BufferedReader reader;
57 /** The writer to write the results to. */
58 private final Writer writer;
61 * Creates a new command reader.
64 * The core being controlled
66 * The reader to read commands from
68 * The write to write results to
70 public CommandReader(Core core, Reader reader, Writer writer) {
72 this.reader = new BufferedReader(reader);
77 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
81 protected void run() throws Exception {
84 final List<Result> lastResult = Lists.newArrayList();
85 while ((line = reader.readLine()) != null) {
86 if (line.equals("")) {
89 String[] words = line.split(" +");
90 if (words[0].equalsIgnoreCase("search")) {
92 for (Bot bot : core.bots()) {
93 for (Pack pack : bot) {
95 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
96 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
100 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
106 lastResult.add(new Result(bot, pack));
110 Collections.sort(lastResult);
112 for (Result result : lastResult) {
113 writeLine(String.format("[%d] %s (%s) from %s (#%s) on %s", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name()));
115 writeLine("End of Search.");
116 } else if (words[0].equalsIgnoreCase("dcc")) {
118 for (DccReceiver dccReceiver : core.dccReceivers()) {
119 write(String.format("[%d] %s (%s, ", counter++, dccReceiver.filename(), dccReceiver.size()));
120 if (dccReceiver.isRunning()) {
121 write(String.format("%.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
123 if (dccReceiver.progress() >= dccReceiver.size()) {
124 write(String.format("complete, %s", f(dccReceiver.overallRate())));
126 write(String.format("aborted at %.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
131 writeLine("End of DCCs.");
132 } else if (words[0].equalsIgnoreCase("get")) {
133 Integer index = Ints.tryParse(words[1]);
134 if ((index != null) && (index < lastResult.size())) {
135 core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
137 } else if (words[0].equalsIgnoreCase("stats")) {
138 int configuredChannelsCount = core.channels().size();
139 int joinedChannelsCount = core.joinedChannels().size();
140 int extraChannelsCount = core.extraChannels().size();
141 Collection<Bot> bots = core.bots();
142 Set<String> packNames = Sets.newHashSet();
144 for (Bot bot : bots) {
145 packsCount += bot.packs().size();
146 for (Pack pack : bot) {
147 packNames.add(pack.name());
151 writeLine(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
163 * Called when a download was started.
165 * @param downloadStarted
166 * The download started event
169 public void downloadStarted(DownloadStarted downloadStarted) {
170 Download download = downloadStarted.download();
172 writeLine(String.format("Download of %s (from %s, %s) has started.", download.filename(), download.bot().name(), download.bot().network().name()));
173 } catch (IOException ioe1) {
179 * Called when a download is finished.
181 * @param downloadFinished
182 * The download finished event
185 public void downloadFinished(DownloadFinished downloadFinished) {
186 Download download = downloadFinished.download();
188 writeLine(String.format("Download of %s (from %s, %s) has finished, at %s/s.", download.filename(), download.bot().name(), download.bot().network().name(), f(download.dccReceiver().overallRate())));
189 } catch (IOException ioe1) {
195 * Called when a download fails.
197 * @param downloadFailed
198 * The download failed event
201 public void downloadFailed(DownloadFailed downloadFailed) {
202 Download download = downloadFailed.download();
204 writeLine(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())));
205 } catch (IOException ioe1) {
215 * Writes the given text to the {@link #writer}.
219 * @throws IOException
220 * if an I/O error occurs
222 private void write(String text) throws IOException {
228 * Writes the given line followed by an LF to the {@link #writer}.
232 * @throws IOException
233 * if an I/O error occurs
235 private void writeLine(String line) throws IOException {
236 writer.write(line + "\n");
241 * Converts large numbers into a human-friendly format, by showing SI prefixes
242 * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
245 * The number to convert
246 * @return The converted number
248 private static String f(long number) {
249 if (number >= (1 << 30)) {
250 return String.format("%.1fG", number / (double) (1 << 30));
252 if (number >= (1 << 20)) {
253 return String.format("%.1fM", number / (double) (1 << 20));
255 if (number >= (1 << 10)) {
256 return String.format("%.1fK", number / (double) (1 << 10));
258 return String.format("%dB", number);
261 /** Container for result information. */
262 private static class Result implements Comparable<Result> {
264 /** The bot carrying the pack. */
265 private final Bot bot;
268 private final Pack pack;
271 * Creates a new result.
274 * The bot carrying the pack
278 private Result(Bot bot, Pack pack) {
288 * Returns the bot carrying the pack.
290 * @return The bot carrying the pack
306 // COMPARABLE METHODS
310 public int compareTo(Result result) {
311 return pack().name().compareToIgnoreCase(result.pack().name());