Make it possible to give the configuration file location as parameter.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / main / Main.java
1 /*
2  * XdccDownloader - Main.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.main;
19
20 import static com.lexicalscope.jewel.cli.CliFactory.parseArguments;
21
22 import java.io.File;
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;
34
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;
44
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;
49
50 /**
51  * Xudocci main class.
52  *
53  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54  */
55 public class Main {
56
57         private static final String VERSION = "0.1-SNAPSHOT";
58
59         public static String version() {
60                 return VERSION;
61         }
62
63         public static void main(String... arguments) throws Exception {
64
65                 ConsoleHandler consoleHandler = new ConsoleHandler();
66                 consoleHandler.setFormatter(new Formatter() {
67
68                         @Override
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());
71                         }
72                 });
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);
79
80                 Parameters parameters = parseArguments(Parameters.class, arguments);
81
82                 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
83                 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
84
85                 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
86                 eventBus.register(core);
87
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);
95                                 }
96                                 for (int encryptedPort : server.getEncryptedPorts()) {
97                                         serverBuilder.sslPort(encryptedPort);
98                                 }
99                                 serverBuilder.endServer();
100                         }
101                         Network createdNetwork = networkBuilder.build();
102                         for (String channel : network.getChannels()) {
103                                 core.addChannel(new Channel(createdNetwork, channel));
104                         }
105                 }
106
107                 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
108
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);
112
113                 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
114                 networkAcceptor.start();
115
116                 core.start();
117         }
118
119         /**
120          * Defines the command-line parameters.
121          *
122          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
123          */
124         private interface Parameters {
125
126                 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
127                 String getConfiguration();
128
129         }
130
131 }