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 static net.pterodactylus.xdcc.data.Download.BY_NAME;
21 import static net.pterodactylus.xdcc.data.Download.BY_RUNNING;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.Writer;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
33 import java.util.regex.Pattern;
35 import net.pterodactylus.irc.Connection;
36 import net.pterodactylus.irc.DccReceiver;
37 import net.pterodactylus.irc.util.MessageCleaner;
38 import net.pterodactylus.xdcc.core.Core;
39 import net.pterodactylus.xdcc.core.event.DownloadFailed;
40 import net.pterodactylus.xdcc.core.event.DownloadFinished;
41 import net.pterodactylus.xdcc.core.event.DownloadStarted;
42 import net.pterodactylus.xdcc.core.event.GenericMessage;
43 import net.pterodactylus.xdcc.core.event.MessageReceived;
44 import net.pterodactylus.xdcc.data.Bot;
45 import net.pterodactylus.xdcc.data.Download;
46 import net.pterodactylus.xdcc.data.Pack;
48 import com.google.common.base.Predicate;
49 import com.google.common.collect.ComparisonChain;
50 import com.google.common.collect.FluentIterable;
51 import com.google.common.collect.Lists;
52 import com.google.common.collect.Ordering;
53 import com.google.common.collect.Sets;
54 import com.google.common.eventbus.Subscribe;
55 import com.google.common.primitives.Ints;
56 import com.google.common.util.concurrent.AbstractExecutionThreadService;
59 * Command interface for arbitrary {@link Reader}s.
61 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
63 public class CommandReader extends AbstractExecutionThreadService {
65 /** The core being controlled. */
66 private final Core core;
68 /** The reader to read commands from. */
69 private final BufferedReader reader;
71 /** The writer to write the results to. */
72 private final Writer writer;
75 * Creates a new command reader.
78 * The core being controlled
80 * The reader to read commands from
82 * The write to write results to
84 public CommandReader(Core core, Reader reader, Writer writer) {
86 this.reader = new BufferedReader(reader);
91 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
95 protected void run() throws Exception {
98 final List<Result> lastResult = Lists.newArrayList();
99 final List<Connection> lastConnections = Lists.newArrayList();
100 while ((line = reader.readLine()) != null) {
101 if (line.equals("")) {
104 String[] words = line.split(" +");
105 if (words[0].equalsIgnoreCase("search")) {
107 for (Bot bot : Lists.newArrayList(core.bots())) {
108 for (Pack pack : Lists.newArrayList(bot)) {
109 boolean found = true;
110 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
111 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
115 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
121 lastResult.add(new Result(bot, pack));
125 Collections.sort(lastResult);
127 for (Result result : lastResult) {
128 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()));
130 writeLine("End of Search.");
131 } else if (words[0].equalsIgnoreCase("dcc")) {
133 for (Download download : FluentIterable.from(core.downloads()).toSortedList(Ordering.from(BY_NAME).compound(BY_RUNNING))) {
134 DccReceiver dccReceiver = download.dccReceiver();
135 if (dccReceiver == null) {
136 /* download has not even started. */
137 writer.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name()));
140 writer.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
141 if (dccReceiver.isRunning()) {
142 writer.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), getTimeLeft(dccReceiver)));
144 if (dccReceiver.progress() >= dccReceiver.size()) {
145 writer.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
147 writer.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
152 writeLine("End of DCCs.");
153 } else if (words[0].equalsIgnoreCase("get")) {
154 Integer index = Ints.tryParse(words[1]);
155 if ((index != null) && (index < lastResult.size())) {
156 core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
158 } else if (words[0].equalsIgnoreCase("stats")) {
159 int configuredChannelsCount = core.channels().size();
160 int joinedChannelsCount = core.joinedChannels().size();
161 int extraChannelsCount = core.extraChannels().size();
162 Collection<Bot> bots = core.bots();
163 Set<String> packNames = Sets.newHashSet();
165 for (Bot bot : bots) {
166 packsCount += bot.packs().size();
167 for (Pack pack : bot) {
168 packNames.add(pack.name());
172 writeLine(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
173 } else if (words[0].equalsIgnoreCase("connections")) {
174 lastConnections.clear();
176 for (Connection connection : core.connections()) {
177 lastConnections.add(connection);
178 writer.write(String.format("[%d] %s:%d, %s/s\n", counter++, connection.hostname(), connection.port(), f(connection.getInputRate())));
180 writeLine("End of connections.");
181 } else if (words[0].equalsIgnoreCase("disconnect")) {
182 if ((words.length == 1) || ("all".equals(words[1]))) {
183 for (Connection connection : lastConnections) {
184 core.closeConnection(connection);
187 Integer index = Ints.tryParse(words[1]);
188 if ((index != null) && (index < lastConnections.size())) {
189 core.closeConnection(lastConnections.get(index));
203 * Called when a download was started.
205 * @param downloadStarted
206 * The download started event
209 public void downloadStarted(DownloadStarted downloadStarted) {
210 Download download = downloadStarted.download();
212 writeLine(String.format("Download of %s (from %s, %s) has started.", download.pack().name(), download.bot().name(), download.bot().network().name()));
213 } catch (IOException ioe1) {
219 * Called when a download is finished.
221 * @param downloadFinished
222 * The download finished event
225 public void downloadFinished(DownloadFinished downloadFinished) {
226 Download download = downloadFinished.download();
228 writeLine(String.format("Download of %s (from %s, %s) has finished, at %s/s.", download.pack().name(), download.bot().name(), download.bot().network().name(), f(download.dccReceiver().overallRate())));
229 } catch (IOException ioe1) {
235 * Called when a download fails.
237 * @param downloadFailed
238 * The download failed event
241 public void downloadFailed(DownloadFailed downloadFailed) {
242 Download download = downloadFailed.download();
244 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())));
245 } catch (IOException ioe1) {
251 * Displays the received message on the console.
253 * @param messageReceived
254 * The message received event
257 public void messageReceived(MessageReceived messageReceived) {
259 writeLine(String.format("Message from %s: %s", messageReceived.source(), MessageCleaner.getDefaultInstance().clean(messageReceived.message())));
260 } catch (IOException e) {
266 * Writes a generic message to the console.
268 * @param genericMessage
269 * The generic message event
272 public void genericMessage(GenericMessage genericMessage) {
274 writeLine(genericMessage.message());
275 } catch (IOException ioe1) {
285 * Writes the given line followed by an LF to the {@link #writer}.
289 * @throws IOException
290 * if an I/O error occurs
292 private void writeLine(String line) throws IOException {
293 writer.write(line + "\n");
298 * Converts large numbers into a human-friendly format, by showing SI prefixes
299 * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
302 * The number to convert
303 * @return The converted number
305 private static String f(long number) {
306 if (number >= (1 << 30)) {
307 return String.format("%.1fG", number / (double) (1 << 30));
309 if (number >= (1 << 20)) {
310 return String.format("%.1fM", number / (double) (1 << 20));
312 if (number >= (1 << 10)) {
313 return String.format("%.1fK", number / (double) (1 << 10));
315 return String.format("%dB", number);
319 * Returns the estimated time left for the given transfer.
322 * The DCC receiver to get the time left for
323 * @return The time left for the transfer, or “unknown” if the time can not be
326 private static String getTimeLeft(DccReceiver dccReceiver) {
327 if ((dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) {
330 long secondsLeft = (dccReceiver.size() - dccReceiver.progress()) / dccReceiver.currentRate();
331 if (secondsLeft > 3600) {
332 return String.format("%02d:%02d:%02d", secondsLeft / 3600, (secondsLeft / 60) % 60, secondsLeft % 60);
334 return String.format("%02d:%02d", (secondsLeft / 60) % 60, secondsLeft % 60);
337 /** Container for result information. */
338 private static class Result implements Comparable<Result> {
340 /** {@link Predicate} that matches {@link Result}s that contain an archive. */
341 private static final Predicate<Result> isArchive = new Predicate<Result>() {
343 /** All suffixes that are recognized as archives. */
344 private final List<String> archiveSuffixes = Arrays.asList("rar", "tar", "zip", "tar.gz", "tar.bz2", "tar.lzma", "7z");
347 public boolean apply(Result result) {
348 for (String suffix : archiveSuffixes) {
349 if (result.pack().name().toLowerCase().endsWith(suffix)) {
358 * {@link Comparator} for {@link Result}s that sorts archives (as per {@link
359 * #isArchive} to the back of the list.
361 private static final Comparator<Result> packArchiveComparator = new Comparator<Result>() {
363 public int compare(Result leftResult, Result rightResult) {
364 if (isArchive.apply(leftResult) && !isArchive.apply(rightResult)) {
367 if (!isArchive.apply(leftResult) && isArchive.apply(rightResult)) {
375 * {@link Comparator} for bot nicknames. It comprises different strategies:
376 * one name pattern is preferred (and thus listed first), one pattern is
377 * disliked (and thus listed last), the rest is sorted alphabetically.
379 private static final Comparator<Result> botNameComparator = new Comparator<Result>() {
381 /** Regular expression pattern for preferred names. */
382 private final Pattern preferredNames = Pattern.compile("(?i)[^\\w]EUR?[^\\w]");
384 /** Regular expression pattern for disliked names. */
385 private final Pattern dislikedNames = Pattern.compile("(?i)[^\\w]USA?[^\\w]");
388 public int compare(Result leftResult, Result rightResult) {
389 String leftBotName = leftResult.bot().name();
390 String rightBotName = rightResult.bot().name();
391 /* preferred names to the front! */
392 if (preferredNames.matcher(leftBotName).find() && !preferredNames.matcher(rightBotName).find()) {
395 if (preferredNames.matcher(rightBotName).find() && !preferredNames.matcher(leftBotName).find()) {
398 /* disliked names to the back. */
399 if (dislikedNames.matcher(leftBotName).find() && !dislikedNames.matcher(rightBotName).find()) {
402 if (dislikedNames.matcher(rightBotName).find() && !dislikedNames.matcher(leftBotName).find()) {
410 * {@link Comparator} for {@link Result}s that sorts them by the name of the
413 private static final Comparator<Result> packNameComparator = new Comparator<Result>() {
415 public int compare(Result leftResult, Result rightResult) {
416 return leftResult.pack().name().compareToIgnoreCase(rightResult.pack().name());
420 /** The bot carrying the pack. */
421 private final Bot bot;
424 private final Pack pack;
427 * Creates a new result.
430 * The bot carrying the pack
434 private Result(Bot bot, Pack pack) {
444 * Returns the bot carrying the pack.
446 * @return The bot carrying the pack
462 // COMPARABLE METHODS
466 public int compareTo(Result result) {
467 return ComparisonChain.start()
468 .compare(this, result, packArchiveComparator)
469 .compare(this, result, botNameComparator)
470 .compare(this, result, packNameComparator).result();