Search in sources :

Example 1 with StartServer

use of com.biglybt.ui.common.StartServer in project BiglyBT by BiglySoftware.

the class Main method main.

public static void main(String[] args) {
    if (DEBUG_STARTUPTIME) {
        lastDebugTime = System.currentTimeMillis();
    }
    if (Launcher.checkAndLaunch(Main.class, args))
        return;
    // This *has* to be done first as it sets system properties that are read and cached by Java
    COConfigurationManager.preInitialise();
    if (DEBUG_STARTUPTIME) {
        logTime("args: " + Arrays.toString(args));
    }
    Thread.currentThread().setName(Constants.APP_NAME);
    String mi_str = System.getProperty("MULTI_INSTANCE");
    boolean mi = mi_str != null && mi_str.equalsIgnoreCase("true");
    if (DEBUG_STARTUPTIME) {
        logTime("preInit");
    }
    try {
        // Build a list of UIS
        Options uiOptions = new Options();
        Builder builder = Option.builder("u").longOpt("ui").argName("uis").hasArg();
        uiOptions.addOption(builder.build());
        if (Constants.isWindows) {
            builder = Option.builder("console");
            uiOptions.addOption(builder.build());
        }
        try {
            CommandLine commandLine = new DefaultParser().parse(uiOptions, args, true);
            buildUIList(commandLine);
        } catch (ParseException e) {
        }
        // Add UIS Command Line Options
        Options options = UIConst.buildOptions();
        commands = UIConst.buildCommandLine(options, args);
        if (commands == null) {
            System.exit(0);
        }
        if (DEBUG_STARTUPTIME) {
            logTime("buildCommandLine");
        }
        if (!mi) {
            startServer = new StartServer();
            if (startServer.getServerState() == StartServer.STATE_FAULTY) {
                System.setProperty("transitory.startup", "1");
                // looks like there's already a process listening on 127.0.0.1:<port>
                // attempt to pass args to existing instance
                // First, do some OSX magic because parameters are passed via OpenDocument API and other callbacks
                args = CocoaMagic(args);
                if (!new CoreSingleInstanceClient().sendArgs(args, 5000)) {
                    // arg passing attempt failed, so start core anyway
                    String msg = "There appears to be another process already listening on socket [127.0.0.1:" + Constants.INSTANCE_PORT + "].\n\nLocate and terminate the other program or change the control port - <a href=\"" + Constants.URL_WIKI + "w/Commandline_options#Changing_the_Control_Port\">see the wiki for details</a>.";
                    System.err.println(msg);
                    return;
                } else {
                    // we sent params to other core, don't init the core
                    return;
                }
            }
            if (commands.hasOption("closedown") || commands.hasOption("shutdown") || commands.hasOption("restart")) {
                return;
            }
            if (DEBUG_STARTUPTIME) {
                logTime("StartServer");
            }
        } else {
            System.out.println("MULTI_INSTANCE enabled");
        }
        // Special Exit if user ask for help
        if (commands != null && commands.hasOption('h')) {
            HelpFormatter hf = new HelpFormatter();
            hf.setOptionComparator(null);
            hf.printHelp("[options] [torrent [torrent ...]]", options);
            if (startServer != null) {
                startServer.stopIt();
            }
            System.exit(0);
        }
        boolean isFirst = true;
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.init(isFirst, (UIConst.UIS.size() > 1));
            isFirst = false;
        }
        neverStarted = true;
        core = CoreFactory.create();
        if (DEBUG_STARTUPTIME) {
            logTime("Core Create");
        }
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.coreCreated(core);
        }
        if (DEBUG_STARTUPTIME) {
            logTime("UIConst.set" + Constants.AZUREUS_NAME + "Core");
        }
        UIConst.processArgs(commands, options, args);
        if (DEBUG_STARTUPTIME) {
            logTime("UIConst.processArgs");
        }
        if (startServer != null) {
            startServer.setDaemon(true);
            startServer.start();
        }
        neverStarted = !core.isStarted();
        core.addLifecycleListener(new CoreLifecycleAdapter() {

            @Override
            public void started(Core core) {
                Main.neverStarted = false;
            }

            @Override
            public void stopping(Core core) {
                Main.stopping = true;
            }

            @Override
            public void stopped(Core core) {
                if (startServer != null) {
                    startServer.stopIt();
                }
                Main.stopped = true;
            }
        });
        for (IUserInterface ui : UIConst.UIS.values()) {
            ui.takeMainThread();
            if (stopping) {
                break;
            }
        }
        if (neverStarted) {
            if (DEBUG_STARTUPTIME) {
                logTime("takeMainThread");
            }
            core.start();
            if (DEBUG_STARTUPTIME) {
                logTime("coreStart");
            }
        }
        if (!stopping) {
            // no one took the main thread!
            while (!stopped) {
                try {
                    Thread.sleep(200);
                    // (not case "-u console,Swt")
                    if (newUI != null) {
                        IUserInterface threadTaker = newUI;
                        newUI = null;
                        threadTaker.takeMainThread();
                    }
                } catch (InterruptedException e) {
                }
            }
        }
    } catch (CoreException e) {
        System.out.println("Start fails:");
        e.printStackTrace();
    }
    if (DEBUG_STARTUPTIME) {
        logTime("DONE");
    }
}
Also used : CoreSingleInstanceClient(com.biglybt.core.impl.CoreSingleInstanceClient) CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) IUserInterface(com.biglybt.ui.common.IUserInterface) Builder(org.apache.commons.cli.Option.Builder) CoreException(com.biglybt.core.CoreException) StartServer(com.biglybt.ui.common.StartServer) Core(com.biglybt.core.Core)

Aggregations

Core (com.biglybt.core.Core)1 CoreException (com.biglybt.core.CoreException)1 CoreLifecycleAdapter (com.biglybt.core.CoreLifecycleAdapter)1 CoreSingleInstanceClient (com.biglybt.core.impl.CoreSingleInstanceClient)1 IUserInterface (com.biglybt.ui.common.IUserInterface)1 StartServer (com.biglybt.ui.common.StartServer)1 Builder (org.apache.commons.cli.Option.Builder)1