Search in sources :

Example 1 with UTTimerEventPerformer

use of com.biglybt.pif.utils.UTTimerEventPerformer in project BiglyBT by BiglySoftware.

the class DHTPlugin method initialize.

@Override
public void initialize(PluginInterface _plugin_interface) {
    status = STATUS_INITALISING;
    plugin_interface = _plugin_interface;
    dht_data_port = UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
    log = plugin_interface.getLogger().getTimeStampedChannel(PLUGIN_NAME);
    UIManager ui_manager = plugin_interface.getUIManager();
    final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel(PLUGIN_RESOURCE_ID);
    model.setConfigSectionID(PLUGIN_CONFIGSECTION_ID);
    BasicPluginConfigModel config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, PLUGIN_CONFIGSECTION_ID);
    config.addLabelParameter2("dht.info");
    final BooleanParameter enabled_param = config.addBooleanParameter2("dht.enabled", "dht.enabled", true);
    plugin_interface.getPluginconfig().addListener(new PluginConfigListener() {

        @Override
        public void configSaved() {
            int new_dht_data_port = UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
            if (new_dht_data_port != dht_data_port) {
                changePort(new_dht_data_port);
            }
        }
    });
    LabelParameter reseed_label = config.addLabelParameter2("dht.reseed.label");
    final StringParameter reseed_ip = config.addStringParameter2("dht.reseed.ip", "dht.reseed.ip", "");
    final IntParameter reseed_port = config.addIntParameter2("dht.reseed.port", "dht.reseed.port", 0);
    reseed = config.addActionParameter2("dht.reseed.info", "dht.reseed");
    reseed.setEnabled(false);
    config.createGroup("dht.reseed.group", new Parameter[] { reseed_label, reseed_ip, reseed_port, reseed });
    final BooleanParameter ipfilter_logging_param = config.addBooleanParameter2("dht.ipfilter.log", "dht.ipfilter.log", true);
    ipfilter_logging[0] = ipfilter_logging_param.getValue();
    ipfilter_logging_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter p) {
            ipfilter_logging[0] = ipfilter_logging_param.getValue();
        }
    });
    warn_user = config.addBooleanParameter2("dht.warn.user", "dht.warn.user", true);
    prefer_i2p = config.addBooleanParameter2("dht.prefer.i2p", "dht.prefer.i2p", false);
    BooleanParameter sleeping = config.addBooleanParameter2("dht.is.sleeping", "dht.is.sleeping", false);
    AERunStateHandler.addListener(new AERunStateHandler.RunStateChangeListener() {

        @Override
        public void runStateChanged(long run_state) {
            sleeping.setValue(AERunStateHandler.isDHTSleeping());
        }
    }, true);
    sleeping.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            AERunStateHandler.setDHTSleeping(sleeping.getValue());
        }
    });
    final BooleanParameter advanced = config.addBooleanParameter2("dht.advanced", "dht.advanced", false);
    LabelParameter advanced_label = config.addLabelParameter2("dht.advanced.label");
    final StringParameter override_ip = config.addStringParameter2("dht.override.ip", "dht.override.ip", "");
    config.createGroup("dht.advanced.group", new Parameter[] { advanced_label, override_ip });
    advanced.addEnabledOnSelection(advanced_label);
    advanced.addEnabledOnSelection(override_ip);
    final StringParameter command = config.addStringParameter2("dht.execute.command", "dht.execute.command", "print");
    ActionParameter execute = config.addActionParameter2("dht.execute.info", "dht.execute");
    final BooleanParameter logging = config.addBooleanParameter2("dht.logging", "dht.logging", false);
    config.createGroup("dht.diagnostics.group", new Parameter[] { command, execute, logging });
    logging.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            if (dhts != null) {
                for (int i = 0; i < dhts.length; i++) {
                    dhts[i].setLogging(logging.getValue());
                }
            }
        }
    });
    final DHTPluginOperationListener log_polistener = new DHTPluginOperationListener() {

        @Override
        public boolean diversified() {
            return (true);
        }

        @Override
        public void starts(byte[] key) {
        }

        @Override
        public void valueRead(DHTPluginContact originator, DHTPluginValue value) {
            log.log("valueRead: " + new String(value.getValue()) + " from " + originator.getName() + "/" + originator.getAddress() + ", flags=" + Integer.toHexString(value.getFlags() & 0x00ff));
            if ((value.getFlags() & DHTPlugin.FLAG_STATS) != 0) {
                DHTPluginKeyStats stats = decodeStats(value);
                log.log("    stats: size=" + (stats == null ? "null" : stats.getSize()));
            }
        }

        @Override
        public void valueWritten(DHTPluginContact target, DHTPluginValue value) {
            log.log("valueWritten:" + new String(value.getValue()) + " to " + target.getName() + "/" + target.getAddress());
        }

        @Override
        public void complete(byte[] key, boolean timeout_occurred) {
            log.log("complete: timeout = " + timeout_occurred);
        }
    };
    execute.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            AEThread2 t = new AEThread2("DHT:commandrunner", true) {

                @Override
                public void run() {
                    try {
                        if (dhts == null) {
                            return;
                        }
                        String c = command.getValue().trim();
                        String lc = c.toLowerCase();
                        if (lc.equals("suspend")) {
                            if (!setSuspended(true)) {
                                Debug.out("Suspend failed");
                            }
                            return;
                        } else if (lc.equals("resume")) {
                            if (!setSuspended(false)) {
                                Debug.out("Resume failed");
                            }
                            return;
                        } else if (lc.equals("bridge_put")) {
                            try {
                                List<DistributedDatabase> ddbs = plugin_interface.getUtilities().getDistributedDatabases(new String[] { AENetworkClassifier.AT_I2P });
                                DistributedDatabase ddb = ddbs.get(0);
                                DistributedDatabaseKey key = ddb.createKey("fred");
                                key.setFlags(DistributedDatabaseKey.FL_BRIDGED);
                                ddb.write(new DistributedDatabaseListener() {

                                    @Override
                                    public void event(DistributedDatabaseEvent event) {
                                    // TODO Auto-generated method stub
                                    }
                                }, key, ddb.createValue("bill"));
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
                            return;
                        }
                        for (int i = 0; i < dhts.length; i++) {
                            DHT dht = dhts[i].getDHT();
                            DHTTransportUDP transport = (DHTTransportUDP) dht.getTransport();
                            if (lc.equals("print")) {
                                dht.print(true);
                                dhts[i].logStats();
                            } else if (lc.equals("pingall")) {
                                if (i == 1) {
                                    dht.getControl().pingAll();
                                }
                            } else if (lc.equals("versions")) {
                                List<DHTRouterContact> contacts = dht.getRouter().getAllContacts();
                                Map<Byte, Integer> counts = new TreeMap<>();
                                for (DHTRouterContact r : contacts) {
                                    DHTControlContact contact = (DHTControlContact) r.getAttachment();
                                    byte v = contact.getTransportContact().getProtocolVersion();
                                    Integer count = counts.get(v);
                                    if (count == null) {
                                        counts.put(v, 1);
                                    } else {
                                        counts.put(v, count + 1);
                                    }
                                }
                                log.log("Net " + dht.getTransport().getNetwork());
                                int total = contacts.size();
                                if (total == 0) {
                                    log.log("   no contacts");
                                } else {
                                    String ver = "";
                                    for (Map.Entry<Byte, Integer> entry : counts.entrySet()) {
                                        ver += (ver.length() == 0 ? "" : ", ") + entry.getKey() + "=" + 100 * entry.getValue() / total + "%";
                                    }
                                    log.log("    contacts=" + total + ": " + ver);
                                }
                            } else if (lc.equals("testca")) {
                                ((DHTTransportUDPImpl) transport).testExternalAddressChange();
                            } else if (lc.equals("testnd")) {
                                ((DHTTransportUDPImpl) transport).testNetworkAlive(false);
                            } else if (lc.equals("testna")) {
                                ((DHTTransportUDPImpl) transport).testNetworkAlive(true);
                            } else {
                                int pos = c.indexOf(' ');
                                if (pos != -1) {
                                    String lhs = lc.substring(0, pos);
                                    String rhs = c.substring(pos + 1);
                                    if (lhs.equals("set")) {
                                        pos = rhs.indexOf('=');
                                        if (pos != -1) {
                                            DHTPlugin.this.put(rhs.substring(0, pos).getBytes(), "DHT Plugin: set", rhs.substring(pos + 1).getBytes(), (byte) 0, log_polistener);
                                        }
                                    } else if (lhs.equals("get")) {
                                        DHTPlugin.this.get(rhs.getBytes("UTF-8"), "DHT Plugin: get", (byte) 0, 1, 10000, true, false, log_polistener);
                                    } else if (lhs.equals("query")) {
                                        DHTPlugin.this.get(rhs.getBytes("UTF-8"), "DHT Plugin: get", DHTPlugin.FLAG_STATS, 1, 10000, true, false, log_polistener);
                                    } else if (lhs.equals("punch")) {
                                        Map originator_data = new HashMap();
                                        originator_data.put("hello", "mum");
                                        DHTNATPuncher puncher = dht.getNATPuncher();
                                        if (puncher != null) {
                                            puncher.punch("Test", transport.getLocalContact(), null, originator_data);
                                        }
                                    } else if (lhs.equals("stats")) {
                                        try {
                                            pos = rhs.lastIndexOf(":");
                                            DHTTransportContact contact;
                                            if (pos == -1) {
                                                contact = transport.getLocalContact();
                                            } else {
                                                String host = rhs.substring(0, pos);
                                                int port = Integer.parseInt(rhs.substring(pos + 1));
                                                contact = transport.importContact(new InetSocketAddress(host, port), transport.getProtocolVersion(), false);
                                            }
                                            log.log("Stats request to " + contact.getName());
                                            DHTTransportFullStats stats = contact.getStats();
                                            log.log("Stats:" + (stats == null ? "<null>" : stats.getString()));
                                            DHTControlActivity[] activities = dht.getControl().getActivities();
                                            for (int j = 0; j < activities.length; j++) {
                                                log.log("    act:" + activities[j].getString());
                                            }
                                        } catch (Throwable e) {
                                            Debug.printStackTrace(e);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                }
            };
            t.start();
        }
    });
    reseed.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            reseed.setEnabled(false);
            AEThread2 t = new AEThread2("DHT:reseeder", true) {

                @Override
                public void run() {
                    try {
                        String ip = reseed_ip.getValue().trim();
                        if (dhts == null) {
                            return;
                        }
                        int port = reseed_port.getValue();
                        for (int i = 0; i < dhts.length; i++) {
                            DHTPluginImpl dht = dhts[i];
                            if (ip.length() == 0 || port == 0) {
                                dht.checkForReSeed(true);
                            } else {
                                DHTTransportContact seed = dht.importSeed(ip, port);
                                if (seed != null) {
                                    dht.integrateDHT(false, seed);
                                }
                            }
                        }
                    } finally {
                        reseed.setEnabled(true);
                    }
                }
            };
            t.start();
        }
    });
    model.getActivity().setVisible(false);
    model.getProgress().setVisible(false);
    log.addListener(new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String message) {
            model.getLogArea().appendText(message + "\n");
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            model.getLogArea().appendText(error.toString() + "\n");
        }
    });
    dht_log = new DHTLogger() {

        @Override
        public void log(String str) {
            log.log(str);
        }

        @Override
        public void log(Throwable e) {
            log.log(e);
        }

        @Override
        public void log(int log_type, String str) {
            if (isEnabled(log_type)) {
                log.log(str);
            }
        }

        @Override
        public boolean isEnabled(int log_type) {
            if (log_type == DHTLogger.LT_IP_FILTER) {
                return ipfilter_logging[0];
            }
            return (true);
        }

        @Override
        public PluginInterface getPluginInterface() {
            return (log.getLogger().getPluginInterface());
        }
    };
    if (!enabled_param.getValue()) {
        model.getStatus().setText("Disabled");
        status = STATUS_DISABLED;
        init_sem.releaseForever();
        return;
    }
    setPluginInfo();
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
            PluginInterface pi_upnp = plugin_interface.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
            if (pi_upnp == null) {
                log.log("UPnP plugin not found, can't map port");
            } else {
                upnp_mapping = ((UPnPPlugin) pi_upnp.getPlugin()).addMapping(plugin_interface.getPluginName(), false, dht_data_port, true);
            }
            String ip = null;
            if (advanced.getValue()) {
                ip = override_ip.getValue().trim();
                if (ip.length() == 0) {
                    ip = null;
                }
            }
            initComplete(model.getStatus(), logging.getValue(), ip);
        }

        @Override
        public void closedownInitiated() {
            if (dhts != null) {
                for (int i = 0; i < dhts.length; i++) {
                    dhts[i].closedownInitiated();
                }
            }
            saveClockSkew();
        }

        @Override
        public void closedownComplete() {
        }
    });
    final int sample_frequency = 60 * 1000;
    // every 15 mins
    final int sample_stats_ticks = 15;
    plugin_interface.getUtilities().createTimer("DHTStats", true).addPeriodicEvent(sample_frequency, new UTTimerEventPerformer() {

        @Override
        public void perform(UTTimerEvent event) {
            if (dhts != null) {
                for (int i = 0; i < dhts.length; i++) {
                    dhts[i].updateStats(sample_stats_ticks);
                }
            }
            setPluginInfo();
            saveClockSkew();
        }
    });
}
Also used : DHTTransportUDP(com.biglybt.core.dht.transport.udp.DHTTransportUDP) LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) InetSocketAddress(java.net.InetSocketAddress) UIManager(com.biglybt.pif.ui.UIManager) DHTLogger(com.biglybt.core.dht.DHTLogger) DistributedDatabaseListener(com.biglybt.pif.ddb.DistributedDatabaseListener) DHTRouterContact(com.biglybt.core.dht.router.DHTRouterContact) DHTTransportFullStats(com.biglybt.core.dht.transport.DHTTransportFullStats) DHTTransportUDPImpl(com.biglybt.core.dht.transport.udp.impl.DHTTransportUDPImpl) DHTNATPuncher(com.biglybt.core.dht.nat.DHTNATPuncher) DistributedDatabaseEvent(com.biglybt.pif.ddb.DistributedDatabaseEvent) UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) DHTTransportContact(com.biglybt.core.dht.transport.DHTTransportContact) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) DHTControlContact(com.biglybt.core.dht.control.DHTControlContact) DistributedDatabase(com.biglybt.pif.ddb.DistributedDatabase) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) DHT(com.biglybt.core.dht.DHT) DHTControlActivity(com.biglybt.core.dht.control.DHTControlActivity) DHTPluginImpl(com.biglybt.plugin.dht.impl.DHTPluginImpl) DistributedDatabaseKey(com.biglybt.pif.ddb.DistributedDatabaseKey) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Example 2 with UTTimerEventPerformer

