2 * XdccDownloader - Main.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.main;
20 import static com.lexicalscope.jewel.cli.CliFactory.parseArguments;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.concurrent.CopyOnWriteArraySet;
26 import java.util.concurrent.Executors;
28 import net.pterodactylus.xdcc.core.Core;
29 import net.pterodactylus.xdcc.data.Channel;
30 import net.pterodactylus.xdcc.data.Download;
31 import net.pterodactylus.xdcc.data.Network;
32 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
33 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
34 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
35 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
37 import com.google.common.eventbus.AsyncEventBus;
38 import com.google.common.eventbus.EventBus;
39 import com.lexicalscope.jewel.cli.Option;
40 import org.codehaus.jackson.map.ObjectMapper;
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 private static final String VERSION = "0.1-SNAPSHOT";
51 public static String version() {
55 public static void main(String... arguments) throws Exception {
57 Parameters parameters = parseArguments(Parameters.class, arguments);
59 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
60 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
62 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
63 eventBus.register(core);
65 for (Configuration.Network network : configuration.getNetworks()) {
66 NetworkBuilder networkBuilder = Network.builder(network.getName());
67 for (Configuration.Network.Server server : network.getServers()) {
68 ServerBuilder serverBuilder = networkBuilder.addServer();
69 serverBuilder.at(server.getHostname());
70 for (int unencryptedPort : server.getUnencryptedPorts()) {
71 serverBuilder.port(unencryptedPort);
73 for (int encryptedPort : server.getEncryptedPorts()) {
74 serverBuilder.sslPort(encryptedPort);
76 serverBuilder.endServer();
78 Network createdNetwork = networkBuilder.build();
79 for (String channel : network.getChannels()) {
80 core.addChannel(new Channel(createdNetwork, channel));
84 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
86 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
87 networkAcceptor.start();
93 * Defines the command-line parameters.
95 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
97 private interface Parameters {
99 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
100 String getConfiguration();