Search in sources :

Example 11 with AESemaphore

use of com.biglybt.core.util.AESemaphore in project BiglyBT by BiglySoftware.

the class DHTTransportUDPContactImpl method isAlive.

@Override
public boolean isAlive(long timeout) {
    final AESemaphore sem = new AESemaphore("DHTTransportContact:alive");
    final boolean[] alive = { false };
    try {
        sendPing(new DHTTransportReplyHandlerAdapter() {

            @Override
            public void pingReply(DHTTransportContact contact) {
                alive[0] = true;
                sem.release();
            }

            @Override
            public void failed(DHTTransportContact contact, Throwable cause) {
                sem.release();
            }
        });
        sem.reserve(timeout);
        return (alive[0]);
    } catch (Throwable e) {
        return (false);
    }
}
Also used : AESemaphore(com.biglybt.core.util.AESemaphore)

Example 12 with AESemaphore

use of com.biglybt.core.util.AESemaphore in project BiglyBT by BiglySoftware.

the class ConfigSectionConnectionProxy method configSectionCreate.

@Override
public Composite configSectionCreate(final Composite parent) {
    GridData gridData;
    GridLayout layout;
    Composite cSection = new Composite(parent, SWT.NULL);
    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    Utils.setLayoutData(cSection, gridData);
    layout = new GridLayout();
    layout.numColumns = 2;
    cSection.setLayout(layout);
    int userMode = COConfigurationManager.getIntParameter("User Mode");
    if (userMode < REQUIRED_MODE) {
        Label label = new Label(cSection, SWT.WRAP);
        gridData = new GridData();
        gridData.horizontalSpan = 2;
        Utils.setLayoutData(label, gridData);
        final String[] modeKeys = { "ConfigView.section.mode.beginner", "ConfigView.section.mode.intermediate", "ConfigView.section.mode.advanced" };
        String param1, param2;
        if (REQUIRED_MODE < modeKeys.length)
            param1 = MessageText.getString(modeKeys[REQUIRED_MODE]);
        else
            param1 = String.valueOf(REQUIRED_MODE);
        if (userMode < modeKeys.length)
            param2 = MessageText.getString(modeKeys[userMode]);
        else
            param2 = String.valueOf(userMode);
        label.setText(MessageText.getString("ConfigView.notAvailableForMode", new String[] { param1, param2 }));
        return cSection;
    }
    // ////////////////////  PROXY GROUP /////////////////
    Group gProxyTracker = new Group(cSection, SWT.NULL);
    Messages.setLanguageText(gProxyTracker, "ConfigView.section.proxy.group.tracker");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    Utils.setLayoutData(gProxyTracker, gridData);
    layout = new GridLayout();
    layout.numColumns = 2;
    gProxyTracker.setLayout(layout);
    final BooleanParameter enableProxy = new BooleanParameter(gProxyTracker, "Enable.Proxy", "ConfigView.section.proxy.enable_proxy");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    enableProxy.setLayoutData(gridData);
    final BooleanParameter enableSocks = new BooleanParameter(gProxyTracker, "Enable.SOCKS", "ConfigView.section.proxy.enable_socks");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    enableSocks.setLayoutData(gridData);
    Label lHost = new Label(gProxyTracker, SWT.NULL);
    Messages.setLanguageText(lHost, "ConfigView.section.proxy.host");
    final StringParameter pHost = new StringParameter(gProxyTracker, "Proxy.Host", "", false);
    gridData = new GridData();
    gridData.widthHint = 105;
    pHost.setLayoutData(gridData);
    Label lPort = new Label(gProxyTracker, SWT.NULL);
    Messages.setLanguageText(lPort, "ConfigView.section.proxy.port");
    final StringParameter pPort = new StringParameter(gProxyTracker, "Proxy.Port", "", false);
    gridData = new GridData();
    gridData.widthHint = 40;
    pPort.setLayoutData(gridData);
    Label lUser = new Label(gProxyTracker, SWT.NULL);
    Messages.setLanguageText(lUser, "ConfigView.section.proxy.username");
    final StringParameter pUser = new StringParameter(gProxyTracker, "Proxy.Username", false);
    gridData = new GridData();
    gridData.widthHint = 105;
    pUser.setLayoutData(gridData);
    Label lPass = new Label(gProxyTracker, SWT.NULL);
    Messages.setLanguageText(lPass, "ConfigView.section.proxy.password");
    final StringParameter pPass = new StringParameter(gProxyTracker, "Proxy.Password", "", false);
    gridData = new GridData();
    gridData.widthHint = 105;
    pPass.setLayoutData(gridData);
    final BooleanParameter trackerDNSKill = new BooleanParameter(gProxyTracker, "Proxy.SOCKS.Tracker.DNS.Disable", "ConfigView.section.proxy.no.local.dns");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    trackerDNSKill.setLayoutData(gridData);
    final NetworkAdminSocksProxy[] test_proxy = { null };
    final Button test_socks = new Button(gProxyTracker, SWT.PUSH);
    Messages.setLanguageText(test_socks, "ConfigView.section.proxy.testsocks");
    test_socks.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            final NetworkAdminSocksProxy target;
            synchronized (test_proxy) {
                target = test_proxy[0];
            }
            if (target != null) {
                final TextViewerWindow viewer = new TextViewerWindow(MessageText.getString("ConfigView.section.proxy.testsocks.title"), null, "Testing SOCKS connection to " + target.getHost() + ":" + target.getPort(), false);
                final AESemaphore test_done = new AESemaphore("");
                new AEThread2("SOCKS test") {

                    @Override
                    public void run() {
                        try {
                            String[] vers = target.getVersionsSupported();
                            String ver = "";
                            for (String v : vers) {
                                ver += (ver.length() == 0 ? "" : ", ") + v;
                            }
                            appendText(viewer, "\r\nConnection OK - supported version(s): " + ver);
                        } catch (Throwable e) {
                            appendText(viewer, "\r\n" + Debug.getNestedExceptionMessage(e));
                        } finally {
                            test_done.release();
                        }
                    }
                }.start();
                new AEThread2("SOCKS test dotter") {

                    @Override
                    public void run() {
                        while (!test_done.reserveIfAvailable()) {
                            appendText(viewer, ".");
                            try {
                                Thread.sleep(500);
                            } catch (Throwable e) {
                                break;
                            }
                        }
                    }
                }.start();
            }
        }

        private void appendText(final TextViewerWindow viewer, final String line) {
            Utils.execSWTThread(new Runnable() {

                @Override
                public void run() {
                    if (!viewer.isDisposed()) {
                        viewer.append2(line);
                    }
                }
            });
        }
    });
    Parameter[] socks_params = { enableProxy, enableSocks, pHost, pPort, pUser, pPass, trackerDNSKill };
    ParameterChangeAdapter socks_adapter = new ParameterChangeAdapter() {

        @Override
        public void parameterChanged(Parameter p, boolean caused_internally) {
            if (test_socks.isDisposed()) {
                p.removeChangeListener(this);
            } else {
                if (!caused_internally) {
                    boolean enabled = enableProxy.isSelected() && enableSocks.isSelected() && pHost.getValue().trim().length() > 0 && pPort.getValue().trim().length() > 0;
                    boolean socks_enabled = enableProxy.isSelected() && enableSocks.isSelected();
                    trackerDNSKill.setEnabled(socks_enabled);
                    if (enabled) {
                        try {
                            int port = Integer.parseInt(pPort.getValue());
                            NetworkAdminSocksProxy nasp = NetworkAdmin.getSingleton().createSocksProxy(pHost.getValue(), port, pUser.getValue(), pPass.getValue());
                            synchronized (test_proxy) {
                                test_proxy[0] = nasp;
                            }
                        } catch (Throwable e) {
                            enabled = false;
                        }
                    }
                    if (!enabled) {
                        synchronized (test_proxy) {
                            test_proxy[0] = null;
                        }
                    }
                    final boolean f_enabled = enabled;
                    Utils.execSWTThread(new Runnable() {

                        @Override
                        public void run() {
                            if (!test_socks.isDisposed()) {
                                test_socks.setEnabled(f_enabled);
                            }
                        }
                    });
                }
            }
        }
    };
    for (Parameter p : socks_params) {
        p.addChangeListener(socks_adapter);
    }
    // init settings
    socks_adapter.parameterChanged(null, false);
    // //////////////////////////////////////////////
    Group gProxyPeer = new Group(cSection, SWT.NULL);
    Messages.setLanguageText(gProxyPeer, "ConfigView.section.proxy.group.peer");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    Utils.setLayoutData(gProxyPeer, gridData);
    layout = new GridLayout();
    layout.numColumns = 2;
    gProxyPeer.setLayout(layout);
    final BooleanParameter enableSocksPeer = new BooleanParameter(gProxyPeer, "Proxy.Data.Enable", "ConfigView.section.proxy.enable_socks.peer");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    enableSocksPeer.setLayoutData(gridData);
    final BooleanParameter socksPeerInform = new BooleanParameter(gProxyPeer, "Proxy.Data.SOCKS.inform", "ConfigView.section.proxy.peer.informtracker");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    socksPeerInform.setLayoutData(gridData);
    Label lSocksVersion = new Label(gProxyPeer, SWT.NULL);
    Messages.setLanguageText(lSocksVersion, "ConfigView.section.proxy.socks.version");
    String[] socks_types = { "V4", "V4a", "V5" };
    String[] dropLabels = new String[socks_types.length];
    String[] dropValues = new String[socks_types.length];
    for (int i = 0; i < socks_types.length; i++) {
        dropLabels[i] = socks_types[i];
        dropValues[i] = socks_types[i];
    }
    final StringListParameter socksType = new StringListParameter(gProxyPeer, "Proxy.Data.SOCKS.version", "V4", dropLabels, dropValues);
    final BooleanParameter sameConfig = new BooleanParameter(gProxyPeer, "Proxy.Data.Same", "ConfigView.section.proxy.peer.same");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    sameConfig.setLayoutData(gridData);
    Label lDataHost = new Label(gProxyPeer, SWT.NULL);
    Messages.setLanguageText(lDataHost, "ConfigView.section.proxy.host");
    StringParameter pDataHost = new StringParameter(gProxyPeer, "Proxy.Data.Host", "");
    gridData = new GridData();
    gridData.widthHint = 105;
    pDataHost.setLayoutData(gridData);
    Label lDataPort = new Label(gProxyPeer, SWT.NULL);
    Messages.setLanguageText(lDataPort, "ConfigView.section.proxy.port");
    StringParameter pDataPort = new StringParameter(gProxyPeer, "Proxy.Data.Port", "");
    gridData = new GridData();
    gridData.widthHint = 40;
    pDataPort.setLayoutData(gridData);
    Label lDataUser = new Label(gProxyPeer, SWT.NULL);
    Messages.setLanguageText(lDataUser, "ConfigView.section.proxy.username");
    StringParameter pDataUser = new StringParameter(gProxyPeer, "Proxy.Data.Username");
    gridData = new GridData();
    gridData.widthHint = 105;
    pDataUser.setLayoutData(gridData);
    Label lDataPass = new Label(gProxyPeer, SWT.NULL);
    Messages.setLanguageText(lDataPass, "ConfigView.section.proxy.password");
    StringParameter pDataPass = new StringParameter(gProxyPeer, "Proxy.Data.Password", "");
    gridData = new GridData();
    gridData.widthHint = 105;
    pDataPass.setLayoutData(gridData);
    final Control[] proxy_controls = new Control[] { enableSocks.getControl(), lHost, pHost.getControl(), lPort, pPort.getControl(), lUser, pUser.getControl(), lPass, pPass.getControl() };
    IAdditionalActionPerformer proxy_enabler = new GenericActionPerformer(new Control[] {}) {

        @Override
        public void performAction() {
            for (int i = 0; i < proxy_controls.length; i++) {
                proxy_controls[i].setEnabled(enableProxy.isSelected());
            }
        }
    };
    final Control[] proxy_peer_controls = new Control[] { lDataHost, pDataHost.getControl(), lDataPort, pDataPort.getControl(), lDataUser, pDataUser.getControl(), lDataPass, pDataPass.getControl() };
    final Control[] proxy_peer_details = new Control[] { sameConfig.getControl(), socksPeerInform.getControl(), socksType.getControl(), lSocksVersion };
    IAdditionalActionPerformer proxy_peer_enabler = new GenericActionPerformer(new Control[] {}) {

        @Override
        public void performAction() {
            for (int i = 0; i < proxy_peer_controls.length; i++) {
                proxy_peer_controls[i].setEnabled(enableSocksPeer.isSelected() && !sameConfig.isSelected());
            }
            for (int i = 0; i < proxy_peer_details.length; i++) {
                proxy_peer_details[i].setEnabled(enableSocksPeer.isSelected());
            }
        }
    };
    enableSocks.setAdditionalActionPerformer(proxy_enabler);
    enableProxy.setAdditionalActionPerformer(proxy_enabler);
    enableSocksPeer.setAdditionalActionPerformer(proxy_peer_enabler);
    sameConfig.setAdditionalActionPerformer(proxy_peer_enabler);
    // dns info
    Label label = new Label(cSection, SWT.WRAP);
    Messages.setLanguageText(label, "ConfigView.section.proxy.dns.info");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    // needed for wrap
    gridData.widthHint = 200;
    Utils.setLayoutData(label, gridData);
    // disable plugin proxies
    final BooleanParameter disablepps = new BooleanParameter(cSection, "Proxy.SOCKS.disable.plugin.proxies", "ConfigView.section.proxy.disable.plugin.proxies");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    disablepps.setLayoutData(gridData);
    // check on start
    final BooleanParameter checkOnStart = new BooleanParameter(cSection, "Proxy.Check.On.Start", "ConfigView.section.proxy.check.on.start");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    checkOnStart.setLayoutData(gridData);
    // icon
    final BooleanParameter showIcon = new BooleanParameter(cSection, "Proxy.SOCKS.ShowIcon", "ConfigView.section.proxy.show_icon");
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    showIcon.setLayoutData(gridData);
    final BooleanParameter flagIncoming = new BooleanParameter(cSection, "Proxy.SOCKS.ShowIcon.FlagIncoming", "ConfigView.section.proxy.show_icon.flag.incoming");
    gridData = new GridData();
    gridData.horizontalIndent = 50;
    gridData.horizontalSpan = 2;
    flagIncoming.setLayoutData(gridData);
    showIcon.setAdditionalActionPerformer(new ChangeSelectionActionPerformer(flagIncoming));
    // username info
    label = new Label(cSection, SWT.WRAP);
    gridData = new GridData();
    gridData.horizontalSpan = 2;
    Utils.setLayoutData(label, gridData);
    label.setText(MessageText.getString("ConfigView.section.proxy.username.info"));
    return cSection;
}
Also used : Group(org.eclipse.swt.widgets.Group) Listener(org.eclipse.swt.widgets.Listener) Label(org.eclipse.swt.widgets.Label) AESemaphore(com.biglybt.core.util.AESemaphore) GridLayout(org.eclipse.swt.layout.GridLayout) Control(org.eclipse.swt.widgets.Control) Button(org.eclipse.swt.widgets.Button) TextViewerWindow(com.biglybt.ui.swt.TextViewerWindow) Composite(org.eclipse.swt.widgets.Composite) NetworkAdminSocksProxy(com.biglybt.core.networkmanager.admin.NetworkAdminSocksProxy) AEThread2(com.biglybt.core.util.AEThread2) GridData(org.eclipse.swt.layout.GridData) Event(org.eclipse.swt.widgets.Event)

