Search in sources :

Example 11 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class CategoryUIUtils method createMenuItems.

public static void createMenuItems(final Menu menu, final Category category) {
    if (category.getType() == Category.TYPE_USER) {
        final MenuItem itemDelete = new MenuItem(menu, SWT.PUSH);
        Messages.setLanguageText(itemDelete, "MyTorrentsView.menu.category.delete");
        itemDelete.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
                List<DownloadManager> managers = category.getDownloadManagers(gm.getDownloadManagers());
                // move to array,since setCategory removed it from the category,
                // which would mess up our loop
                DownloadManager[] dms = managers.toArray(new DownloadManager[managers.size()]);
                for (DownloadManager dm : dms) {
                    DownloadManagerState state = dm.getDownloadState();
                    if (state != null) {
                        state.setCategory(null);
                    }
                }
                CategoryManager.removeCategory(category);
            }
        });
    }
    if (category.getType() != Category.TYPE_ALL) {
        long kInB = DisplayFormatters.getKinB();
        long maxDownload = COConfigurationManager.getIntParameter("Max Download Speed KBs", 0) * kInB;
        long maxUpload = COConfigurationManager.getIntParameter("Max Upload Speed KBs", 0) * kInB;
        int down_speed = category.getDownloadSpeed();
        int up_speed = category.getUploadSpeed();
        ViewUtils.addSpeedMenu(menu.getShell(), menu, true, true, true, true, false, down_speed == 0, down_speed, down_speed, maxDownload, false, up_speed == 0, up_speed, up_speed, maxUpload, 1, null, new SpeedAdapter() {

            @Override
            public void setDownSpeed(int val) {
                category.setDownloadSpeed(val);
            }

            @Override
            public void setUpSpeed(int val) {
                category.setUploadSpeed(val);
            }
        });
    }
    GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
    List<DownloadManager> managers = category.getDownloadManagers(gm.getDownloadManagers());
    final DownloadManager[] dms = managers.toArray(new DownloadManager[managers.size()]);
    boolean start = false;
    boolean stop = false;
    for (DownloadManager dm : dms) {
        stop = stop || ManagerUtils.isStopable(dm);
        start = start || ManagerUtils.isStartable(dm);
    }
    // Queue
    final MenuItem itemQueue = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemQueue, "MyTorrentsView.menu.queue");
    Utils.setMenuItemImage(itemQueue, "start");
    itemQueue.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
            List<?> managers = category.getDownloadManagers(gm.getDownloadManagers());
            Object[] dms = managers.toArray();
            TorrentUtil.queueDataSources(dms, true);
        }
    });
    itemQueue.setEnabled(start);
    // Stop
    final MenuItem itemStop = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemStop, "MyTorrentsView.menu.stop");
    Utils.setMenuItemImage(itemStop, "stop");
    itemStop.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
            List<?> managers = category.getDownloadManagers(gm.getDownloadManagers());
            Object[] dms = managers.toArray();
            TorrentUtil.stopDataSources(dms);
        }
    });
    itemStop.setEnabled(stop);
    if (category.canBePublic()) {
        new MenuItem(menu, SWT.SEPARATOR);
        final MenuItem itemPublic = new MenuItem(menu, SWT.CHECK);
        itemPublic.setSelection(category.isPublic());
        Messages.setLanguageText(itemPublic, "cat.share");
        itemPublic.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                category.setPublic(itemPublic.getSelection());
            }
        });
    }
    // share with friends
    PluginInterface bpi = PluginInitializer.getDefaultInterface().getPluginManager().getPluginInterfaceByClass(BuddyPlugin.class);
    int cat_type = category.getType();
    if (bpi != null && cat_type != Category.TYPE_UNCATEGORIZED) {
        final BuddyPlugin buddy_plugin = (BuddyPlugin) bpi.getPlugin();
        if (buddy_plugin.isClassicEnabled()) {
            final Menu share_menu = new Menu(menu.getShell(), SWT.DROP_DOWN);
            final MenuItem share_item = new MenuItem(menu, SWT.CASCADE);
            Messages.setLanguageText(share_item, "azbuddy.ui.menu.cat.share");
            share_item.setMenu(share_menu);
            List<BuddyPluginBuddy> buddies = buddy_plugin.getBuddies();
            if (buddies.size() == 0) {
                final MenuItem item = new MenuItem(share_menu, SWT.CHECK);
                item.setText(MessageText.getString("general.add.friends"));
                item.setEnabled(false);
            } else {
                final String cname;
                if (cat_type == Category.TYPE_ALL) {
                    cname = "All";
                } else {
                    cname = category.getName();
                }
                final boolean is_public = buddy_plugin.isPublicTagOrCategory(cname);
                final MenuItem itemPubCat = new MenuItem(share_menu, SWT.CHECK);
                Messages.setLanguageText(itemPubCat, "general.all.friends");
                itemPubCat.setSelection(is_public);
                itemPubCat.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        if (is_public) {
                            buddy_plugin.removePublicTagOrCategory(cname);
                        } else {
                            buddy_plugin.addPublicTagOrCategory(cname);
                        }
                    }
                });
                new MenuItem(share_menu, SWT.SEPARATOR);
                for (final BuddyPluginBuddy buddy : buddies) {
                    if (buddy.getNickName() == null) {
                        continue;
                    }
                    final boolean auth = buddy.isLocalRSSTagOrCategoryAuthorised(cname);
                    final MenuItem itemShare = new MenuItem(share_menu, SWT.CHECK);
                    itemShare.setText(buddy.getName());
                    itemShare.setSelection(auth || is_public);
                    if (is_public) {
                        itemShare.setEnabled(false);
                    }
                    itemShare.addListener(SWT.Selection, new Listener() {

                        @Override
                        public void handleEvent(Event event) {
                            if (auth) {
                                buddy.removeLocalAuthorisedRSSTagOrCategory(cname);
                            } else {
                                buddy.addLocalAuthorisedRSSTagOrCategory(cname);
                            }
                        }
                    });
                }
            }
        }
    }
    if (category.getType() != Category.TYPE_ALL) {
        TrancodeUIUtils.TranscodeTarget[] tts = TrancodeUIUtils.getTranscodeTargets();
        if (tts.length > 0) {
            final Menu t_menu = new Menu(menu.getShell(), SWT.DROP_DOWN);
            final MenuItem t_item = new MenuItem(menu, SWT.CASCADE);
            Messages.setLanguageText(t_item, "cat.autoxcode");
            t_item.setMenu(t_menu);
            String existing = category.getStringAttribute(Category.AT_AUTO_TRANSCODE_TARGET);
            for (TrancodeUIUtils.TranscodeTarget tt : tts) {
                TrancodeUIUtils.TranscodeProfile[] profiles = tt.getProfiles();
                if (profiles.length > 0) {
                    final Menu tt_menu = new Menu(t_menu.getShell(), SWT.DROP_DOWN);
                    final MenuItem tt_item = new MenuItem(t_menu, SWT.CASCADE);
                    tt_item.setText(tt.getName());
                    tt_item.setMenu(tt_menu);
                    for (final TrancodeUIUtils.TranscodeProfile tp : profiles) {
                        final MenuItem p_item = new MenuItem(tt_menu, SWT.CHECK);
                        p_item.setText(tp.getName());
                        boolean selected = existing != null && existing.equals(tp.getUID());
                        if (selected) {
                            Utils.setMenuItemImage(tt_item, "blacktick");
                        }
                        p_item.setSelection(selected);
                        p_item.addListener(SWT.Selection, new Listener() {

                            @Override
                            public void handleEvent(Event event) {
                                category.setStringAttribute(Category.AT_AUTO_TRANSCODE_TARGET, p_item.getSelection() ? tp.getUID() : null);
                            }
                        });
                    }
                }
            }
        }
    }
    // rss feed
    final MenuItem rssOption = new MenuItem(menu, SWT.CHECK);
    rssOption.setSelection(category.getBooleanAttribute(Category.AT_RSS_GEN));
    Messages.setLanguageText(rssOption, "cat.rss.gen");
    rssOption.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            boolean set = rssOption.getSelection();
            category.setBooleanAttribute(Category.AT_RSS_GEN, set);
        }
    });
    if (cat_type != Category.TYPE_UNCATEGORIZED && cat_type != Category.TYPE_ALL) {
        final MenuItem upPriority = new MenuItem(menu, SWT.CHECK);
        upPriority.setSelection(category.getIntAttribute(Category.AT_UPLOAD_PRIORITY) > 0);
        Messages.setLanguageText(upPriority, "cat.upload.priority");
        upPriority.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                boolean set = upPriority.getSelection();
                category.setIntAttribute(Category.AT_UPLOAD_PRIORITY, set ? 1 : 0);
            }
        });
    }
    // options
    MenuItem itemOptions = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemOptions, "cat.options");
    itemOptions.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
            if (uiFunctions != null) {
                MultipleDocumentInterface mdi = uiFunctions.getMDI();
                if (mdi != null) {
                    mdi.showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_OPTIONS, dms);
                }
            }
        }
    });
    if (dms.length == 0) {
        itemOptions.setEnabled(false);
    }
}
Also used : Listener(org.eclipse.swt.widgets.Listener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) MenuListener(org.eclipse.swt.events.MenuListener) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) BuddyPlugin(com.biglybt.plugin.net.buddy.BuddyPlugin) SpeedAdapter(com.biglybt.ui.swt.views.ViewUtils.SpeedAdapter) GlobalManager(com.biglybt.core.global.GlobalManager) UIFunctions(com.biglybt.ui.UIFunctions) List(java.util.List) Menu(org.eclipse.swt.widgets.Menu) PluginInterface(com.biglybt.pif.PluginInterface) MenuItem(org.eclipse.swt.widgets.MenuItem) MultipleDocumentInterface(com.biglybt.ui.mdi.MultipleDocumentInterface) BuddyPluginBuddy(com.biglybt.plugin.net.buddy.BuddyPluginBuddy) Event(org.eclipse.swt.widgets.Event) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 12 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class DeviceManagerUPnPImpl method startUp.