use of com.biglybt.pif.utils.UTTimerEventPerformer in project BiglyBT by BiglySoftware.

the class RunEverythingPlugin method initialize.

@Override
public void initialize(PluginInterface _pi) {
    plugin_interface = _pi;
    logger = plugin_interface.getLogger().getChannel("RunEverythingSeedingRules");
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
        }

        @Override
        public void closedownInitiated() {
            closing = true;
        }

        @Override
        public void closedownComplete() {
        }
    });
    downloads = new HashMap();
    downloads_mon = plugin_interface.getUtilities().getMonitor();
    work_sem = plugin_interface.getUtilities().getSemaphore();
    plugin_interface.getDownloadManager().addListener(this);
    plugin_interface.getUtilities().createTimer("DownloadRules", true).addPeriodicEvent(10000, new UTTimerEventPerformer() {

        @Override
        public void perform(UTTimerEvent event) {
            checkRules();
        }
    });
    plugin_interface.getUtilities().createThread("DownloadRules", new Runnable() {

        @Override
        public void run() {
            processLoop();
        }
    });
}
Also used : UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Example 3 with UTTimerEventPerformer

use of com.biglybt.pif.utils.UTTimerEventPerformer in project BiglyBT by BiglySoftware.

the class LocalTrackerPlugin method track.

