741ed6ab76776bbaec6c982751e00c9129cef34f
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / ListConnectionsCommand.java
1 /*
2  * XdccDownloader - ListConnectionsCommand.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 static com.google.common.collect.Lists.newArrayList;
21 import static java.util.Collections.emptyList;
22 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.f;
23
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.util.Collection;
27 import java.util.List;
28
29 import net.pterodactylus.irc.Connection;
30 import net.pterodactylus.xdcc.core.Core;
31
32 /**
33  * Command that will list all current connectios.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class ListConnectionsCommand implements Command {
38
39         /** The core to operate on. */
40         private final Core core;
41
42         /**
43          * Creates a new list connections command.
44          *
45          * @param core
46          *              The core to operate on
47          */
48         public ListConnectionsCommand(Core core) {
49                 this.core = core;
50         }
51
52         //
53         // COMMAND METHODS
54         //
55
56         @Override
57         public String getName() {
58                 return "connections";
59         }
60
61         @Override
62         public Collection<String> getAliases() {
63                 return emptyList();
64         }
65
66         @Override
67         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
68                 List<Connection> lastConnections = newArrayList();
69                 int counter = 0;
70                 for (Connection connection : core.connections()) {
71                         lastConnections.add(connection);
72                         outputWriter.write(String.format("[%d] %s:%d, %s/s\n", counter++, connection.hostname(), connection.port(), f(connection.getInputRate())));
73                 }
74                 outputWriter.write("End of connections.\n");
75                 outputWriter.flush();
76                 return state.setLastConnections(lastConnections);
77         }
78
79 }