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