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.irc.ConnectionFactory;
29 import net.pterodactylus.irc.DefaultConnectionFactory;
30 import net.pterodactylus.xdcc.core.Core;
31 import net.pterodactylus.xdcc.data.Channel;
32 import net.pterodactylus.xdcc.data.Download;
33 import net.pterodactylus.xdcc.data.Network;
34 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
35 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
36 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
37 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
39 import com.google.common.eventbus.AsyncEventBus;
40 import com.google.common.eventbus.EventBus;
41 import com.lexicalscope.jewel.cli.Option;
42 import org.codehaus.jackson.map.ObjectMapper;
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 private static final String VERSION = "0.1-SNAPSHOT";
53 public static String version() {
57 public static void main(String... arguments) throws Exception {
59 Parameters parameters = parseArguments(Parameters.class, arguments);
61 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
62 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
63 ConnectionFactory connectionFactory = new DefaultConnectionFactory(eventBus);
65 Core core = new Core(eventBus, connectionFactory, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
66 eventBus.register(core);
68 for (Configuration.Network network : configuration.getNetworks()) {
69 NetworkBuilder networkBuilder = Network.builder(network.getName());
70 for (Configuration.Network.Server server : network.getServers()) {
71 ServerBuilder serverBuilder = networkBuilder.addServer();
72 serverBuilder.at(server.getHostname());
73 for (int unencryptedPort : server.getUnencryptedPorts()) {
74 serverBuilder.port(unencryptedPort);
76 for (int encryptedPort : server.getEncryptedPorts()) {
77 serverBuilder.sslPort(encryptedPort);
79 serverBuilder.endServer();
81 Network createdNetwork = networkBuilder.build();
82 for (String channel : network.getChannels()) {
83 core.addChannel(new Channel(createdNetwork, channel));
87 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
89 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
90 networkAcceptor.start();
96 * Defines the command-line parameters.
98 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
100 private interface Parameters {
102 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
103 String getConfiguration();