protected void track() {
    long now = plugin_interface.getUtilities().getCurrentSystemTime();
    if (now - plugin_start_time < 60 * 1000) {
        try {
            // initial small delay to let things stabilise
            Thread.sleep(15 * 1000);
        } catch (Throwable e) {
        }
    }
    plugin_interface.getUtilities().createTimer("LanPeerFinder:Tracker", true).addPeriodicEvent(30 * 1000, new UTTimerEventPerformer() {

        @Override
        public void perform(UTTimerEvent event) {
            current_time = plugin_interface.getUtilities().getCurrentSystemTime();
            try {
                List<Download> todo = new ArrayList<>();
                try {
                    mon.enter();
                    Iterator<Map.Entry<Download, long[]>> it = downloads.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry<Download, long[]> entry = it.next();
                        Download dl = entry.getKey();
                        long when = entry.getValue()[0];
                        if (when > current_time || current_time - when > ANNOUNCE_PERIOD) {
                            todo.add(dl);
                        }
                    }
                } finally {
                    mon.exit();
                }
                for (int i = 0; i < todo.size(); i++) {
                    track(todo.get(i));
                }
            } catch (Throwable e) {
                log.log(e);
            }
        }
    });
}
Also used : UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) Download(com.biglybt.pif.download.Download) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Example 4 with UTTimerEventPerformer