protected void startUp() {
    UPnPAdapter adapter = new UPnPAdapter() {

        @Override
        public SimpleXMLParserDocument parseXML(String data) throws SimpleXMLParserDocumentException {
            return (plugin_interface.getUtilities().getSimpleXMLParserDocumentFactory().create(data));
        }

        @Override
        public ResourceDownloaderFactory getResourceDownloaderFactory() {
            return (plugin_interface.getUtilities().getResourceDownloaderFactory());
        }

        @Override
        public UTTimer createTimer(String name) {
            return (plugin_interface.getUtilities().createTimer(name));
        }

        @Override
        public void createThread(String name, final Runnable runnable) {
            plugin_interface.getUtilities().createThread(name, runnable);
        }

        @Override
        public Comparator getAlphanumericComparator() {
            return (plugin_interface.getUtilities().getFormatters().getAlphanumericComparator(true));
        }

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

        @Override
        public void trace(String str) {
        // System.out.println( str );
        }

        @Override
        public void log(String str) {
        // System.out.println( str );
        }

        @Override
        public String getTraceDir() {
            return (plugin_interface.getPluginDirectoryName());
        }
    };
    try {
        upnp = UPnPFactory.getSingleton(adapter, null);
        upnp.addRootDeviceListener(new UPnPListener() {

            @Override
            public boolean deviceDiscovered(String USN, URL location) {
                return (true);
            }

            @Override
            public void rootDeviceFound(UPnPRootDevice device) {
                handleDevice(device, true);
            }
        });
        upnp.getSSDP().addListener(new UPnPSSDPListener() {

            private Map<InetAddress, Boolean> liveness_map = new HashMap<>();

            @Override
            public void receivedResult(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String USN, URL location, String ST, String AL) {
            }

            @Override
            public void receivedNotify(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String USN, URL location, String NT, String NTS) {
                alive(originator, !NTS.contains("byebye"));
            }

            @Override
            public String[] receivedSearch(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String ST) {
                alive(originator, true);
                return (null);
            }

            @Override
            public void interfaceChanged(NetworkInterface network_interface) {
            }

            private void alive(InetAddress address, boolean alive) {
                synchronized (liveness_map) {
                    Boolean b = liveness_map.get(address);
                    if (b != null && b == alive) {
                        return;
                    }
                    liveness_map.put(address, alive);
                }
                DeviceImpl[] devices = manager.getDevices();
                for (DeviceImpl d : devices) {
                    if (d instanceof DeviceMediaRendererImpl) {
                        DeviceMediaRendererImpl r = (DeviceMediaRendererImpl) d;
                        InetAddress device_address = r.getAddress();
                        if (device_address != null && device_address.equals(address)) {
                            if (r.isAlive() != alive) {
                                if (alive) {
                                    r.alive();
                                } else {
                                    r.dead();
                                }
                            }
                        }
                    }
                }
            }
        });
    } catch (Throwable e) {
        manager.log("UPnP device manager failed", e);
    }
    try {
        plugin_interface.addEventListener(new PluginEventListener() {

            @Override
            public void handleEvent(PluginEvent ev) {
                int type = ev.getType();
                if (type == PluginEvent.PEV_PLUGIN_OPERATIONAL || type == PluginEvent.PEV_PLUGIN_NOT_OPERATIONAL) {
                    PluginInterface pi = (PluginInterface) ev.getValue();
                    if (pi.getPluginID().equals("azupnpav")) {
                        if (type == PluginEvent.PEV_PLUGIN_OPERATIONAL) {
                            upnpav_ipc = pi.getIPC();
                            addListener(pi);
                        } else {
                            upnpav_ipc = null;
                        }
                    }
                }
            }
        });
        PluginInterface pi = plugin_interface.getPluginManager().getPluginInterfaceByID("azupnpav");
        if (pi == null) {
            manager.log("No UPnPAV plugin found");
        } else {
            upnpav_ipc = pi.getIPC();
            addListener(pi);
        }
    } catch (Throwable e) {
        manager.log("Failed to hook into UPnPAV", e);
    }
    manager.UPnPManagerStarted();
}
Also used : PluginEventListener(com.biglybt.pif.PluginEventListener) PluginInterface(com.biglybt.pif.PluginInterface) NetworkInterface(java.net.NetworkInterface) URL(java.net.URL) InetAddress(java.net.InetAddress) PluginEvent(com.biglybt.pif.PluginEvent)

