8696380fd68d3f1dd7b70768f54f18ac8bac45d4
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
1 /*
2  * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
20 import java.io.BufferedReader;
21 import java.io.Reader;
22 import java.io.Writer;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Set;
27
28 import net.pterodactylus.irc.DccReceiver;
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.Bot;
31 import net.pterodactylus.xdcc.data.Pack;
32
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Sets;
35 import com.google.common.primitives.Ints;
36 import com.google.common.util.concurrent.AbstractExecutionThreadService;
37
38 /**
39  * Command interface for arbitrary {@link Reader}s.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class CommandReader extends AbstractExecutionThreadService {
44
45         /** The core being controlled. */
46         private final Core core;
47
48         /** The reader to read commands from. */
49         private final BufferedReader reader;
50
51         /** The writer to write the results to. */
52         private final Writer writer;
53
54         /**
55          * Creates a new command reader.
56          *
57          * @param core
58          *              The core being controlled
59          * @param reader
60          *              The reader to read commands from
61          * @param writer
62          *              The write to write results to
63          */
64         public CommandReader(Core core, Reader reader, Writer writer) {
65                 this.core = core;
66                 this.reader = new BufferedReader(reader);
67                 this.writer = writer;
68         }
69
70         //
71         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
72         //
73
74         @Override
75         protected void run() throws Exception {
76                 String lastLine = "";
77                 String line;
78                 final List<Result> lastResult = Lists.newArrayList();
79                 while ((line = reader.readLine()) != null) {
80                         if (line.equals("")) {
81                                 line = lastLine;
82                         }
83                         String[] words = line.split(" +");
84                         if (words[0].equalsIgnoreCase("search")) {
85                                 lastResult.clear();
86                                 for (Bot bot : core.bots()) {
87                                         for (Pack pack : bot) {
88                                                 boolean found = true;
89                                                 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
90                                                         if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
91                                                                 found = false;
92                                                                 break;
93                                                         }
94                                                         if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
95                                                                 found = false;
96                                                                 break;
97                                                         }
98                                                 }
99                                                 if (found) {
100                                                         lastResult.add(new Result(bot, pack));
101                                                 }
102                                         }
103                                 }
104                                 Collections.sort(lastResult);
105                                 int counter = 0;
106                                 for (Result result : lastResult) {
107                                         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()));
108                                 }
109                                 writer.write("End of Search.\n");
110                         } else if (words[0].equalsIgnoreCase("dcc")) {
111                                 int counter = 0;
112                                 for (DccReceiver dccReceiver : core.dccReceivers()) {
113                                         writer.write(String.format("[%d] %s (%s, ", counter++, dccReceiver.filename(), dccReceiver.size()));
114                                         if (dccReceiver.isRunning()) {
115                                                 writer.write(String.format("%.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
116                                         } else {
117                                                 if (dccReceiver.progress() >= dccReceiver.size()) {
118                                                         writer.write(String.format("complete, %s", f(dccReceiver.overallRate())));
119                                                 } else {
120                                                         writer.write(String.format("aborted at %.1f%%, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
121                                                 }
122                                         }
123                                         writer.write("/s)\n");
124                                 }
125                                 writer.write("End of DCCs.\n");
126                         } else if (words[0].equalsIgnoreCase("get")) {
127                                 Integer index = Ints.tryParse(words[1]);
128                                 if ((index != null) && (index < lastResult.size())) {
129                                         core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
130                                 }
131                         } else if (words[0].equalsIgnoreCase("stats")) {
132                                 int configuredChannelsCount = core.channels().size();
133                                 int joinedChannelsCount = core.joinedChannels().size();
134                                 int extraChannelsCount = core.extraChannels().size();
135                                 Collection<Bot> bots = core.bots();
136                                 Set<String> packNames = Sets.newHashSet();
137                                 int packsCount = 0;
138                                 for (Bot bot : bots) {
139                                         packsCount += bot.packs().size();
140                                         for (Pack pack : bot) {
141                                                 packNames.add(pack.name());
142                                         }
143                                 }
144
145                                 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()));
146                         }
147
148                         lastLine = line;
149                         writer.flush();
150                 }
151         }
152
153         //
154         // PRIVATE METHODS
155         //
156
157         /**
158          * Converts large numbers into a human-friendly format, by showing SI prefixes
159          * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
160          *
161          * @param number
162          *              The number to convert
163          * @return The converted number
164          */
165         private static String f(long number) {
166                 if (number >= (1 << 30)) {
167                         return String.format("%.1fG", number / (double) (1 << 30));
168                 }
169                 if (number >= (1 << 20)) {
170                         return String.format("%.1fM", number / (double) (1 << 20));
171                 }
172                 if (number >= (1 << 10)) {
173                         return String.format("%.1fK", number / (double) (1 << 10));
174                 }
175                 return String.format("%dB", number);
176         }
177
178         /** Container for result information. */
179         private static class Result implements Comparable<Result> {
180
181                 /** The bot carrying the pack. */
182                 private final Bot bot;
183
184                 /** The pack. */
185                 private final Pack pack;
186
187                 /**
188                  * Creates a new result.
189                  *
190                  * @param bot
191                  *              The bot carrying the pack
192                  * @param pack
193                  *              The pack
194                  */
195                 private Result(Bot bot, Pack pack) {
196                         this.bot = bot;
197                         this.pack = pack;
198                 }
199
200                 //
201                 // ACCESSORS
202                 //
203
204                 /**
205                  * Returns the bot carrying the pack.
206                  *
207                  * @return The bot carrying the pack
208                  */
209                 public Bot bot() {
210                         return bot;
211                 }
212
213                 /**
214                  * Returns the pack.
215                  *
216                  * @return The pack
217                  */
218                 public Pack pack() {
219                         return pack;
220                 }
221
222                 //
223                 // COMPARABLE METHODS
224                 //
225
226                 @Override
227                 public int compareTo(Result result) {
228                         return pack().name().compareToIgnoreCase(result.pack().name());
229                 }
230
231         }
232
233 }