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.DownloadFinished;
32 import net.pterodactylus.xdcc.core.event.DownloadStarted;
33 import net.pterodactylus.xdcc.data.Bot;
34 import net.pterodactylus.xdcc.data.Download;
35 import net.pterodactylus.xdcc.data.Pack;
37 import com.google.common.collect.Lists;
38 import com.google.common.collect.Sets;
39 import com.google.common.eventbus.Subscribe;
40 import com.google.common.primitives.Ints;
41 import com.google.common.util.concurrent.AbstractExecutionThreadService;
44 * Command interface for arbitrary {@link Reader}s.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class CommandReader extends AbstractExecutionThreadService {
50 /** The core being controlled. */
51 private final Core core;
53 /** The reader to read commands from. */
54 private final BufferedReader reader;
56 /** The writer to write the results to. */
57 private final Writer writer;
60 * Creates a new command reader.
63 * The core being controlled
65 * The reader to read commands from
67 * The write to write results to
69 public CommandReader(Core core, Reader reader, Writer writer) {
71 this.reader = new BufferedReader(reader);
76 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
80 protected void run() throws Exception {
83 final List<Result> lastResult = Lists.newArrayList();
84 while ((line = reader.readLine()) != null) {
85 if (line.equals("")) {
88 String[] words = line.split(" +");
89 if (words[0].equalsIgnoreCase("search")) {
91 for (Bot bot : core.bots()) {
92 for (Pack pack : bot) {
94 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
95 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
99 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
105 lastResult.add(new Result(bot, pack));
109 Collections.sort(lastResult);
111 for (Result result : lastResult) {
112 writer.write(String.format("[%d] %s (%s) from %s (#%s) on %s\n", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name()));
114 writer.write("End of Search.\n");
115 } else if (words[0].equalsIgnoreCase("dcc")) {
117 for (DccReceiver dccReceiver : core.dccReceivers()) {
118 writer.write(String.format("[%d] %s (%s, ", counter++, dccReceiver.filename(), dccReceiver.size()));
119 if (dccReceiver.isRunning()) {
120 writer.write(String.format("%.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
122 if (dccReceiver.progress() >= dccReceiver.size()) {
123 writer.write(String.format("complete, %s", f(dccReceiver.overallRate())));
125 writer.write(String.format("aborted at %.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
128 writer.write("/s)\n");
130 writer.write("End of DCCs.\n");
131 } else if (words[0].equalsIgnoreCase("get")) {
132 Integer index = Ints.tryParse(words[1]);
133 if ((index != null) && (index < lastResult.size())) {
134 core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
136 } else if (words[0].equalsIgnoreCase("stats")) {
137 int configuredChannelsCount = core.channels().size();
138 int joinedChannelsCount = core.joinedChannels().size();
139 int extraChannelsCount = core.extraChannels().size();
140 Collection<Bot> bots = core.bots();
141 Set<String> packNames = Sets.newHashSet();
143 for (Bot bot : bots) {
144 packsCount += bot.packs().size();
145 for (Pack pack : bot) {
146 packNames.add(pack.name());
150 writer.write(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).\n", 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 writer.write(String.format("Download of %s (from %s, %s) has started.\n", download.filename(), download.bot().name(), download.bot().network().name()));
174 } catch (IOException ioe1) {
180 * Called when a download is finished.
182 * @param downloadFinished
183 * The download finished event
186 public void downloadFinished(DownloadFinished downloadFinished) {
187 Download download = downloadFinished.download();
189 writer.write(String.format("Download of %s (from %s, %s) has finished, at %s/s.\n", download.filename(), download.bot().name(), download.bot().network().name(), download.dccReceiver().overallRate()));
191 } catch (IOException ioe1) {
201 * Converts large numbers into a human-friendly format, by showing SI prefixes
202 * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
205 * The number to convert
206 * @return The converted number
208 private static String f(long number) {
209 if (number >= (1 << 30)) {
210 return String.format("%.1fG", number / (double) (1 << 30));
212 if (number >= (1 << 20)) {
213 return String.format("%.1fM", number / (double) (1 << 20));
215 if (number >= (1 << 10)) {
216 return String.format("%.1fK", number / (double) (1 << 10));
218 return String.format("%dB", number);
221 /** Container for result information. */
222 private static class Result implements Comparable<Result> {
224 /** The bot carrying the pack. */
225 private final Bot bot;
228 private final Pack pack;
231 * Creates a new result.
234 * The bot carrying the pack
238 private Result(Bot bot, Pack pack) {
248 * Returns the bot carrying the pack.
250 * @return The bot carrying the pack
266 // COMPARABLE METHODS
270 public int compareTo(Result result) {
271 return pack().name().compareToIgnoreCase(result.pack().name());