Example 13 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class DownloadManagerImpl method getTrackerPeerSources.

@Override
public List<TrackerPeerSource> getTrackerPeerSources() {
    try {
        this_mon.enter();
        Object[] tps_data = (Object[]) getUserData(TPS_Key);
        List<TrackerPeerSource> tps;
        if (tps_data == null) {
            tps = new ArrayList<>();
            TOTorrentListener tol = new TOTorrentListener() {

                @Override
                public void torrentChanged(TOTorrent torrent, int type) {
                    if (type == TOTorrentListener.CT_ANNOUNCE_URLS) {
                        List<DownloadManagerTPSListener> to_inform = null;
                        try {
                            this_mon.enter();
                            torrent.removeListener(this);
                            setUserData(TPS_Key, null);
                            if (tps_listeners != null) {
                                to_inform = new ArrayList<>(tps_listeners);
                            }
                        } finally {
                            this_mon.exit();
                        }
                        if (to_inform != null) {
                            for (DownloadManagerTPSListener l : to_inform) {
                                try {
                                    l.trackerPeerSourcesChanged();
                                } catch (Throwable e) {
                                    Debug.out(e);
                                }
                            }
                        }
                    }
                }
            };
            setUserData(TPS_Key, new Object[] { tps, tol });
            Download plugin_download = PluginCoreUtils.wrap(this);
            if (isDestroyed() || plugin_download == null) {
                return (tps);
            }
            // tracker peer sources
            final TOTorrent t = getTorrent();
            if (t != null) {
                t.addListener(tol);
                TOTorrentAnnounceURLSet[] sets = t.getAnnounceURLGroup().getAnnounceURLSets();
                if (sets.length == 0) {
                    sets = new TOTorrentAnnounceURLSet[] { t.getAnnounceURLGroup().createAnnounceURLSet(new URL[] { torrent.getAnnounceURL() }) };
                }
                for (final TOTorrentAnnounceURLSet set : sets) {
                    final URL[] urls = set.getAnnounceURLs();
                    if (urls.length == 0 || TorrentUtils.isDecentralised(urls[0])) {
                        continue;
                    }
                    tps.add(new TrackerPeerSource() {

                        private TrackerPeerSource _delegate;

                        private TRTrackerAnnouncer ta;

                        private long ta_fixup;

                        private long last_scrape_fixup_time;

                        private Object[] last_scrape;

                        private TrackerPeerSource fixup() {
                            long now = SystemTime.getMonotonousTime();
                            if (now - ta_fixup > 1000) {
                                TRTrackerAnnouncer current_ta = getTrackerClient();
                                if (current_ta == ta) {
                                    if (current_ta != null && _delegate == null) {
                                        _delegate = current_ta.getTrackerPeerSource(set);
                                    }
                                } else {
                                    if (current_ta == null) {
                                        _delegate = null;
                                    } else {
                                        _delegate = current_ta.getTrackerPeerSource(set);
                                    }
                                    ta = current_ta;
                                }
                                ta_fixup = now;
                            }
                            return (_delegate);
                        }

                        protected Object[] getScrape() {
                            long now = SystemTime.getMonotonousTime();
                            if (now - last_scrape_fixup_time > 30 * 1000 || last_scrape == null) {
                                TRTrackerScraper scraper = globalManager.getTrackerScraper();
                                int max_peers = -1;
                                int max_seeds = -1;
                                int max_comp = -1;
                                int max_time = 0;
                                int min_scrape = Integer.MAX_VALUE;
                                String status_str = null;
                                boolean found_usable = false;
                                for (URL u : urls) {
                                    TRTrackerScraperResponse resp = scraper.peekScrape(torrent, u);
                                    if (resp != null) {
                                        if (!resp.isDHTBackup()) {
                                            found_usable = true;
                                            int peers = resp.getPeers();
                                            int seeds = resp.getSeeds();
                                            int comp = resp.getCompleted();
                                            if (peers > max_peers) {
                                                max_peers = peers;
                                            }
                                            if (seeds > max_seeds) {
                                                max_seeds = seeds;
                                            }
                                            if (comp > max_comp) {
                                                max_comp = comp;
                                            }
                                            if (resp.getStatus() != TRTrackerScraperResponse.ST_INITIALIZING) {
                                                status_str = resp.getStatusString();
                                                int time = resp.getScrapeTime();
                                                if (time > max_time) {
                                                    max_time = time;
                                                }
                                                long next_scrape = resp.getNextScrapeStartTime();
                                                if (next_scrape > 0) {
                                                    int ns = (int) (next_scrape / 1000);
                                                    if (ns < min_scrape) {
                                                        min_scrape = ns;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (found_usable || last_scrape == null) {
                                    last_scrape = new Object[] { max_seeds, max_peers, max_time, min_scrape, max_comp, status_str };
                                }
                                last_scrape_fixup_time = now;
                            }
                            return (last_scrape);
                        }

                        @Override
                        public int getType() {
                            return (TrackerPeerSource.TP_TRACKER);
                        }

                        @Override
                        public String getName() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (urls[0].toExternalForm());
                            }
                            return (delegate.getName());
                        }

                        @Override
                        public int getStatus() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (ST_STOPPED);
                            }
                            return (delegate.getStatus());
                        }

                        @Override
                        public String getStatusString() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return ((String) getScrape()[5]);
                            }
                            return (delegate.getStatusString());
                        }

                        @Override
                        public int getSeedCount() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return ((Integer) getScrape()[0]);
                            }
                            int seeds = delegate.getSeedCount();
                            if (seeds < 0) {
                                seeds = (Integer) getScrape()[0];
                            }
                            return (seeds);
                        }

                        @Override
                        public int getLeecherCount() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return ((Integer) getScrape()[1]);
                            }
                            int leechers = delegate.getLeecherCount();
                            if (leechers < 0) {
                                leechers = (Integer) getScrape()[1];
                            }
                            return (leechers);
                        }

                        @Override
                        public int getCompletedCount() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return ((Integer) getScrape()[4]);
                            }
                            int comp = delegate.getCompletedCount();
                            if (comp < 0) {
                                comp = (Integer) getScrape()[4];
                            }
                            return (comp);
                        }

                        @Override
                        public int getPeers() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (-1);
                            }
                            return (delegate.getPeers());
                        }

                        @Override
                        public int getInterval() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                Object[] si = getScrape();
                                int last = (Integer) si[2];
                                int next = (Integer) si[3];
                                if (last > 0 && next < Integer.MAX_VALUE && last < next) {
                                    return (next - last);
                                }
                                return (-1);
                            }
                            return (delegate.getInterval());
                        }

                        @Override
                        public int getMinInterval() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (-1);
                            }
                            return (delegate.getMinInterval());
                        }

                        @Override
                        public boolean isUpdating() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (false);
                            }
                            return (delegate.isUpdating());
                        }

                        @Override
                        public int getLastUpdate() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return ((Integer) getScrape()[2]);
                            }
                            return (delegate.getLastUpdate());
                        }

                        @Override
                        public int getSecondsToUpdate() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (-1);
                            }
                            return (delegate.getSecondsToUpdate());
                        }

                        @Override
                        public boolean canManuallyUpdate() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate == null) {
                                return (false);
                            }
                            return (delegate.canManuallyUpdate());
                        }

                        @Override
                        public void manualUpdate() {
                            TrackerPeerSource delegate = fixup();
                            if (delegate != null) {
                                delegate.manualUpdate();
                            }
                        }

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

                        @Override
                        public void delete() {
                            List<List<String>> lists = TorrentUtils.announceGroupsToList(t);
                            List<String> rem = new ArrayList<>();
                            for (URL u : urls) {
                                rem.add(u.toExternalForm());
                            }
                            lists = TorrentUtils.removeAnnounceURLs2(lists, rem, false);
                            TorrentUtils.listToAnnounceGroups(lists, t);
                        }
                    });
                }
                // cache peer source
                tps.add(new TrackerPeerSourceAdapter() {

                    private TrackerPeerSource _delegate;

                    private TRTrackerAnnouncer ta;

                    private boolean enabled;

                    private long ta_fixup;

                    private TrackerPeerSource fixup() {
                        long now = SystemTime.getMonotonousTime();
                        if (now - ta_fixup > 1000) {
                            TRTrackerAnnouncer current_ta = getTrackerClient();
                            if (current_ta == ta) {
                                if (current_ta != null && _delegate == null) {
                                    _delegate = current_ta.getCacheTrackerPeerSource();
                                }
                            } else {
                                if (current_ta == null) {
                                    _delegate = null;
                                } else {
                                    _delegate = current_ta.getCacheTrackerPeerSource();
                                }
                                ta = current_ta;
                            }
                            enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_BT_TRACKER);
                            ta_fixup = now;
                        }
                        return (_delegate);
                    }

                    @Override
                    public int getType() {
                        return (TrackerPeerSource.TP_TRACKER);
                    }

                    @Override
                    public String getName() {
                        TrackerPeerSource delegate = fixup();
                        if (delegate == null) {
                            return (MessageText.getString("tps.tracker.cache"));
                        }
                        return (delegate.getName());
                    }

                    @Override
                    public int getStatus() {
                        TrackerPeerSource delegate = fixup();
                        if (!enabled) {
                            return (ST_DISABLED);
                        }
                        if (delegate == null) {
                            return (ST_STOPPED);
                        }
                        return (ST_ONLINE);
                    }

                    @Override
                    public int getPeers() {
                        TrackerPeerSource delegate = fixup();
                        if (delegate == null || !enabled) {
                            return (-1);
                        }
                        return (delegate.getPeers());
                    }
                });
            }
            try {
                ExternalSeedPlugin esp = DownloadManagerController.getExternalSeedPlugin();
                if (esp != null) {
                    tps.add(esp.getTrackerPeerSource(plugin_download));
                }
            } catch (Throwable e) {
            }
            try {
                PluginInterface dht_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(DHTTrackerPlugin.class);
                if (dht_pi != null) {
                    tps.add(((DHTTrackerPlugin) dht_pi.getPlugin()).getTrackerPeerSource(plugin_download));
                }
            } catch (Throwable e) {
            }
            try {
                PluginInterface lt_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(LocalTrackerPlugin.class);
                if (lt_pi != null) {
                    tps.add(((LocalTrackerPlugin) lt_pi.getPlugin()).getTrackerPeerSource(plugin_download));
                }
            } catch (Throwable e) {
            }
            try {
                tps.add(((DownloadImpl) plugin_download).getTrackerPeerSource());
            } catch (Throwable e) {
            }
            // PEX...
            tps.add(new TrackerPeerSourceAdapter() {

                private PEPeerManager _pm;

                private TrackerPeerSource _delegate;

                private TrackerPeerSource fixup() {
                    PEPeerManager pm = getPeerManager();
                    if (pm == null) {
                        _delegate = null;
                        _pm = null;
                    } else if (pm != _pm) {
                        _pm = pm;
                        _delegate = pm.getTrackerPeerSource();
                    }
                    return (_delegate);
                }

                @Override
                public int getType() {
                    return (TP_PEX);
                }

                @Override
                public int getStatus() {
                    TrackerPeerSource delegate = fixup();
                    if (delegate == null) {
                        return (ST_STOPPED);
                    } else {
                        return (delegate.getStatus());
                    }
                }

                @Override
                public String getName() {
                    TrackerPeerSource delegate = fixup();
                    if (delegate == null) {
                        return ("");
                    } else {
                        return (delegate.getName());
                    }
                }

                @Override
                public int getPeers() {
                    TrackerPeerSource delegate = fixup();
                    if (delegate == null) {
                        return (-1);
                    } else {
                        return (delegate.getPeers());
                    }
                }
            });
            // incoming
            tps.add(new TrackerPeerSourceAdapter() {

                private long fixup_time;

                private PEPeerManager _pm;

                private int tcp;

                private int udp;

                private int utp;

                private int total;

                private boolean enabled;

                private PEPeerManager fixup() {
                    long now = SystemTime.getMonotonousTime();
                    if (now - fixup_time > 1000) {
                        PEPeerManager pm = _pm = getPeerManager();
                        if (pm != null) {
                            tcp = pm.getNbRemoteTCPConnections();
                            udp = pm.getNbRemoteUDPConnections();
                            utp = pm.getNbRemoteUTPConnections();
                            total = pm.getStats().getTotalIncomingConnections();
                        }
                        enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_INCOMING);
                        fixup_time = now;
                    }
                    return (_pm);
                }

                @Override
                public int getType() {
                    return (TP_INCOMING);
                }

                @Override
                public int getStatus() {
                    PEPeerManager delegate = fixup();
                    if (delegate == null) {
                        return (ST_STOPPED);
                    } else if (!enabled) {
                        return (ST_DISABLED);
                    } else {
                        return (ST_ONLINE);
                    }
                }

                @Override
                public String getName() {
                    PEPeerManager delegate = fixup();
                    if (delegate == null || !enabled) {
                        return ("");
                    } else {
                        return (MessageText.getString("tps.incoming.details", new String[] { String.valueOf(tcp), String.valueOf(udp + utp), String.valueOf(total) }));
                    }
                }

                @Override
                public int getPeers() {
                    PEPeerManager delegate = fixup();
                    if (delegate == null || !enabled) {
                        return (-1);
                    } else {
                        return (tcp + udp);
                    }
                }
            });
        } else {
            tps = (List<TrackerPeerSource>) tps_data[0];
        }
        return (tps);
    } finally {
        this_mon.exit();
    }
}
Also used : TrackerPeerSource(com.biglybt.core.tracker.TrackerPeerSource) URL(java.net.URL) Download(com.biglybt.pif.download.Download) TrackerPeerSourceAdapter(com.biglybt.core.tracker.TrackerPeerSourceAdapter) PluginInterface(com.biglybt.pif.PluginInterface) ExternalSeedPlugin(com.biglybt.plugin.extseed.ExternalSeedPlugin) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 14 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class NetworkAdminImpl method getNATDevices.