use of com.biglybt.pif.utils.UTTimerEventPerformer in project BiglyBT by BiglySoftware.

the class UPnPPlugin method initialize.

@Override
public void initialize(PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    log = plugin_interface.getLogger().getTimeStampedChannel("UPnP");
    log.setDiagnostic();
    log.setForce(true);
    UIManager ui_manager = plugin_interface.getUIManager();
    final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel("UPnP");
    model.setConfigSectionID(UPNP_PLUGIN_CONFIGSECTION_ID);
    BasicPluginConfigModel upnp_config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, UPNP_PLUGIN_CONFIGSECTION_ID);
    // NATPMP
    BasicPluginConfigModel natpmp_config = ui_manager.createBasicPluginConfigModel(UPNP_PLUGIN_CONFIGSECTION_ID, NATPMP_PLUGIN_CONFIGSECTION_ID);
    natpmp_config.addLabelParameter2("natpmp.info");
    ActionParameter natpmp_wiki = natpmp_config.addActionParameter2("Utils.link.visit", "MainWindow.about.internet.wiki");
    natpmp_wiki.setStyle(ActionParameter.STYLE_LINK);
    natpmp_wiki.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            try {
                plugin_interface.getUIManager().openURL(new URL(Constants.URL_WIKI + "w/NATPMP"));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    });
    natpmp_enable_param = natpmp_config.addBooleanParameter2("natpmp.enable", "natpmp.enable", false);
    nat_pmp_router = natpmp_config.addStringParameter2("natpmp.routeraddress", "natpmp.routeraddress", "");
    natpmp_enable_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            setNATPMPEnableState();
        }
    });
    natpmp_enable_param.addEnabledOnSelection(nat_pmp_router);
    // UPNP
    upnp_config.addLabelParameter2("upnp.info");
    upnp_config.addHyperlinkParameter2("upnp.wiki_link", Constants.URL_WIKI + "w/UPnP");
    upnp_enable_param = upnp_config.addBooleanParameter2("upnp.enable", "upnp.enable", true);
    grab_ports_param = upnp_config.addBooleanParameter2("upnp.grabports", "upnp.grabports", false);
    release_mappings_param = upnp_config.addBooleanParameter2("upnp.releasemappings", "upnp.releasemappings", true);
    ActionParameter refresh_param = upnp_config.addActionParameter2("upnp.refresh.label", "upnp.refresh.button");
    refresh_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            UPnPPlugin.this.refreshMappings();
        }
    });
    // Auto-refresh mappings every minute when enabled.
    final BooleanParameter auto_refresh_on_bad_nat_param = upnp_config.addBooleanParameter2("upnp.refresh_on_bad_nat", "upnp.refresh_mappings_on_bad_nat", false);
    plugin_interface.getUtilities().createTimer("upnp mapping auto-refresh", true).addPeriodicEvent(1 * 60 * 1000, new UTTimerEventPerformer() {

        private long last_bad_nat = 0;

        @Override
        public void perform(UTTimerEvent event) {
            if (upnp == null) {
                return;
            }
            if (!auto_refresh_on_bad_nat_param.getValue()) {
                return;
            }
            if (!upnp_enable_param.getValue()) {
                return;
            }
            int status = plugin_interface.getConnectionManager().getNATStatus();
            if (status == ConnectionManager.NAT_BAD) {
                // Only try to refresh the mappings if this is the first bad NAT
                // message we've been given in the last 15 minutes - we don't want
                // to endlessly retry performing the mappings
                long now = plugin_interface.getUtilities().getCurrentSystemTime();
                if (last_bad_nat + (15 * 60 * 1000) < now) {
                    last_bad_nat = now;
                    log.log(LoggerChannel.LT_WARNING, "NAT status is firewalled - trying to refresh UPnP mappings");
                    refreshMappings(true);
                }
            }
        }
    });
    upnp_config.addLabelParameter2("blank.resource");
    alert_success_param = upnp_config.addBooleanParameter2("upnp.alertsuccess", "upnp.alertsuccess", false);
    alert_other_port_param = upnp_config.addBooleanParameter2("upnp.alertothermappings", "upnp.alertothermappings", true);
    alert_device_probs_param = upnp_config.addBooleanParameter2("upnp.alertdeviceproblems", "upnp.alertdeviceproblems", true);
    selected_interfaces_param = upnp_config.addStringParameter2("upnp.selectedinterfaces", "upnp.selectedinterfaces", "");
    selected_interfaces_param.setGenerateIntermediateEvents(false);
    selected_addresses_param = upnp_config.addStringParameter2("upnp.selectedaddresses", "upnp.selectedaddresses", "");
    selected_addresses_param.setGenerateIntermediateEvents(false);
    desc_prefix_param = upnp_config.addStringParameter2("upnp.descprefix", "upnp.descprefix", Constants.APP_NAME + " UPnP");
    desc_prefix_param.setGenerateIntermediateEvents(false);
    ignore_bad_devices = upnp_config.addBooleanParameter2("upnp.ignorebaddevices", "upnp.ignorebaddevices", true);
    ignored_devices_list = upnp_config.addLabelParameter2("upnp.ignorebaddevices.info");
    ActionParameter reset_param = upnp_config.addActionParameter2("upnp.ignorebaddevices.reset", "upnp.ignorebaddevices.reset.action");
    reset_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            PluginConfig pc = plugin_interface.getPluginconfig();
            for (int i = 0; i < STATS_KEYS.length; i++) {
                String key = "upnp.device.stats." + STATS_KEYS[i];
                pc.setPluginMapParameter(key, new HashMap());
            }
            pc.setPluginMapParameter("upnp.device.ignorelist", new HashMap());
            updateIgnoreList();
        }
    });
    trace_to_log = upnp_config.addBooleanParameter2("upnp.trace_to_log", "upnp.trace_to_log", false);
    final boolean enabled = upnp_enable_param.getValue();
    upnp_enable_param.addEnabledOnSelection(alert_success_param);
    upnp_enable_param.addEnabledOnSelection(grab_ports_param);
    upnp_enable_param.addEnabledOnSelection(refresh_param);
    auto_refresh_on_bad_nat_param.addEnabledOnSelection(refresh_param);
    upnp_enable_param.addEnabledOnSelection(alert_other_port_param);
    upnp_enable_param.addEnabledOnSelection(alert_device_probs_param);
    upnp_enable_param.addEnabledOnSelection(release_mappings_param);
    upnp_enable_param.addEnabledOnSelection(selected_interfaces_param);
    upnp_enable_param.addEnabledOnSelection(selected_addresses_param);
    upnp_enable_param.addEnabledOnSelection(desc_prefix_param);
    upnp_enable_param.addEnabledOnSelection(ignore_bad_devices);
    upnp_enable_param.addEnabledOnSelection(ignored_devices_list);
    upnp_enable_param.addEnabledOnSelection(reset_param);
    upnp_enable_param.addEnabledOnSelection(trace_to_log);
    natpmp_enable_param.setEnabled(enabled);
    model.getStatus().setText(enabled ? "Running" : "Disabled");
    upnp_enable_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter p) {
            boolean e = upnp_enable_param.getValue();
            natpmp_enable_param.setEnabled(e);
            model.getStatus().setText(e ? "Running" : "Disabled");
            if (e) {
                startUp();
            } else {
                closeDown(true);
            }
            setNATPMPEnableState();
        }
    });
    model.getActivity().setVisible(false);
    model.getProgress().setVisible(false);
    log.addListener(new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String message) {
            model.getLogArea().appendText(message + "\n");
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            model.getLogArea().appendText(error.toString() + "\n");
        }
    });
    // startup() used to be called on initializationComplete()
    // Moved to delayed task because rootDeviceFound can take
    // a lot of CPU cycle.  Let's hope nothing breaks
    DelayedTask dt = plugin_interface.getUtilities().createDelayedTask(new Runnable() {

        @Override
        public void run() {
            if (enabled) {
                updateIgnoreList();
                startUp();
            }
        }
    });
    dt.queue();
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
        }

        @Override
        public void closedownInitiated() {
            if (services.size() == 0) {
                plugin_interface.getPluginconfig().setPluginParameter("plugin.info", "");
            }
        }

        @Override
        public void closedownComplete() {
            closeDown(true);
        }
    });
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) UIManager(com.biglybt.pif.ui.UIManager) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) URL(java.net.URL) PluginListener(com.biglybt.pif.PluginListener) PluginConfig(com.biglybt.pif.PluginConfig) DelayedTask(com.biglybt.pif.utils.DelayedTask) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Example 5 with UTTimerEventPerformer

