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.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.concurrent.Executors;
29 import java.util.logging.ConsoleHandler;
30 import java.util.logging.Formatter;
31 import java.util.logging.Level;
32 import java.util.logging.LogRecord;
33 import java.util.logging.Logger;
35 import net.pterodactylus.irc.Connection;
36 import net.pterodactylus.xdcc.core.Core;
37 import net.pterodactylus.xdcc.data.Channel;
38 import net.pterodactylus.xdcc.data.Download;
39 import net.pterodactylus.xdcc.data.Network;
40 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
41 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
42 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
43 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
45 import com.google.common.eventbus.AsyncEventBus;
46 import com.google.common.eventbus.EventBus;
47 import com.lexicalscope.jewel.cli.Option;
48 import org.codehaus.jackson.map.ObjectMapper;
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 private static final String VERSION = "0.1-SNAPSHOT";
59 public static String version() {
63 public static void main(String... arguments) throws Exception {
65 ConsoleHandler consoleHandler = new ConsoleHandler();
66 consoleHandler.setFormatter(new Formatter() {
69 public String format(LogRecord logRecord) {
70 return String.format("%1$tF %1$tT %2$s %3$s\n", logRecord.getMillis(), logRecord.getLoggerName(), logRecord.getMessage());
73 consoleHandler.setLevel(Level.ALL);
74 Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]);
75 Logger.getLogger("").addHandler(consoleHandler);
76 Logger.getLogger("").setLevel(Level.ALL);
77 Logger.getLogger(Connection.class.getName()).setLevel(Level.INFO);
78 Logger.getLogger(Core.class.getName()).setLevel(Level.INFO);
80 Parameters parameters = parseArguments(Parameters.class, arguments);
82 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
83 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
85 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
86 eventBus.register(core);
88 for (Configuration.Network network : configuration.getNetworks()) {
89 NetworkBuilder networkBuilder = Network.builder(network.getName());
90 for (Configuration.Network.Server server : network.getServers()) {
91 ServerBuilder serverBuilder = networkBuilder.addServer();
92 serverBuilder.at(server.getHostname());
93 for (int unencryptedPort : server.getUnencryptedPorts()) {
94 serverBuilder.port(unencryptedPort);
96 for (int encryptedPort : server.getEncryptedPorts()) {
97 serverBuilder.sslPort(encryptedPort);
99 serverBuilder.endServer();
101 Network createdNetwork = networkBuilder.build();
102 for (String channel : network.getChannels()) {
103 core.addChannel(new Channel(createdNetwork, channel));
107 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
109 CommandReader commandReader = new CommandReader(core, new InputStreamReader(System.in, "UTF-8"), new OutputStreamWriter(System.out, "UTF-8"), failedDownloads);
110 commandReader.start();
111 eventBus.register(commandReader);
113 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
114 networkAcceptor.start();
120 * Defines the command-line parameters.
122 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
124 private interface Parameters {
126 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
127 String getConfiguration();