Closed abbasshah17 closed 4 years ago
Seems like what you ask is very particular to what you need, but we could try to implement an AlertListener that's associated to one or more Sha1Hash
objects.
interface OnTorrentAlertHandler {
void onAlert(Alert<?> alert);
}
public static final class TorrentFilteredAlertListener implements AlertListener {
private final int[] alertTypes;
private final HashSet<sha1_hash> hashes = new HashSet<>();
private final OnTorrentAlertHandler alertHandler;
public TorrentFilteredAlertListener(int[] alertTypes, OnTorrentAlertHandler alertHandler) {
this.alertTypes = alertTypes; //so that alerts can be filtered by alert type, ideally you pass
this.alertHandler = alertHandler;
}
public void trackTorrent(Sha1Hash torrentSha1) {
hashes.add(torrentSha1.swig());
}
public void stopTrackingTorrent(Sha1Hash torrentSha1) {
hashes.remove(torrentSha1.swig());
}
public void alert(Alert<?> alert) {
torrent_handle th = ((torrent_alert)((TorrentAlert)alert).swig()).getHandle();
if (th != null && th.is_valid() && alertHandler != null && hashes.contains(th.info_hash()) {
try {
alertHandler.onAlert(alert);
} catch (Throwable t) { /* handle exception */ }
}
}
You'd then define what to do with the alert by implementing a OnTorrentAlertHandler
.
You'd then add a new TorrentFilteredAlertListener
as a parameter of the
TorrentFilteredAlertListener torrentListener = new TorrentFilteredAlertListner(types, myOnTorrentAlertHandler);
sessionManager.addListener(torrentListener);
// then if you want to listen to alerts of only one torrent you'd do this:
torrentListener.trackTorrent(torrentHandle.infoHash());
// to stop tracking that torrent's alerts
torrentListener.stopTrackingTorrent(torrentHandle.infoHash());
torrentHandle.infoHash()
That is exactly the kind of thing I was looking for, I was reluctant to use names of the torrents since they can match with different torrents with same name. I can't believe I didn't think of that, it was right there! Thanks a lot.
Seems like what you ask is very particular to what you need
Really? I'd have imagined this being very useful. Any client downloading more than a single torrent would need similar functionality to update their UI with updates for each torrent separately and not have to go through the entire list of torrents to update a single one.
yes, there are so many kinds of alerts being fired and as you can see it's trivial to implement your own alert listener.
Most likely this github post will serve as a template for people wanting to do the same as you.
I wouldn't like to introduce this to the API as it then burdens the user into managing the HashSet contents (tracking/untracking), other programmers might have a different idea on how to implement this.
Closing ticket.
Out of curiosity, it'd be awesome to know the product you're using jlibtorrent with, perhaps we could put a link to it on the read me under a section of projects using jlibtorrent.
Sure, I'd really like to have my app added, but it's not live yet. Have been working on and off for the past couple years, hopefully it'll be live in the next couple of months, atleast with my current dev speed with COVID-19. I'll send you a request for it as soon as I move to production.
Awesome! Good luck with your release
@gubatron I finally published the app: YIFY BROWSER if you wanna include it in the ReadMe.
Regarding other issues where my responses are pending, I just wanna say that I haven't forgotten about them, have just been real busy. But now that the app is finally live I would be getting around to them during the weekend.
Congrats, however 2 issues.
But I'm planning on putting the app on other available stores as well.
OnePlus 7T
While attempting to listen to various alerts on a torrent via
sessionManager.addListener()
you get Alerts for all the torrents added through the session manager. Is there a way to simply listen to Alerts from selected torrents?Currently I'm matching the name from each Alert to map to the existing torrents and dispatching these events accordingly. But I'm not happy about it, it adds too much verbosity to the code and feels kinda bad/hacky approach.
If currently there is no way to do that, would it be possible to add it to a future release? And meanwhile would you point me in the correct direction on how to achieve the same effect in a better and more compact way?