use of com.biglybt.pif.utils.UTTimerEventPerformer in project BiglyBT by BiglySoftware.

the class UTTimerImpl method addPeriodicEvent.

@Override
public UTTimerEvent addPeriodicEvent(long periodic_millis, final UTTimerEventPerformer ext_performer) {
    if (destroyed) {
        throw (new RuntimeException("Timer has been destroyed"));
    }
    final timerEvent res = new timerEvent();
    TimerEventPerformer performer = new TimerEventPerformer() {

        @Override
        public void perform(TimerEvent ev) {
            UtilitiesImpl.callWithPluginThreadContext(plugin_interface, new Runnable() {

                @Override
                public void run() {
                    try {
                        res.perform(ext_performer);
                    } catch (Throwable e) {
                        Debug.out("Plugin '" + plugin_interface.getPluginName() + " (" + plugin_interface.getPluginID() + " " + plugin_interface.getPluginVersion() + ") caused an error while processing a timer event", e);
                    }
                }
            });
        }
    };
    if (timer == null) {
        res.setEvent(SimpleTimer.addPeriodicEvent("Plugin:" + ext_performer.getClass(), periodic_millis, performer));
    } else {
        res.setEvent(timer.addPeriodicEvent("Plugin:" + ext_performer.getClass(), periodic_millis, performer));
    }
    return (res);
}
Also used : UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer) UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent)