Example 13 with AESemaphore

use of com.biglybt.core.util.AESemaphore in project BiglyBT by BiglySoftware.

the class SimplePluginInstaller method install.

public boolean install() {
    try {
        installer = CoreFactory.getSingleton().getPluginManager().getPluginInstaller();
        StandardPlugin sp = installer.getStandardPlugin(plugin_id);
        if (sp == null) {
            throw (new Exception("Unknown plugin"));
        }
        Map<Integer, Object> properties = new HashMap<>();
        properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
        properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
        final AESemaphore sem = new AESemaphore("plugin-install");
        final Object[] result = new Object[] { null };
        instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {

            @Override
            public void completed() {
                synchronized (SimplePluginInstaller.this) {
                    completed = true;
                }
                result[0] = true;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void cancelled() {
                result[0] = new Exception("Cancelled");
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void failed(PluginException e) {
                result[0] = e;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }
        });
        boolean kill_it;
        synchronized (this) {
            kill_it = cancelled;
        }
        if (kill_it) {
            instance.cancel();
            action_listener.actionComplete(new Exception("Cancelled"));
            return (false);
        }
        instance.addListener(new UpdateCheckInstanceListener() {

            @Override
            public void cancelled(UpdateCheckInstance instance) {
            }

            @Override
            public void complete(UpdateCheckInstance instance) {
                Update[] updates = instance.getUpdates();
                for (final Update update : updates) {
                    ResourceDownloader[] rds = update.getDownloaders();
                    for (ResourceDownloader rd : rds) {
                        rd.addListener(new ResourceDownloaderAdapter() {

                            @Override
                            public void reportActivity(ResourceDownloader downloader, String activity) {
                            }

                            @Override
                            public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                if (listener != null) {
                                    listener.progress(percentage);
                                }
                            }
                        });
                    }
                }
            }
        });
        sem.reserve();
        action_listener.actionComplete(result[0]);
        return (result[0] instanceof Boolean);
    } catch (Throwable e) {
        if (listener != null) {
            listener.finished();
        }
        action_listener.actionComplete(e);
    }
    return false;
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) HashMap(java.util.HashMap) PluginException(com.biglybt.pif.PluginException) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) AESemaphore(com.biglybt.core.util.AESemaphore) Update(com.biglybt.pif.update.Update) PluginException(com.biglybt.pif.PluginException) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener)

