Search in sources :

Example 26 with Torrent

use of com.biglybt.pif.torrent.Torrent in project BiglyBT by BiglySoftware.

the class MySharesView method refreshToolBarItems.

@Override
public void refreshToolBarItems(Map<String, Long> list) {
    boolean start = false, stop = false, remove = false;
    if (!CoreFactory.isCoreRunning()) {
        return;
    }
    List items = getSelectedItems();
    if (items.size() > 0) {
        PluginInterface pi = PluginInitializer.getDefaultInterface();
        com.biglybt.pif.download.DownloadManager dm = pi.getDownloadManager();
        remove = true;
        for (int i = 0; i < items.size(); i++) {
            ShareItem item = (ShareItem) items.get(i);
            try {
                Torrent t = item.getTorrent();
                Download download = dm.getDownload(t);
                if (download == null) {
                    continue;
                }
                int dl_state = download.getState();
                if (dl_state == Download.ST_ERROR) {
                } else if (dl_state != Download.ST_STOPPED) {
                    stop = true;
                } else {
                    start = true;
                }
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
        }
    }
    list.put("start", start ? UIToolBarItem.STATE_ENABLED : 0);
    list.put("stop", stop ? UIToolBarItem.STATE_ENABLED : 0);
    list.put("remove", remove ? UIToolBarItem.STATE_ENABLED : 0);
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) PluginInterface(com.biglybt.pif.PluginInterface) List(java.util.List) Download(com.biglybt.pif.download.Download)

Example 27 with Torrent

use of com.biglybt.pif.torrent.Torrent in project BiglyBT by BiglySoftware.

the class RelatedContentManager method getKeys.

private byte[][] getKeys(Download download) {
    byte[] tracker_keys = null;
    byte[] ws_keys = null;
    try {
        Torrent torrent = download.getTorrent();
        if (torrent != null) {
            TOTorrent to_torrent = PluginCoreUtils.unwrap(torrent);
            Set<String> tracker_domains = new HashSet<>();
            addURLToDomainKeySet(tracker_domains, to_torrent.getAnnounceURL());
            TOTorrentAnnounceURLGroup group = to_torrent.getAnnounceURLGroup();
            TOTorrentAnnounceURLSet[] sets = group.getAnnounceURLSets();
            for (TOTorrentAnnounceURLSet set : sets) {
                URL[] urls = set.getAnnounceURLs();
                for (URL u : urls) {
                    addURLToDomainKeySet(tracker_domains, u);
                }
            }
            tracker_keys = domainsToArray(tracker_domains, 8);
            Set<String> ws_domains = new HashSet<>();
            List getright = BDecoder.decodeStrings(getURLList(to_torrent, "url-list"));
            List webseeds = BDecoder.decodeStrings(getURLList(to_torrent, "httpseeds"));
            for (List l : new List[] { getright, webseeds }) {
                for (Object o : l) {
                    if (o instanceof String) {
                        try {
                            addURLToDomainKeySet(ws_domains, new URL((String) o));
                        } catch (Throwable e) {
                        }
                    }
                }
            }
            ws_keys = domainsToArray(ws_domains, 3);
        }
    } catch (Throwable e) {
    }
    return (new byte[][] { tracker_keys, ws_keys });
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup) URL(java.net.URL) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 28 with Torrent

use of com.biglybt.pif.torrent.Torrent in project BiglyBT by BiglySoftware.

the class PlatformContentDirectory method lookupContentFile.

@Override
public ContentFile lookupContentFile(Map attributes) {
    byte[] hash = (byte[]) attributes.get(AT_BTIH);
    int index = ((Integer) attributes.get(AT_FILE_INDEX)).intValue();
    try {
        Download download = downloadManager.getDownload(hash);
        if (download == null) {
            return (null);
        }
        Torrent t_torrent = download.getTorrent();
        if (t_torrent == null) {
            return (null);
        }
        String ud_key = "PlatformContentDirectory" + ":" + index;
        ContentFile acf = (ContentFile) download.getUserData(ud_key);
        if (acf != null) {
            return (acf);
        }
        final DiskManagerFileInfo file = download.getDiskManagerFileInfo(index);
        acf = new ContentFile() {

            @Override
            public DiskManagerFileInfo getFile() {
                return (file);
            }

            @Override
            public Object getProperty(String name) {
                try {
                    if (name.equals(PT_DATE)) {
                        return (new Long(file.getDownload().getCreationTime()));
                    } else if (name.equals(PT_CATEGORIES)) {
                        try {
                            String cat = file.getDownload().getCategoryName();
                            if (cat != null && cat.length() > 0) {
                                if (!cat.equalsIgnoreCase("Categories.uncategorized")) {
                                    return (new String[] { cat });
                                }
                            }
                        } catch (Throwable e) {
                        }
                        return (new String[0]);
                    } else if (name.equals(PT_TAGS)) {
                        List<Tag> tags = TagManagerFactory.getTagManager().getTagsForTaggable(PluginCoreUtils.unwrap(file.getDownload()));
                        List<String> tag_names = new ArrayList<>();
                        for (Tag tag : tags) {
                            if (tag.getTagType().getTagType() == TagType.TT_DOWNLOAD_MANUAL) {
                                tag_names.add(tag.getTagName(true));
                            }
                        }
                        return (tag_names.toArray(new String[tag_names.size()]));
                    } else if (name.equals(PT_PERCENT_DONE)) {
                        long size = file.getLength();
                        return (new Long(size == 0 ? 100 : (1000 * file.getDownloaded() / size)));
                    } else if (name.equals(PT_ETA)) {
                        return (getETA(file));
                    }
                } catch (Throwable e) {
                }
                return (null);
            }
        };
        download.setUserData(ud_key, acf);
        final ContentFile f_acf = acf;
        download.addAttributeListener(new DownloadAttributeListener() {

            @Override
            public void attributeEventOccurred(Download download, TorrentAttribute attribute, int eventType) {
                fireCatsChanged(f_acf);
            }
        }, ta_category, DownloadAttributeListener.WRITTEN);
        TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL).addTagListener(PluginCoreUtils.unwrap(download), new TagListener() {

            @Override
            public void taggableSync(Tag tag) {
            }

            @Override
            public void taggableRemoved(Tag tag, Taggable tagged) {
                update(tagged);
            }

            @Override
            public void taggableAdded(Tag tag, Taggable tagged) {
                update(tagged);
            }

            private void update(Taggable tagged) {
                fireTagsChanged(f_acf);
            }
        });
        return (acf);
    } catch (Throwable e) {
        return (null);
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) DownloadAttributeListener(com.biglybt.pif.download.DownloadAttributeListener) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteList(com.biglybt.core.util.CopyOnWriteList) Download(com.biglybt.pif.download.Download)

Example 29 with Torrent

use of com.biglybt.pif.torrent.Torrent in project BiglyBT by BiglySoftware.

the class RelatedContentManager method addDownloads.

protected void addDownloads(Download[] downloads, boolean initialising) {
    synchronized (rcm_lock) {
        List<DownloadInfo> new_info = new ArrayList<>(downloads.length);
        for (Download download : downloads) {
            try {
                if (!download.isPersistent()) {
                    continue;
                }
                Torrent torrent = download.getTorrent();
                if (torrent == null) {
                    continue;
                }
                byte[] hash = torrent.getHash();
                if (download_info_map.containsKey(hash)) {
                    continue;
                }
                byte nets = getNetworks(download);
                if (nets == NET_NONE) {
                    continue;
                }
                TOTorrent to_torrent = PluginCoreUtils.unwrap(torrent);
                if (!(TorrentUtils.isReallyPrivate(to_torrent) || TorrentUtils.getFlag(to_torrent, TorrentUtils.TORRENT_FLAG_DISABLE_RCM))) {
                    DownloadManagerState state = PluginCoreUtils.unwrap(download).getDownloadState();
                    if (state.getFlag(DownloadManagerState.FLAG_LOW_NOISE) || state.getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
                        continue;
                    }
                    if (to_torrent.getTorrentType() == TOTorrent.TT_V1_V2 && to_torrent.getEffectiveTorrentType() == TOTorrent.TT_V2) {
                        continue;
                    }
                    LinkedList<DownloadInfo> download_infos1;
                    LinkedList<DownloadInfo> download_infos2;
                    if ((nets & NET_PUBLIC) != 0) {
                        download_infos1 = pub_download_infos1;
                        download_infos2 = pub_download_infos2;
                    } else {
                        download_infos1 = non_pub_download_infos1;
                        download_infos2 = non_pub_download_infos2;
                    }
                    int version = RelatedContent.VERSION_INITIAL;
                    long rand = global_random_id ^ state.getLongParameter(DownloadManagerState.PARAM_RANDOM_SEED);
                    int seeds_leechers;
                    int[] aggregate_seeds_leechers = DownloadManagerStateFactory.getCachedAggregateScrapeSeedsLeechers(state);
                    if (aggregate_seeds_leechers == null) {
                        long cache = state.getLongAttribute(DownloadManagerState.AT_SCRAPE_CACHE);
                        if (cache == -1) {
                            seeds_leechers = -1;
                        } else {
                            int seeds = (int) ((cache >> 32) & 0x00ffffff);
                            int leechers = (int) (cache & 0x00ffffff);
                            seeds_leechers = (int) ((seeds << 16) | (leechers & 0xffff));
                        }
                    } else {
                        version = RelatedContent.VERSION_BETTER_SCRAPE;
                        int seeds = aggregate_seeds_leechers[0];
                        int leechers = aggregate_seeds_leechers[1];
                        seeds_leechers = (int) ((seeds << 16) | (leechers & 0xffff));
                    }
                    byte[][] keys = getKeys(download);
                    int first_seen = (int) (state.getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME) / 1000);
                    DownloadInfo info = new DownloadInfo(version, hash, hash, download.getName(), (int) rand, torrent.isPrivate() ? StringInterner.intern(torrent.getAnnounceURL().getHost()) : null, keys[0], keys[1], getTags(download), nets, first_seen, 0, false, torrent.getSize(), (int) (to_torrent.getCreationDate() / (60 * 60)), seeds_leechers);
                    new_info.add(info);
                    if (initialising || download_infos1.size() == 0) {
                        download_infos1.add(info);
                    } else {
                        download_infos1.add(RandomUtils.nextInt(download_infos1.size()), info);
                    }
                    download_infos2.add(info);
                    download_info_map.put(hash, info);
                    if (info.getTracker() != null) {
                        download_priv_set.add(getPrivateInfoKey(info));
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
        List<Map<String, Object>> history = (List<Map<String, Object>>) COConfigurationManager.getListParameter("rcm.dlinfo.history.privx", new ArrayList<Map<String, Object>>());
        if (initialising) {
            int padd = MAX_HISTORY - download_info_map.size();
            for (int i = 0; i < history.size() && padd > 0; i++) {
                try {
                    DownloadInfo info = deserialiseDI((Map<String, Object>) history.get(i), null);
                    if (info != null && !download_info_map.containsKey(info.getHash())) {
                        download_info_map.put(info.getHash(), info);
                        if (info.getTracker() != null) {
                            download_priv_set.add(getPrivateInfoKey(info));
                        }
                        byte nets = info.getNetworksInternal();
                        if (nets != NET_NONE) {
                            if ((nets & NET_PUBLIC) != 0) {
                                pub_download_infos1.add(info);
                                pub_download_infos2.add(info);
                            } else {
                                non_pub_download_infos1.add(info);
                                non_pub_download_infos2.add(info);
                            }
                            padd--;
                        }
                    }
                } catch (Throwable e) {
                }
            }
            Collections.shuffle(pub_download_infos1);
            Collections.shuffle(non_pub_download_infos1);
        } else {
            if (new_info.size() > 0) {
                final List<String> base32_hashes = new ArrayList<>();
                for (DownloadInfo info : new_info) {
                    byte[] hash = info.getHash();
                    if (hash != null) {
                        base32_hashes.add(Base32.encode(hash));
                    }
                    Map<String, Object> map = serialiseDI(info, null);
                    if (map != null) {
                        history.add(map);
                    }
                }
                while (history.size() > MAX_HISTORY) {
                    history.remove(0);
                }
                COConfigurationManager.setParameter("rcm.dlinfo.history.privx", history);
                if (base32_hashes.size() > 0) {
                    content_change_dispatcher.dispatch(new AERunnable() {

                        @Override
                        public void runSupport() {
                            List<RelatedContent> to_remove = new ArrayList<>();
                            synchronized (rcm_lock) {
                                ContentCache content_cache = loadRelatedContent();
                                for (String h : base32_hashes) {
                                    DownloadInfo di = content_cache.related_content.get(h);
                                    if (di != null) {
                                        to_remove.add(di);
                                    }
                                }
                            }
                            if (to_remove.size() > 0) {
                                delete(to_remove.toArray(new RelatedContent[to_remove.size()]));
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) Download(com.biglybt.pif.download.Download) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 30 with Torrent

use of com.biglybt.pif.torrent.Torrent in project BiglyBT by BiglySoftware.

the class CategoryManagerImpl method generate.

@Override
public boolean generate(TrackerWebPageRequest request, TrackerWebPageResponse response) throws IOException {
    URL url = request.getAbsoluteURL();
    String path = url.getPath();
    int pos = path.indexOf('?');
    if (pos != -1) {
        path = path.substring(0, pos);
    }
    path = path.substring(PROVIDER.length() + 1);
    XMLEscapeWriter pw = new XMLEscapeWriter(new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")));
    pw.setEnabled(false);
    if (path.length() <= 1) {
        response.setContentType("text/html; charset=UTF-8");
        pw.println("<HTML><HEAD><TITLE>" + Constants.APP_NAME + " Category Feeds</TITLE></HEAD><BODY>");
        Map<String, String> lines = new TreeMap<>();
        List<CategoryImpl> cats;
        try {
            categories_mon.enter();
            cats = new ArrayList<>(categories.values());
        } finally {
            categories_mon.exit();
        }
        for (CategoryImpl c : cats) {
            if (c.getBooleanAttribute(Category.AT_RSS_GEN)) {
                String name = getDisplayName(c);
                String cat_url = PROVIDER + "/" + URLEncoder.encode(c.getName(), "UTF-8");
                lines.put(name, "<LI><A href=\"" + cat_url + "\">" + name + "</A></LI>");
            }
        }
        for (String line : lines.values()) {
            pw.println(line);
        }
        pw.println("</BODY></HTML>");
    } else {
        String cat_name = URLDecoder.decode(path.substring(1), "UTF-8");
        CategoryImpl cat;
        try {
            categories_mon.enter();
            cat = categories.get(cat_name);
        } finally {
            categories_mon.exit();
        }
        if (cat == null) {
            response.setReplyStatus(404);
            return (true);
        }
        List<DownloadManager> dms = cat.getDownloadManagers(CoreFactory.getSingleton().getGlobalManager().getDownloadManagers());
        List<Download> downloads = new ArrayList<>(dms.size());
        long dl_marker = 0;
        for (DownloadManager dm : dms) {
            TOTorrent torrent = dm.getTorrent();
            if (torrent == null) {
                continue;
            }
            if (!TorrentUtils.isReallyPrivate(torrent)) {
                dl_marker += dm.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
                downloads.add(PluginCoreUtils.wrap(dm));
            }
        }
        String config_key = "cat.rss.config." + Base32.encode(cat.getName().getBytes("UTF-8"));
        long old_marker = COConfigurationManager.getLongParameter(config_key + ".marker", 0);
        long last_modified = COConfigurationManager.getLongParameter(config_key + ".last_mod", 0);
        long now = SystemTime.getCurrentTime();
        if (old_marker == dl_marker) {
            if (last_modified == 0) {
                last_modified = now;
            }
        } else {
            COConfigurationManager.setParameter(config_key + ".marker", dl_marker);
            last_modified = now;
        }
        if (last_modified == now) {
            COConfigurationManager.setParameter(config_key + ".last_mod", last_modified);
        }
        response.setContentType("application/xml; charset=UTF-8");
        pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        pw.println("<rss version=\"2.0\" xmlns:vuze=\"http://www.vuze.com\">");
        pw.println("<channel>");
        pw.println("<title>" + escape(getDisplayName(cat)) + "</title>");
        Collections.sort(downloads, new Comparator<Download>() {

            @Override
            public int compare(Download d1, Download d2) {
                long added1 = getAddedTime(d1) / 1000;
                long added2 = getAddedTime(d2) / 1000;
                return ((int) (added2 - added1));
            }
        });
        pw.println("<pubDate>" + TimeFormatter.getHTTPDate(last_modified) + "</pubDate>");
        for (int i = 0; i < downloads.size(); i++) {
            Download download = downloads.get(i);
            DownloadManager core_download = PluginCoreUtils.unwrap(download);
            Torrent torrent = download.getTorrent();
            byte[] hash = torrent.getHash();
            String hash_str = Base32.encode(hash);
            pw.println("<item>");
            pw.println("<title>" + escape(download.getName()) + "</title>");
            pw.println("<guid>" + hash_str + "</guid>");
            String magnet_url = escape(UrlUtils.getMagnetURI(download));
            pw.println("<link>" + magnet_url + "</link>");
            long added = core_download.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
            pw.println("<pubDate>" + TimeFormatter.getHTTPDate(added) + "</pubDate>");
            pw.println("<vuze:size>" + torrent.getSize() + "</vuze:size>");
            pw.println("<vuze:assethash>" + hash_str + "</vuze:assethash>");
            pw.println("<vuze:downloadurl>" + magnet_url + "</vuze:downloadurl>");
            DownloadScrapeResult scrape = download.getLastScrapeResult();
            if (scrape != null && scrape.getResponseType() == DownloadScrapeResult.RT_SUCCESS) {
                pw.println("<vuze:seeds>" + scrape.getSeedCount() + "</vuze:seeds>");
                pw.println("<vuze:peers>" + scrape.getNonSeedCount() + "</vuze:peers>");
            }
            pw.println("</item>");
        }
        pw.println("</channel>");
        pw.println("</rss>");
    }
    pw.flush();
    return (true);
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) XMLEscapeWriter(com.biglybt.core.xml.util.XMLEscapeWriter) DownloadManager(com.biglybt.core.download.DownloadManager) URL(java.net.URL) TOTorrent(com.biglybt.core.torrent.TOTorrent) Download(com.biglybt.pif.download.Download) TagDownload(com.biglybt.core.tag.TagDownload) DownloadScrapeResult(com.biglybt.pif.download.DownloadScrapeResult)

Aggregations

Torrent (com.biglybt.pif.torrent.Torrent)41 Download (com.biglybt.pif.download.Download)16 TOTorrent (com.biglybt.core.torrent.TOTorrent)13 URL (java.net.URL)12 PluginInterface (com.biglybt.pif.PluginInterface)7 DownloadManager (com.biglybt.core.download.DownloadManager)6 TorrentAttribute (com.biglybt.pif.torrent.TorrentAttribute)5 File (java.io.File)5 Tag (com.biglybt.core.tag.Tag)4 InetSocketAddress (java.net.InetSocketAddress)4 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)3 PEPeerManager (com.biglybt.core.peer.PEPeerManager)3 TrackerTorrent (com.biglybt.pif.tracker.TrackerTorrent)3 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)3 RPException (com.biglybt.pifimpl.remote.RPException)3 RPReply (com.biglybt.pifimpl.remote.RPReply)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ParameterListener (com.biglybt.core.config.ParameterListener)2 PEPeer (com.biglybt.core.peer.PEPeer)2