Search in sources :

Example 16 with PluginInterface

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

the class NetworkAdminSpeedTesterBTImpl method startUp.

protected static void startUp() {
    PluginInterface plugin = PluginInitializer.getDefaultInterface();
    com.biglybt.pif.download.DownloadManager dm = plugin.getDownloadManager();
    Download[] downloads = dm.getDownloads();
    if (downloads != null) {
        int num = downloads.length;
        for (int i = 0; i < num; i++) {
            Download download = downloads[i];
            if (download.getBooleanAttribute(speedTestAttrib)) {
                try {
                    if (download.getState() != Download.ST_STOPPED) {
                        try {
                            download.stop();
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                    download.remove(true, true);
                } catch (Throwable e) {
                    Debug.out("Had " + e.getMessage() + " while trying to remove " + downloads[i].getName());
                }
            }
        }
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) Download(com.biglybt.pif.download.Download)

Example 17 with PluginInterface

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

the class SubscriptionManagerImpl method getSubscriptionDownloadCount.

protected int getSubscriptionDownloadCount() {
    PluginInterface pi = PluginInitializer.getDefaultInterface();
    Download[] downloads = pi.getDownloadManager().getDownloads();
    int res = 0;
    for (int i = 0; i < downloads.length; i++) {
        Download download = downloads[i];
        if (download.getBooleanAttribute(ta_subs_download)) {
            res++;
        }
    }
    return (res);
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface)

Example 18 with PluginInterface

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

the class SubscriptionManagerImpl method recordAssociationsSupport.

protected boolean recordAssociationsSupport(byte[] association_hash, SubscriptionImpl[] subscriptions, boolean full_lookup) {
    PluginInterface pi = PluginInitializer.getDefaultInterface();
    boolean download_found = false;
    boolean changed = false;
    boolean assoc_added = false;
    try {
        Download download = pi.getDownloadManager().getDownload(association_hash);
        if (download != null) {
            download_found = true;
            Map<String, Object> map = (Map<String, Object>) download.getMapAttribute(ta_subscription_info);
            if (map == null) {
                map = new LightHashMap<>();
            } else {
                map = new LightHashMap<>(map);
            }
            List<byte[]> s = (List<byte[]>) map.get("s");
            for (int i = 0; i < subscriptions.length; i++) {
                SubscriptionImpl subscription = subscriptions[i];
                byte[] sid = subscription.getShortID();
                if (s == null) {
                    s = new ArrayList<>();
                    s.add(sid);
                    changed = true;
                    map.put("s", s);
                } else {
                    boolean found = false;
                    for (int j = 0; j < s.size(); j++) {
                        byte[] existing = s.get(j);
                        if (Arrays.equals(sid, existing)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        s.add(sid);
                        if (subscription.isSubscribed() && subscription.isPublic() && !subscription.isSearchTemplate()) {
                            if (subscription.addAssociationSupport(association_hash, true)) {
                                assoc_added = true;
                            }
                        }
                        changed = true;
                    }
                }
            }
            if (full_lookup) {
                map.put("lc", new Long(SystemTime.getCurrentTime()));
                changed = true;
            }
            if (changed) {
                download.setMapAttribute(ta_subscription_info, map);
            }
            if (subscriptions.length == 1 && subscriptions[0].isSearchTemplate() && !full_lookup) {
                searchTemplateOK(subscriptions[0], download);
            }
        }
    } catch (Throwable e) {
        log("Failed to record associations", e);
    }
    if (changed) {
        Iterator it = listeners.iterator();
        while (it.hasNext()) {
            try {
                ((SubscriptionManagerListener) it.next()).associationsChanged(association_hash);
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
        }
    }
    if (assoc_added) {
        publishAssociations();
    }
    return (download_found);
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface)

Example 19 with PluginInterface

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

the class SubscriptionManagerImpl method setCategoryOnExisting.

protected void setCategoryOnExisting(SubscriptionImpl subscription, String old_category, String new_category) {
    PluginInterface default_pi = PluginInitializer.getDefaultInterface();
    Download[] downloads = default_pi.getDownloadManager().getDownloads();
    for (Download d : downloads) {
        if (subscriptionExists(d, subscription)) {
            String existing = d.getAttribute(ta_category);
            if (existing == null || existing.equals(old_category)) {
                d.setAttribute(ta_category, new_category);
            }
        }
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface)

Example 20 with PluginInterface

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

the class SubscriptionManagerImpl method initWithCore.

protected void initWithCore(Core _core) {
    synchronized (this) {
        if (started) {
            return;
        }
        started = true;
    }
    core = _core;
    final PluginInterface default_pi = PluginInitializer.getDefaultInterface();
    rss_publisher = new SubscriptionRSSFeed(this, default_pi);
    TorrentManager tm = default_pi.getTorrentManager();
    ta_subs_download = tm.getPluginAttribute("azsubs.subs_dl");
    ta_subs_download_rd = tm.getPluginAttribute("azsubs.subs_dl_rd");
    ta_subscription_info = tm.getPluginAttribute("azsubs.subs_info");
    ta_category = tm.getAttribute(TorrentAttribute.TA_CATEGORY);
    ta_networks = tm.getAttribute(TorrentAttribute.TA_NETWORKS);
    PluginInterface dht_plugin_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(DHTPlugin.class);
    if (dht_plugin_pi != null) {
        dht_plugin_public = (DHTPlugin) dht_plugin_pi.getPlugin();
        /*
			if ( Constants.isCVSVersion()){

				addListener(
						new SubscriptionManagerListener()
						{
							public void
							subscriptionAdded(
								Subscription subscription )
							{
							}

							public void
							subscriptionChanged(
								Subscription		subscription )
							{
							}

							public void
							subscriptionRemoved(
								Subscription subscription )
							{
							}

							public void
							associationsChanged(
								byte[] hash )
							{
								System.out.println( "Subscriptions changed: " + ByteFormatter.encodeString( hash ));

								Subscription[] subs = getKnownSubscriptions( hash );

								for (int i=0;i<subs.length;i++){

									System.out.println( "    " + subs[i].getString());
								}
							}
						});
			}
			*/
        default_pi.getDownloadManager().addListener(new DownloadManagerListener() {

            @Override
            public void downloadAdded(Download download) {
                Torrent torrent = download.getTorrent();
                if (torrent != null) {
                    byte[] hash = torrent.getHash();
                    Object[] entry;
                    synchronized (potential_associations2) {
                        entry = (Object[]) potential_associations2.remove(new HashWrapper(hash));
                    }
                    if (entry != null) {
                        SubscriptionImpl[] subs = (SubscriptionImpl[]) entry[0];
                        String subs_str = "";
                        for (int i = 0; i < subs.length; i++) {
                            subs_str += (i == 0 ? "" : ",") + subs[i].getName();
                        }
                        log("Applying deferred asocciation for " + ByteFormatter.encodeString(hash) + " -> " + subs_str);
                        recordAssociationsSupport(hash, subs, ((Boolean) entry[1]).booleanValue());
                    }
                }
            }

            @Override
            public void downloadRemoved(Download download) {
            }
        }, false);
        default_pi.getDownloadManager().addDownloadWillBeAddedListener(new DownloadWillBeAddedListener() {

            @Override
            public void initialised(Download download) {
                Torrent torrent = download.getTorrent();
                if (torrent != null) {
                    byte[] hash = torrent.getHash();
                    HashWrapper hw = new HashWrapper(hash);
                    Object[] entry;
                    synchronized (potential_associations2) {
                        entry = (Object[]) potential_associations2.get(hw);
                    }
                    if (entry != null) {
                        SubscriptionImpl[] subs = (SubscriptionImpl[]) entry[0];
                        prepareDownload(download, subs, null);
                    } else {
                        synchronized (potential_associations3) {
                            entry = potential_associations3.get(hw);
                        }
                        if (entry != null) {
                            Subscription[] subs = (Subscription[]) entry[0];
                            SubscriptionResult[] results = (SubscriptionResult[]) entry[1];
                            prepareDownload(download, subs, results);
                        }
                    }
                }
            }
        });
        TorrentUtils.addTorrentAttributeListener(new TorrentUtils.torrentAttributeListener() {

            @Override
            public void attributeSet(TOTorrent torrent, String attribute, Object value) {
                if (attribute == TorrentUtils.TORRENT_AZ_PROP_OBTAINED_FROM) {
                    try {
                        checkPotentialAssociations(torrent.getHash(), (String) value);
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }
            }
        });
        DelayedTask delayed_task = UtilitiesImpl.addDelayedTask("Subscriptions", new Runnable() {

            @Override
            public void run() {
                new AEThread2("Subscriptions:delayInit", true) {

                    @Override
                    public void run() {
                        asyncInit();
                    }
                }.start();
            }

            protected void asyncInit() {
                Download[] downloads = default_pi.getDownloadManager().getDownloads();
                for (int i = 0; i < downloads.length; i++) {
                    Download download = downloads[i];
                    if (download.getBooleanAttribute(ta_subs_download)) {
                        Map rd = download.getMapAttribute(ta_subs_download_rd);
                        boolean delete_it;
                        if (rd == null) {
                            delete_it = true;
                        } else {
                            delete_it = !recoverSubscriptionUpdate(download, rd);
                        }
                        if (delete_it) {
                            removeDownload(download, true);
                        }
                    }
                }
                default_pi.getDownloadManager().addListener(new DownloadManagerListener() {

                    @Override
                    public void downloadAdded(final Download download) {
                        if (!downloadIsIgnored(download)) {
                            if (!dht_plugin_public.isInitialising()) {
                                // if new download then we want to check out its subscription status
                                lookupAssociations(download.getMapAttribute(ta_subscription_info) == null);
                            } else {
                                new AEThread2("Subscriptions:delayInit", true) {

                                    @Override
                                    public void run() {
                                        lookupAssociations(download.getMapAttribute(ta_subscription_info) == null);
                                    }
                                }.start();
                            }
                        }
                    }

                    @Override
                    public void downloadRemoved(Download download) {
                    }
                }, false);
                for (int i = 0; i < PUB_ASSOC_CONC_MAX; i++) {
                    if (publishAssociations()) {
                        break;
                    }
                }
                publishSubscriptions();
                COConfigurationManager.addParameterListener(CONFIG_MAX_RESULTS, new ParameterListener() {

                    @Override
                    public void parameterChanged(String name) {
                        final int max_results = COConfigurationManager.getIntParameter(CONFIG_MAX_RESULTS);
                        new AEThread2("Subs:max results changer", true) {

                            @Override
                            public void run() {
                                checkMaxResults(max_results);
                            }
                        }.start();
                    }
                });
                SimpleTimer.addPeriodicEvent("SubscriptionChecker", TIMER_PERIOD, new TimerEventPerformer() {

                    private int ticks;

                    @Override
                    public void perform(TimerEvent event) {
                        ticks++;
                        checkStuff(ticks);
                    }
                });
            }
        });
        delayed_task.queue();
    }
    if (isSearchEnabled()) {
        try {
            default_pi.getUtilities().registerSearchProvider(new SearchProvider() {

                private Map<Integer, Object> properties = new HashMap<>();

                {
                    properties.put(PR_NAME, MessageText.getString("ConfigView.section.Subscriptions"));
                    try {
                        URL url = MagnetURIHandler.getSingleton().registerResource(new MagnetURIHandler.ResourceProvider() {

                            @Override
                            public String getUID() {
                                return (SubscriptionManager.class.getName() + ".2");
                            }

                            @Override
                            public String getFileType() {
                                return ("png");
                            }

                            @Override
                            public byte[] getData() {
                                InputStream is = getClass().getClassLoader().getResourceAsStream("com/biglybt/ui/images/subscription_icon_1616.png");
                                if (is == null) {
                                    return (null);
                                }
                                try {
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                    try {
                                        byte[] buffer = new byte[8192];
                                        while (true) {
                                            int len = is.read(buffer);
                                            if (len <= 0) {
                                                break;
                                            }
                                            baos.write(buffer, 0, len);
                                        }
                                    } finally {
                                        is.close();
                                    }
                                    return (baos.toByteArray());
                                } catch (Throwable e) {
                                    return (null);
                                }
                            }
                        });
                        properties.put(PR_ICON_URL, url.toExternalForm());
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                }

                @Override
                public SearchInstance search(Map<String, Object> search_parameters, SearchObserver observer) throws SearchException {
                    try {
                        return (searchSubscriptions(search_parameters, observer));
                    } catch (Throwable e) {
                        throw (new SearchException("Search failed", e));
                    }
                }

                @Override
                public Object getProperty(int property) {
                    return (properties.get(property));
                }

                @Override
                public void setProperty(int property, Object value) {
                    properties.put(property, value);
                }
            });
        } catch (Throwable e) {
            Debug.out("Failed to register search provider");
        }
    }
    default_pi.getUtilities().registerJSONRPCServer(new Utilities.JSONServer() {

        private List<String> methods = new ArrayList<>();

        {
            methods.add("vuze-subs-list");
        }

        @Override
        public String getName() {
            return ("Subscriptions");
        }

        @Override
        public List<String> getSupportedMethods() {
            return (methods);
        }

        @Override
        public Map call(String method, Map args) throws PluginException {
            throw (new PluginException("derp"));
        }
    });
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TOTorrent(com.biglybt.core.torrent.TOTorrent) URL(java.net.URL) Utilities(com.biglybt.pif.utils.Utilities) StaticUtilities(com.biglybt.pif.utils.StaticUtilities) DelayedTask(com.biglybt.pif.utils.DelayedTask) PlatformTorrentUtils(com.biglybt.core.torrent.PlatformTorrentUtils) GZIPInputStream(java.util.zip.GZIPInputStream) PluginInterface(com.biglybt.pif.PluginInterface) PluginException(com.biglybt.pif.PluginException) TorrentManager(com.biglybt.pif.torrent.TorrentManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TOTorrent(com.biglybt.core.torrent.TOTorrent) ParameterListener(com.biglybt.core.config.ParameterListener)

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