Example 14 with AESemaphore

use of com.biglybt.core.util.AESemaphore in project BiglyBT by BiglySoftware.

the class Plugin method execute.

@Override
public void execute(String commandName, final ConsoleInput ci, List args) {
    if (args.isEmpty()) {
        printHelpExtra(ci.out, args);
        return;
    }
    String subcmd = (String) args.get(0);
    if (!java.util.Arrays.asList(new String[] { "location", "list", "listall", "status", "startup", "install", "uninstall", "update" }).contains(subcmd)) {
        ci.out.println("Invalid subcommand: " + subcmd);
        ci.out.println();
        return;
    }
    PluginManager plugin_manager = ci.getCore().getPluginManager();
    if (subcmd.equals("list") || subcmd.equals("listall")) {
        boolean all_plugins = subcmd.equals("listall");
        ci.out.println("> -----");
        PluginInterface[] plugins = plugin_manager.getPluginInterfaces();
        TreeSet plugin_ids = new TreeSet(String.CASE_INSENSITIVE_ORDER);
        for (int i = 0; i < plugins.length; i++) {
            if (!all_plugins && !plugins[i].getPluginState().isOperational()) {
                continue;
            }
            String plugin_id = plugins[i].getPluginID();
            plugin_ids.add(plugin_id);
        }
        TextWrap.printList(plugin_ids.iterator(), ci.out, "   ");
        ci.out.println("> -----");
        return;
    }
    if (subcmd.equals("location")) {
        // Taken from ConfigSectionPlugins.
        File fUserPluginDir = FileUtil.getUserFile("plugins");
        String sep = File.separator;
        String sUserPluginDir;
        try {
            sUserPluginDir = fUserPluginDir.getCanonicalPath();
        } catch (Throwable e) {
            sUserPluginDir = fUserPluginDir.toString();
        }
        if (!sUserPluginDir.endsWith(sep)) {
            sUserPluginDir += sep;
        }
        File fAppPluginDir = FileUtil.getApplicationFile("plugins");
        String sAppPluginDir;
        try {
            sAppPluginDir = fAppPluginDir.getCanonicalPath();
        } catch (Throwable e) {
            sAppPluginDir = fAppPluginDir.toString();
        }
        if (!sAppPluginDir.endsWith(sep)) {
            sAppPluginDir += sep;
        }
        ci.out.println("Shared plugin location:");
        ci.out.println("  " + sAppPluginDir);
        ci.out.println("User plugin location:");
        ci.out.println("  " + sUserPluginDir);
        ci.out.println();
        return;
    }
    if (subcmd.equals("update")) {
        if (args.size() != 1) {
            ci.out.println("Usage: update");
            return;
        }
        UpdateManager update_manager = plugin_manager.getDefaultPluginInterface().getUpdateManager();
        final UpdateCheckInstance checker = update_manager.createUpdateCheckInstance();
        checker.addListener(new UpdateCheckInstanceListener() {

            @Override
            public void cancelled(UpdateCheckInstance instance) {
            }

            @Override
            public void complete(UpdateCheckInstance instance) {
                Update[] updates = instance.getUpdates();
                try {
                    for (Update update : updates) {
                        ci.out.println("Updating " + update.getName());
                        for (ResourceDownloader rd : update.getDownloaders()) {
                            rd.addListener(new ResourceDownloaderAdapter() {

                                @Override
                                public void reportActivity(ResourceDownloader downloader, String activity) {
                                    ci.out.println("\t" + activity);
                                }

                                @Override
                                public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                    ci.out.println("\t" + percentage + "%");
                                }
                            });
                            rd.download();
                        }
                    }
                    boolean restart_required = false;
                    for (int i = 0; i < updates.length; i++) {
                        if (updates[i].getRestartRequired() == Update.RESTART_REQUIRED_YES) {
                            restart_required = true;
                        }
                    }
                    if (restart_required) {
                        ci.out.println("**** Restart required to complete update ****");
                    }
                } catch (Throwable e) {
                    ci.out.println("Plugin update failed: " + Debug.getNestedExceptionMessage(e));
                }
            }
        });
        checker.start();
        return;
    }
    if (subcmd.equals("install")) {
        if (args.size() == 1) {
            ci.out.println("Contacting plugin repository for list of available plugins...");
            try {
                SFPluginDetails[] plugins = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails();
                for (SFPluginDetails p : plugins) {
                    String category = p.getCategory();
                    if (category != null) {
                        if (category.equalsIgnoreCase("hidden") || (category.equalsIgnoreCase("core"))) {
                            continue;
                        }
                    }
                    String id = p.getId();
                    if (plugin_manager.getPluginInterfaceByID(id, false) == null) {
                        String desc = p.getDescription();
                        int pos = desc.indexOf("<br");
                        if (pos > 0) {
                            desc = desc.substring(0, pos);
                        }
                        ci.out.println("\t" + id + ": \t\t" + desc);
                    }
                }
            } catch (Throwable e) {
                ci.out.println("Failed to list plugins: " + Debug.getNestedExceptionMessage(e));
            }
        } else {
            String target_id = (String) args.get(1);
            if (plugin_manager.getPluginInterfaceByID(target_id, false) != null) {
                ci.out.println("Plugin '" + target_id + "' already installed");
                return;
            }
            final PluginInstaller installer = plugin_manager.getPluginInstaller();
            try {
                final StandardPlugin sp = installer.getStandardPlugin(target_id);
                if (sp == null) {
                    ci.out.println("Plugin '" + target_id + "' is unknown");
                    return;
                }
                new AEThread2("Plugin Installer") {

                    @Override
                    public void run() {
                        try {
                            Map<Integer, Object> properties = new HashMap<>();
                            properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
                            properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
                            final AESemaphore sem = new AESemaphore("plugin-install");
                            final boolean[] restart_required = { false };
                            UpdateCheckInstance instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {

                                @Override
                                public void completed() {
                                    ci.out.println("Installation complete");
                                    sem.release();
                                }

                                @Override
                                public void cancelled() {
                                    ci.out.println("Installation cancelled");
                                    sem.release();
                                }

                                @Override
                                public void failed(PluginException e) {
                                    ci.out.println("Installation failed: " + Debug.getNestedExceptionMessage(e));
                                    sem.release();
                                }
                            });
                            instance.addListener(new UpdateCheckInstanceListener() {

                                @Override
                                public void cancelled(UpdateCheckInstance instance) {
                                    ci.out.println("Installation cancelled");
                                }

                                @Override
                                public void complete(UpdateCheckInstance instance) {
                                    Update[] updates = instance.getUpdates();
                                    for (final Update update : updates) {
                                        ResourceDownloader[] rds = update.getDownloaders();
                                        for (ResourceDownloader rd : rds) {
                                            rd.addListener(new ResourceDownloaderAdapter() {

                                                @Override
                                                public void reportActivity(ResourceDownloader downloader, String activity) {
                                                    ci.out.println("\t" + activity);
                                                }

                                                @Override
                                                public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                                    ci.out.println("\t" + percentage + "%");
                                                }
                                            });
                                            try {
                                                rd.download();
                                            } catch (Throwable e) {
                                            }
                                        }
                                        if (update.getRestartRequired() != Update.RESTART_REQUIRED_NO) {
                                            restart_required[0] = true;
                                        }
                                    }
                                }
                            });
                            sem.reserve();
                            if (restart_required[0]) {
                                ci.out.println("**** Restart required to complete installation ****");
                            }
                        } catch (Throwable e) {
                            ci.out.println("Install failed: " + Debug.getNestedExceptionMessage(e));
                        }
                    }
                }.start();
            } catch (Throwable e) {
                ci.out.println("Install failed: " + Debug.getNestedExceptionMessage(e));
            }
        }
        return;
    }
    // Commands from this point require a plugin ID.
    if (args.size() == 1) {
        ci.out.println("No plugin ID given.");
        ci.out.println();
        return;
    }
    String plugin_id = (String) args.get(1);
    PluginInterface plugin = plugin_manager.getPluginInterfaceByID(plugin_id, false);
    if (plugin == null) {
        ci.out.println("Invalid plugin ID: " + plugin_id);
        ci.out.println();
        return;
    }
    if (subcmd.equals("status")) {
        ci.out.println("ID     : " + plugin.getPluginID());
        ci.out.println("Name   : " + plugin.getPluginName());
        ci.out.println("Version: " + plugin.getPluginVersion());
        ci.out.println("Running: " + plugin.getPluginState().isOperational());
        ci.out.println("Runs at startup: " + plugin.getPluginState().isLoadedAtStartup());
        if (!plugin.getPluginState().isBuiltIn()) {
            ci.out.println("Location: " + plugin.getPluginDirectoryName());
        }
        ci.out.println();
        return;
    }
    if (subcmd.equals("startup")) {
        if (args.size() == 2) {
            ci.out.println("Need to pass either \"on\" or \"off\"");
            ci.out.println();
            return;
        }
        String enabled_mode = (String) args.get(2);
        if (enabled_mode.equals("on")) {
            plugin.getPluginState().setLoadedAtStartup(true);
        } else if (enabled_mode.equals("off")) {
            plugin.getPluginState().setLoadedAtStartup(false);
        } else {
            ci.out.println("Need to pass either \"on\" or \"off\"");
            ci.out.println();
            return;
        }
        ci.out.println("Done.");
        ci.out.println();
        return;
    }
    if (subcmd.equals("uninstall")) {
        PluginInterface pi = plugin_manager.getPluginInterfaceByID(plugin_id, false);
        if (pi == null) {
            ci.out.println("Plugin '" + plugin_id + "' is not installed");
            return;
        }
        final PluginInstaller installer = plugin_manager.getPluginInstaller();
        try {
            final StandardPlugin sp = installer.getStandardPlugin(plugin_id);
            if (sp == null) {
                ci.out.println("Plugin '" + plugin_id + "' is not a standard plugin");
                return;
            }
            final PluginInstaller uninstaller = plugin_manager.getPluginInstaller();
            Map<Integer, Object> properties = new HashMap<>();
            final AESemaphore sem = new AESemaphore("plugin-uninstall");
            UpdateCheckInstance instance = uninstaller.uninstall(new PluginInterface[] { pi }, new PluginInstallationListener() {

                @Override
                public void completed() {
                    ci.out.println("Uninstallation complete");
                    sem.release();
                }

                @Override
                public void cancelled() {
                    ci.out.println("Uninstallation cancelled");
                    sem.release();
                }

                @Override
                public void failed(PluginException e) {
                    ci.out.println("Uninstallation failed: " + Debug.getNestedExceptionMessage(e));
                    sem.release();
                }
            }, properties);
            instance.addListener(new UpdateCheckInstanceListener() {

                @Override
                public void cancelled(UpdateCheckInstance instance) {
                    ci.out.println("InsUninstallationtallation cancelled");
                }

                @Override
                public void complete(UpdateCheckInstance instance) {
                    Update[] updates = instance.getUpdates();
                    for (final Update update : updates) {
                        ResourceDownloader[] rds = update.getDownloaders();
                        for (ResourceDownloader rd : rds) {
                            try {
                                rd.download();
                            } catch (Throwable e) {
                            }
                        }
                    }
                }
            });
            sem.reserve();
            Object obj = properties.get(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED);
            if (obj instanceof Boolean && (Boolean) obj) {
                ci.out.println("**** Restart required to complete uninstallation ****");
            }
        } catch (Throwable e) {
            ci.out.println("Uninstall failed: " + Debug.getNestedExceptionMessage(e));
        }
    }
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) HashMap(java.util.HashMap) Update(com.biglybt.pif.update.Update) AESemaphore(com.biglybt.core.util.AESemaphore) PluginManager(com.biglybt.pif.PluginManager) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener) TreeSet(java.util.TreeSet) SFPluginDetails(com.biglybt.pifimpl.update.sf.SFPluginDetails) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) PluginInterface(com.biglybt.pif.PluginInterface) PluginException(com.biglybt.pif.PluginException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) AEThread2(com.biglybt.core.util.AEThread2) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) PluginInstaller(com.biglybt.pif.installer.PluginInstaller) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) UpdateManager(com.biglybt.pif.update.UpdateManager) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AESemaphore (com.biglybt.core.util.AESemaphore)14 PluginException (com.biglybt.pif.PluginException)4 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)4 HashMap (java.util.HashMap)4 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)3 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)3 Update (com.biglybt.pif.update.Update)3 UpdateCheckInstance (com.biglybt.pif.update.UpdateCheckInstance)3 UpdateCheckInstanceListener (com.biglybt.pif.update.UpdateCheckInstanceListener)3 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)3 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)3 InetSocketAddress (java.net.InetSocketAddress)3 Map (java.util.Map)3 ProtocolEndpoint (com.biglybt.core.networkmanager.ProtocolEndpoint)2 NetworkAdminException (com.biglybt.core.networkmanager.admin.NetworkAdminException)2 AEThread2 (com.biglybt.core.util.AEThread2)2 SocketChannel (java.nio.channels.SocketChannel)2 DiskManagerFileInfoImpl (com.biglybt.core.disk.impl.DiskManagerFileInfoImpl)1 DiskManagerRecheckInstance (com.biglybt.core.disk.impl.DiskManagerRecheckInstance)1 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)1