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;
27 import java.util.logging.ConsoleHandler;
28 import java.util.logging.Formatter;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31 import java.util.logging.Logger;
33 import net.pterodactylus.irc.Connection;
34 import net.pterodactylus.xdcc.core.Core;
35 import net.pterodactylus.xdcc.data.Channel;
36 import net.pterodactylus.xdcc.data.Download;
37 import net.pterodactylus.xdcc.data.Network;
38 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
39 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
40 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
41 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
43 import com.google.common.eventbus.AsyncEventBus;
44 import com.google.common.eventbus.EventBus;
45 import com.lexicalscope.jewel.cli.Option;
46 import org.codehaus.jackson.map.ObjectMapper;
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 private static final String VERSION = "0.1-SNAPSHOT";
57 public static String version() {
61 public static void main(String... arguments) throws Exception {
63 ConsoleHandler consoleHandler = new ConsoleHandler();
64 consoleHandler.setFormatter(new Formatter() {
67 public String format(LogRecord logRecord) {
68 return String.format("%1$tF %1$tT %2$s %3$s\n", logRecord.getMillis(), logRecord.getLoggerName(), logRecord.getMessage());
71 consoleHandler.setLevel(Level.ALL);
72 Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]);
73 Logger.getLogger("").addHandler(consoleHandler);
74 Logger.getLogger("").setLevel(Level.ALL);
75 Logger.getLogger(Connection.class.getName()).setLevel(Level.INFO);
76 Logger.getLogger(Core.class.getName()).setLevel(Level.INFO);
78 Parameters parameters = parseArguments(Parameters.class, arguments);
80 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
81 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
83 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
84 eventBus.register(core);
86 for (Configuration.Network network : configuration.getNetworks()) {
87 NetworkBuilder networkBuilder = Network.builder(network.getName());
88 for (Configuration.Network.Server server : network.getServers()) {
89 ServerBuilder serverBuilder = networkBuilder.addServer();
90 serverBuilder.at(server.getHostname());
91 for (int unencryptedPort : server.getUnencryptedPorts()) {
92 serverBuilder.port(unencryptedPort);
94 for (int encryptedPort : server.getEncryptedPorts()) {
95 serverBuilder.sslPort(encryptedPort);
97 serverBuilder.endServer();
99 Network createdNetwork = networkBuilder.build();
100 for (String channel : network.getChannels()) {
101 core.addChannel(new Channel(createdNetwork, channel));
105 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
107 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
108 networkAcceptor.start();
114 * Defines the command-line parameters.
116 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
118 private interface Parameters {
120 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
121 String getConfiguration();