@Override
public NetworkAdminNATDevice[] getNATDevices(Core core) {
    List<NetworkAdminNATDeviceImpl> devices = new ArrayList<>();
    try {
        PluginInterface upnp_pi = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
        if (upnp_pi != null) {
            UPnPPlugin upnp = (UPnPPlugin) upnp_pi.getPlugin();
            UPnPPluginService[] services = upnp.getServices();
            for (UPnPPluginService service : services) {
                NetworkAdminNATDeviceImpl dev = new NetworkAdminNATDeviceImpl(service);
                boolean same = false;
                for (NetworkAdminNATDeviceImpl d : devices) {
                    if (d.sameAs(dev)) {
                        same = true;
                        break;
                    }
                }
                if (!same) {
                    devices.add(dev);
                }
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
    }
    return ((NetworkAdminNATDevice[]) devices.toArray(new NetworkAdminNATDevice[devices.size()]));
}
Also used : UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) PluginInterface(com.biglybt.pif.PluginInterface)

Example 15 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class NetworkAdminProtocolImpl method test.

@Override
public InetAddress test(NetworkAdminNetworkInterfaceAddress address, boolean upnp_map, NetworkAdminProgressListener listener) throws NetworkAdminException {
    InetAddress bind_ip = address == null ? null : address.getAddress();
    NetworkAdminProtocolTester tester;
    if (type == PT_HTTP) {
        tester = new NetworkAdminHTTPTester(core, listener);
    } else if (type == PT_TCP) {
        tester = new NetworkAdminTCPTester(core, listener);
    } else {
        tester = new NetworkAdminUDPTester(core, listener);
    }
    InetAddress res;
    if (port <= 0) {
        res = tester.testOutbound(bind_ip, 0);
    } else {
        UPnPMapping new_mapping = null;
        if (upnp_map) {
            PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
            if (pi_upnp != null) {
                UPnPPlugin upnp = (UPnPPlugin) pi_upnp.getPlugin();
                UPnPMapping mapping = upnp.getMapping(type != PT_UDP, port);
                if (mapping == null) {
                    new_mapping = mapping = upnp.addMapping("NAT Tester", type != PT_UDP, port, true);
                    try {
                        Thread.sleep(500);
                    } catch (Throwable e) {
                    }
                }
            }
        }
        try {
            res = tester.testInbound(bind_ip, port);
        } finally {
            if (new_mapping != null) {
                new_mapping.destroy();
            }
        }
    }
    return (res);
}
Also used : UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) PluginInterface(com.biglybt.pif.PluginInterface) UPnPMapping(com.biglybt.plugin.upnp.UPnPMapping) InetAddress(java.net.InetAddress)

Aggregations

PluginInterface (com.biglybt.pif.PluginInterface)120 URL (java.net.URL)15 IPCInterface (com.biglybt.pif.ipc.IPCInterface)14 UIManager (com.biglybt.pif.ui.UIManager)13 PluginManager (com.biglybt.pif.PluginManager)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Download (com.biglybt.pif.download.Download)10 MenuItemListener (com.biglybt.pif.ui.menus.MenuItemListener)10 File (java.io.File)10 Map (java.util.Map)10 DownloadManager (com.biglybt.core.download.DownloadManager)9 UIFunctions (com.biglybt.ui.UIFunctions)9 ArrayList (java.util.ArrayList)9 PluginException (com.biglybt.pif.PluginException)8 MenuItem (com.biglybt.pif.ui.menus.MenuItem)8 MenuManager (com.biglybt.pif.ui.menus.MenuManager)8 DHTPlugin (com.biglybt.plugin.dht.DHTPlugin)8 DHT (com.biglybt.core.dht.DHT)7 SelectionEvent (org.eclipse.swt.events.SelectionEvent)7