Remove old logger configuration.
[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.util.Collection;
24 import java.util.HashSet;
25 import java.util.concurrent.CopyOnWriteArraySet;
26 import java.util.concurrent.Executors;
27
28 import net.pterodactylus.xdcc.core.Core;
29 import net.pterodactylus.xdcc.data.Channel;
30 import net.pterodactylus.xdcc.data.Download;
31 import net.pterodactylus.xdcc.data.Network;
32 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
33 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
34 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
35 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
36
37 import com.google.common.eventbus.AsyncEventBus;
38 import com.google.common.eventbus.EventBus;
39 import com.lexicalscope.jewel.cli.Option;
40 import org.codehaus.jackson.map.ObjectMapper;
41
42 /**
43  * Xudocci main class.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class Main {
48
49         private static final String VERSION = "0.1-SNAPSHOT";
50
51         public static String version() {
52                 return VERSION;
53         }
54
55         public static void main(String... arguments) throws Exception {
56
57                 Parameters parameters = parseArguments(Parameters.class, arguments);
58
59                 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
60                 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
61
62                 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
63                 eventBus.register(core);
64
65                 for (Configuration.Network network : configuration.getNetworks()) {
66                         NetworkBuilder networkBuilder = Network.builder(network.getName());
67                         for (Configuration.Network.Server server : network.getServers()) {
68                                 ServerBuilder serverBuilder = networkBuilder.addServer();
69                                 serverBuilder.at(server.getHostname());
70                                 for (int unencryptedPort : server.getUnencryptedPorts()) {
71                                         serverBuilder.port(unencryptedPort);
72                                 }
73                                 for (int encryptedPort : server.getEncryptedPorts()) {
74                                         serverBuilder.sslPort(encryptedPort);
75                                 }
76                                 serverBuilder.endServer();
77                         }
78                         Network createdNetwork = networkBuilder.build();
79                         for (String channel : network.getChannels()) {
80                                 core.addChannel(new Channel(createdNetwork, channel));
81                         }
82                 }
83
84                 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
85
86                 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
87                 networkAcceptor.start();
88
89                 core.start();
90         }
91
92         /**
93          * Defines the command-line parameters.
94          *
95          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
96          */
97         private interface Parameters {
98
99                 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
100                 String getConfiguration();
101
102         }
103
104 }