From: David ‘Bombe’ Roden Date: Fri, 18 Oct 2013 04:59:38 +0000 (+0200) Subject: Add main class. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=56a31fda7e5fb351a93f89c058c12f2e54678789;p=xudocci.git Add main class. --- diff --git a/src/main/java/net/pterodactylus/xdcc/main/Main.java b/src/main/java/net/pterodactylus/xdcc/main/Main.java new file mode 100644 index 0000000..14e3bda --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/main/Main.java @@ -0,0 +1,113 @@ +/* + * XdccDownloader - Main.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.xdcc.main; + +import java.io.File; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.concurrent.Executors; +import java.util.logging.ConsoleHandler; +import java.util.logging.Formatter; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import net.pterodactylus.irc.Connection; +import net.pterodactylus.xdcc.core.Core; +import net.pterodactylus.xdcc.data.Channel; +import net.pterodactylus.xdcc.data.Network; +import net.pterodactylus.xdcc.data.Network.NetworkBuilder; +import net.pterodactylus.xdcc.data.Network.ServerBuilder; +import net.pterodactylus.xdcc.ui.stdin.CommandReader; +import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter; +import net.pterodactylus.xdcc.ui.telnet.TelnetInterface; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.EventBus; +import org.codehaus.jackson.map.ObjectMapper; + +/** + * Xudocci main class. + * + * @author David ‘Bombe’ Roden + */ +public class Main { + + private static final String VERSION = "0.1-SNAPSHOT"; + + public static String version() { + return VERSION; + } + + public static void main(String... arguments) throws Exception { + + ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setFormatter(new Formatter() { + + @Override + public String format(LogRecord logRecord) { + return String.format("%1$tF %1$tT %2$s %3$s\n", logRecord.getMillis(), logRecord.getLoggerName(), logRecord.getMessage()); + } + }); + consoleHandler.setLevel(Level.ALL); + Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]); + Logger.getLogger("").addHandler(consoleHandler); + Logger.getLogger("").setLevel(Level.ALL); + Logger.getLogger(Connection.class.getName()).setLevel(Level.INFO); + Logger.getLogger(Core.class.getName()).setLevel(Level.INFO); + + EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor()); + Configuration configuration = new ObjectMapper().readValue(new File("configuration.json"), Configuration.class); + + Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory()); + eventBus.register(core); + + for (Configuration.Network network : configuration.getNetworks()) { + NetworkBuilder networkBuilder = Network.builder(network.getName()); + for (Configuration.Network.Server server : network.getServers()) { + ServerBuilder serverBuilder = networkBuilder.addServer(); + serverBuilder.at(server.getHostname()); + for (int unencryptedPort : server.getUnencryptedPorts()) { + serverBuilder.port(unencryptedPort); + } + for (int encryptedPort : server.getEncryptedPorts()) { + serverBuilder.sslPort(encryptedPort); + } + serverBuilder.endServer(); + } + Network createdNetwork = networkBuilder.build(); + for (String channel : network.getChannels()) { + core.addChannel(new Channel(createdNetwork, channel)); + } + } + + CommandReader commandReader = new CommandReader(core, new InputStreamReader(System.in, "UTF-8"), new OutputStreamWriter(System.out, "UTF-8")); + commandReader.start(); + eventBus.register(commandReader); + + TelnetInterface telnetInterface = new TelnetInterface(core, configuration.getTelnetPort()); + telnetInterface.start(); + eventBus.register(telnetInterface); + + NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, core); + networkAcceptor.start(); + + core.start(); + } + +}