use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class RelatedContentManager method lookupAttributesSupport.
void lookupAttributesSupport(final byte[] from_hash, final byte networks, final RelatedAttributeLookupListener listener) throws ContentException {
try {
if (!enabled) {
throw (new ContentException("rcm is disabled"));
}
Download from_download = getDownload(from_hash);
Set<String> existing_tags;
if (from_download != null) {
existing_tags = getExplicitTags(from_download);
} else {
existing_tags = Collections.emptySet();
}
final DHTPluginInterface dht_plugin = selectDHT(networks);
if (dht_plugin == null) {
throw (new Exception("DHT Plugin unavailable for networks " + getString(convertNetworks(networks))));
}
// really should implement a getNetwork() in DHTPluginInterface...
final String dht_plugin_network = dht_plugin == public_dht_plugin ? AENetworkClassifier.AT_PUBLIC : AENetworkClassifier.AT_I2P;
final String from_hash_str = ByteFormatter.encodeString(from_hash);
final byte[] key_bytes = ("az:rcm:assoc:" + from_hash_str).getBytes("UTF-8");
String op_str = "Content attr read: " + from_hash_str.substring(0, 16);
dht_plugin.get(key_bytes, op_str, DHTPlugin.FLAG_SINGLE_VALUE, 512, 30 * 1000, false, true, new DHTPluginOperationListener() {
private Set<String> tags = new HashSet<>();
private Set<String> swarm_tags = new HashSet<>();
@Override
public void starts(byte[] key) {
if (listener != null) {
try {
listener.lookupStart();
} catch (Throwable e) {
Debug.out(e);
}
ContentCache content_cache = loadRelatedContent();
DownloadInfo info = content_cache.related_content.get(Base32.encode(from_hash));
if (info != null) {
String[] l_tags = info.getTags();
if (l_tags != null) {
for (String tag : l_tags) {
synchronized (tags) {
if (tags.contains(tag)) {
continue;
}
tags.add(tag);
}
try {
listener.tagFound(tag, dht_plugin_network);
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
}
@Override
public boolean diversified() {
return (true);
}
@Override
public void valueRead(DHTPluginContact originator, DHTPluginValue value) {
try {
Map<String, Object> map = (Map<String, Object>) BDecoder.decode(value.getValue());
String[] r_tags = decodeTags((byte[]) map.get("m"));
if (r_tags != null) {
for (String tag : r_tags) {
synchronized (tags) {
if (!TagUtils.isInternalTagName(tag)) {
if (!existing_tags.contains(tag)) {
swarm_tags.add(tag);
}
}
if (tags.contains(tag)) {
continue;
}
tags.add(tag);
}
try {
listener.tagFound(tag, dht_plugin_network);
} catch (Throwable e) {
Debug.out(e);
}
}
}
} catch (Throwable e) {
}
}
@Override
public void valueWritten(DHTPluginContact target, DHTPluginValue value) {
}
@Override
public void complete(byte[] key, boolean timeout_occurred) {
if (from_download != null) {
synchronized (tags) {
if (!swarm_tags.isEmpty()) {
DownloadManagerState dms = PluginCoreUtils.unwrap(from_download).getDownloadState();
String[] old = dms.getListAttribute(DownloadManagerState.AT_SWARM_TAGS);
if (old == null || old.length == 0) {
dms.setListAttribute(DownloadManagerState.AT_SWARM_TAGS, swarm_tags.toArray(new String[0]));
} else {
if (old.length < 16) {
swarm_tags.addAll(Arrays.asList(old));
if (swarm_tags.size() > old.length) {
dms.setListAttribute(DownloadManagerState.AT_SWARM_TAGS, swarm_tags.toArray(new String[0]));
}
}
}
}
}
}
if (listener != null) {
try {
listener.lookupComplete();
} catch (Throwable e) {
Debug.out(e);
}
}
}
});
} catch (Throwable e) {
ContentException ce;
if ((e instanceof ContentException)) {
ce = (ContentException) e;
} else {
ce = new ContentException("Lookup failed", e);
}
if (listener != null) {
try {
listener.lookupFailed(ce);
} catch (Throwable f) {
Debug.out(f);
}
}
throw (ce);
}
}
use of com.biglybt.pif.download.Download 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);
}
}
use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class RelatedContentManager method lookupContent.
public void lookupContent(final byte[] hash, final RelatedContentLookupListener listener) throws ContentException {
if (hash == null) {
throw (new ContentException("hash is null"));
}
byte net = NET_PUBLIC;
try {
Download download = getDownload(hash);
if (download != null) {
net = getNetworks(download);
}
} catch (Throwable e) {
}
final byte f_net = net;
if (!initialisation_complete_sem.isReleasedForever() || (public_dht_plugin != null && public_dht_plugin.isInitialising())) {
AsyncDispatcher dispatcher = new AsyncDispatcher();
dispatcher.dispatch(new AERunnable() {
@Override
public void runSupport() {
try {
initialisation_complete_sem.reserve();
lookupContentSupport(hash, 0, f_net, true, listener);
} catch (ContentException e) {
Debug.out(e);
}
}
});
} else {
lookupContentSupport(hash, 0, f_net, true, listener);
}
}
use of com.biglybt.pif.download.Download 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()]));
}
}
});
}
}
}
}
}
use of com.biglybt.pif.download.Download 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);
}
Aggregations