Don’t start a command reader on stdin/stdout.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / main / Main.java
index d56ccb1..5601f05 100644 (file)
 
 package net.pterodactylus.xdcc.main;
 
+import static com.lexicalscope.jewel.cli.CliFactory.parseArguments;
+
 import java.io.File;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.Executors;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Formatter;
@@ -30,15 +33,16 @@ 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.Download;
 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 com.lexicalscope.jewel.cli.Option;
 import org.codehaus.jackson.map.ObjectMapper;
 
 /**
@@ -71,8 +75,10 @@ public class Main {
                Logger.getLogger(Connection.class.getName()).setLevel(Level.INFO);
                Logger.getLogger(Core.class.getName()).setLevel(Level.INFO);
 
+               Parameters parameters = parseArguments(Parameters.class, arguments);
+
                EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
-               Configuration configuration = new ObjectMapper().readValue(new File("configuration.json"), Configuration.class);
+               Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
 
                Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
                eventBus.register(core);
@@ -96,18 +102,24 @@ public class Main {
                        }
                }
 
-               CommandReader commandReader = new CommandReader(core, new InputStreamReader(System.in, "UTF-8"), new OutputStreamWriter(System.out, "UTF-8"));
-               commandReader.start();
-               eventBus.register(commandReader);
+               Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
 
-               TelnetInterface telnetInterface = new TelnetInterface(core, configuration.getTelnetPort());
-               telnetInterface.start();
-               eventBus.register(telnetInterface);
-
-               NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, core, configuration.getTelnetPort());
+               NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
                networkAcceptor.start();
 
                core.start();
        }
 
+       /**
+        * Defines the command-line parameters.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private interface Parameters {
+
+               @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
+               String getConfiguration();
+
+       }
+
 }