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.irc.util.MessageCleaner;
31 import net.pterodactylus.xdcc.core.Core;
32 import net.pterodactylus.xdcc.core.event.DownloadFailed;
33 import net.pterodactylus.xdcc.core.event.DownloadFinished;
34 import net.pterodactylus.xdcc.core.event.DownloadStarted;
35 import net.pterodactylus.xdcc.core.event.MessageReceived;
36 import net.pterodactylus.xdcc.data.Bot;
37 import net.pterodactylus.xdcc.data.Download;
38 import net.pterodactylus.xdcc.data.Pack;
40 import com.google.common.collect.Lists;
41 import com.google.common.collect.Sets;
42 import com.google.common.eventbus.Subscribe;
43 import com.google.common.primitives.Ints;
44 import com.google.common.util.concurrent.AbstractExecutionThreadService;
47 * Command interface for arbitrary {@link Reader}s.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public class CommandReader extends AbstractExecutionThreadService {
53 /** The core being controlled. */
54 private final Core core;
56 /** The reader to read commands from. */
57 private final BufferedReader reader;
59 /** The writer to write the results to. */
60 private final Writer writer;
63 * Creates a new command reader.
66 * The core being controlled
68 * The reader to read commands from
70 * The write to write results to
72 public CommandReader(Core core, Reader reader, Writer writer) {
74 this.reader = new BufferedReader(reader);
79 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
83 protected void run() throws Exception {
86 final List<Result> lastResult = Lists.newArrayList();
87 while ((line = reader.readLine()) != null) {
88 if (line.equals("")) {
91 String[] words = line.split(" +");
92 if (words[0].equalsIgnoreCase("search")) {
94 for (Bot bot : core.bots()) {
95 for (Pack pack : bot) {
97 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
98 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
102 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
108 lastResult.add(new Result(bot, pack));
112 Collections.sort(lastResult);
114 for (Result result : lastResult) {
115 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()));
117 writeLine("End of Search.");
118 } else if (words[0].equalsIgnoreCase("dcc")) {
120 for (DccReceiver dccReceiver : core.dccReceivers()) {
121 write(String.format("[%d] %s (%s, ", counter++, dccReceiver.filename(), dccReceiver.size()));
122 if (dccReceiver.isRunning()) {
123 write(String.format("%.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
125 if (dccReceiver.progress() >= dccReceiver.size()) {
126 write(String.format("complete, %s", f(dccReceiver.overallRate())));
128 write(String.format("aborted at %.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
133 writeLine("End of DCCs.");
134 } else if (words[0].equalsIgnoreCase("get")) {
135 Integer index = Ints.tryParse(words[1]);
136 if ((index != null) && (index < lastResult.size())) {
137 core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
139 } else if (words[0].equalsIgnoreCase("stats")) {
140 int configuredChannelsCount = core.channels().size();
141 int joinedChannelsCount = core.joinedChannels().size();
142 int extraChannelsCount = core.extraChannels().size();
143 Collection<Bot> bots = core.bots();
144 Set<String> packNames = Sets.newHashSet();
146 for (Bot bot : bots) {
147 packsCount += bot.packs().size();
148 for (Pack pack : bot) {
149 packNames.add(pack.name());
153 writeLine(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
165 * Called when a download was started.
167 * @param downloadStarted
168 * The download started event
171 public void downloadStarted(DownloadStarted downloadStarted) {
172 Download download = downloadStarted.download();
174 writeLine(String.format("Download of %s (from %s, %s) has started.", download.filename(), download.bot().name(), download.bot().network().name()));
175 } catch (IOException ioe1) {
181 * Called when a download is finished.
183 * @param downloadFinished
184 * The download finished event
187 public void downloadFinished(DownloadFinished downloadFinished) {
188 Download download = downloadFinished.download();
190 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())));
191 } catch (IOException ioe1) {
197 * Called when a download fails.
199 * @param downloadFailed
200 * The download failed event
203 public void downloadFailed(DownloadFailed downloadFailed) {
204 Download download = downloadFailed.download();
206 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())));
207 } catch (IOException ioe1) {
213 * Displays the received message on the console.
215 * @param messageReceived
216 * The message received event
219 public void messageReceived(MessageReceived messageReceived) {
221 writeLine(String.format("Message from %s: %s", messageReceived.source(), MessageCleaner.getDefaultInstance().clean(messageReceived.message())));
222 } catch (IOException e) {
232 * Writes the given text to the {@link #writer}.
236 * @throws IOException
237 * if an I/O error occurs
239 private void write(String text) throws IOException {
245 * Writes the given line followed by an LF to the {@link #writer}.
249 * @throws IOException
250 * if an I/O error occurs
252 private void writeLine(String line) throws IOException {
253 writer.write(line + "\n");
258 * Converts large numbers into a human-friendly format, by showing SI prefixes
259 * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
262 * The number to convert
263 * @return The converted number
265 private static String f(long number) {
266 if (number >= (1 << 30)) {
267 return String.format("%.1fG", number / (double) (1 << 30));
269 if (number >= (1 << 20)) {
270 return String.format("%.1fM", number / (double) (1 << 20));
272 if (number >= (1 << 10)) {
273 return String.format("%.1fK", number / (double) (1 << 10));
275 return String.format("%dB", number);
278 /** Container for result information. */
279 private static class Result implements Comparable<Result> {
281 /** The bot carrying the pack. */
282 private final Bot bot;
285 private final Pack pack;
288 * Creates a new result.
291 * The bot carrying the pack
295 private Result(Bot bot, Pack pack) {
305 * Returns the bot carrying the pack.
307 * @return The bot carrying the pack
323 // COMPARABLE METHODS
327 public int compareTo(Result result) {
328 return pack().name().compareToIgnoreCase(result.pack().name());