Search in sources :

Example 1 with CoreSingleInstanceClient

use of com.biglybt.core.impl.CoreSingleInstanceClient in project BiglyBT by BiglySoftware.

the class ScriptBeforeStartup method main.

public static void main(String[] args) {
    // Set transitory so not everything gets loaded up. (such as the AEDiagnostic's tidy flag)
    System.setProperty("transitory.startup", "1");
    // Since stdout will be in a shell script, redirect any stdout not coming
    // from us to stderr
    sysout = System.out;
    try {
        System.setOut(new PrintStream(new FileOutputStream("/dev/stderr")));
    } catch (FileNotFoundException e) {
    }
    String mi_str = System.getProperty(PluginManager.PR_MULTI_INSTANCE);
    boolean mi = mi_str != null && mi_str.equalsIgnoreCase("true");
    if (!mi) {
        boolean argsSent = new CoreSingleInstanceClient().sendArgs(args, 500);
        if (argsSent) {
            // the client was open..
            sysout.println("exit");
            return;
        }
    }
    // If the after shutdown script didn't run or crapped out, then
    // don't run again..
    String scriptAfterShutdown = COConfigurationManager.getStringParameter("scriptaftershutdown", null);
    COConfigurationManager.removeParameter("scriptaftershutdown.exit");
    COConfigurationManager.removeParameter("scriptaftershutdown");
    COConfigurationManager.save();
    if (scriptAfterShutdown != null) {
        log("Script after " + Constants.APP_NAME + " shutdown did not run.. running now");
        sysout.println(scriptAfterShutdown);
        if (!scriptAfterShutdown.contains("$0")) {
            // doesn't have a restart.. add one
            sysout.println("echo \"Restarting " + Constants.APP_NAME + "..\"");
            sysout.println("$0\n");
        }
        // exit is a requirement
        sysout.println("exit");
        return;
    }
    boolean useGTK2 = COConfigurationManager.getBooleanParameter("ui.useGTK2");
    if (useGTK2) {
        sysout.println("export SWT_GTK3=0");
    }
    try {
        Class claDisplay = Class.forName("org.eclipse.swt.widgets.Display");
        claDisplay.newInstance();
    } catch (Throwable e) {
        boolean useSwing = true;
        for (String arg : args) {
            if (arg.equals("-h") || arg.startsWith("-u")) {
                useSwing = false;
                break;
            }
        }
        String error = e.toString();
        if ((e instanceof UnsatisfiedLinkError) && !new File("/etc/gtk-3.0").isDirectory()) {
            error = "No GTK3 found. " + (e.toString());
        }
        System.err.println("SWT check failed with: " + error);
        if (!useSwing) {
            return;
        }
        if (System.getenv("TERM") != null) {
            try {
                int i = JOptionPane.showConfirmDialog(null, "GUI could not be loaded. " + error + "\nLaunch in console mode instead?", "Can't Launch " + Constants.APP_NAME, JOptionPane.YES_NO_OPTION);
                if (i != JOptionPane.OK_OPTION) {
                    sysout.println("exit");
                    return;
                }
            } catch (Throwable t) {
            }
            sysout.println("OTHER_PARAMS=\"${OTHER_PARAMS} --ui=console\"");
        } else {
            try {
                JOptionPane.showMessageDialog(null, "GUI could not be loaded. " + error, "Can't Launch " + Constants.APP_NAME, JOptionPane.ERROR_MESSAGE);
                sysout.println("exit");
            } catch (Throwable t) {
                System.err.println(t.toString());
                sysout.println("OTHER_PARAMS=\"${OTHER_PARAMS} --ui=console\"");
            }
        }
    }
}
Also used : CoreSingleInstanceClient(com.biglybt.core.impl.CoreSingleInstanceClient)

Example 2 with CoreSingleInstanceClient

use of com.biglybt.core.impl.CoreSingleInstanceClient 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

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