Aggregations

UTTimerEvent (com.biglybt.pif.utils.UTTimerEvent)8 UTTimerEventPerformer (com.biglybt.pif.utils.UTTimerEventPerformer)8 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)2 UIManager (com.biglybt.pif.ui.UIManager)2 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)2 BasicPluginViewModel (com.biglybt.pif.ui.model.BasicPluginViewModel)2 DHT (com.biglybt.core.dht.DHT)1 DHTLogger (com.biglybt.core.dht.DHTLogger)1 DHTControlActivity (com.biglybt.core.dht.control.DHTControlActivity)1 DHTControlContact (com.biglybt.core.dht.control.DHTControlContact)1 DHTNATPuncher (com.biglybt.core.dht.nat.DHTNATPuncher)1 DHTRouterContact (com.biglybt.core.dht.router.DHTRouterContact)1 DHTTransportContact (com.biglybt.core.dht.transport.DHTTransportContact)1 DHTTransportFullStats (com.biglybt.core.dht.transport.DHTTransportFullStats)1 DHTTransportUDP (com.biglybt.core.dht.transport.udp.DHTTransportUDP)1 DHTTransportUDPImpl (com.biglybt.core.dht.transport.udp.impl.DHTTransportUDPImpl)1 PluginConfig (com.biglybt.pif.PluginConfig)1 PluginListener (com.biglybt.pif.PluginListener)1 DistributedDatabase (com.biglybt.pif.ddb.DistributedDatabase)1 DistributedDatabaseEvent (com.biglybt.pif.ddb.DistributedDatabaseEvent)1