From 3b93fdc8944825bafd0691f15ba715f42ce97b45 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Thu, 2 Oct 2014 13:45:38 -0400 Subject: [PATCH] Added more alert types. Created a way to notify when a torrent changed priorities via alert. --- src/com/frostwire/jlibtorrent/Downloader.java | 2 + src/com/frostwire/jlibtorrent/Session.java | 20 +++++----- .../jlibtorrent/TorrentAlertAdapter.java | 14 ++++++- .../jlibtorrent/alerts/AlertType.java | 6 ++- .../jlibtorrent/alerts/ScrapeFailedAlert.java | 27 +++++++++++++ .../jlibtorrent/alerts/ScrapeReplyAlert.java | 36 ++++++++++++++++++ .../jlibtorrent/alerts/TorrentAlert.java | 2 +- .../alerts/TorrentPrioritizeAlert.java | 38 +++++++++++++++++++ .../jlibtorrent/alerts/TrackerAlert.java | 2 +- .../jlibtorrent/alerts/TrackerReplyAlert.java | 29 ++++++++++++++ 10 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 src/com/frostwire/jlibtorrent/alerts/ScrapeFailedAlert.java create mode 100644 src/com/frostwire/jlibtorrent/alerts/ScrapeReplyAlert.java create mode 100644 src/com/frostwire/jlibtorrent/alerts/TorrentPrioritizeAlert.java create mode 100644 src/com/frostwire/jlibtorrent/alerts/TrackerReplyAlert.java diff --git a/src/com/frostwire/jlibtorrent/Downloader.java b/src/com/frostwire/jlibtorrent/Downloader.java index b12776c44..256fd77fa 100644 --- a/src/com/frostwire/jlibtorrent/Downloader.java +++ b/src/com/frostwire/jlibtorrent/Downloader.java @@ -2,6 +2,7 @@ import com.frostwire.jlibtorrent.alerts.Alert; import com.frostwire.jlibtorrent.alerts.MetadataReceivedAlert; +import com.frostwire.jlibtorrent.alerts.TorrentPrioritizeAlert; import com.frostwire.jlibtorrent.swig.*; import java.io.File; @@ -41,6 +42,7 @@ public void download(TorrentInfo ti, File saveDir, Priority[] priorities, File r } th.prioritizeFiles(priorities); + s.fireAlert(new TorrentPrioritizeAlert(th)); } } else { // new download s.asyncAddTorrent(ti, saveDir, priorities, resumeFile); diff --git a/src/com/frostwire/jlibtorrent/Session.java b/src/com/frostwire/jlibtorrent/Session.java index 41ad02fee..1544917a0 100644 --- a/src/com/frostwire/jlibtorrent/Session.java +++ b/src/com/frostwire/jlibtorrent/Session.java @@ -730,15 +730,7 @@ public void run() { for (int i = 0; i < deque.size(); i++) { Alert a = castAlert(deque.getitem(i)); - synchronized (listeners) { - for (AlertListener l : listeners) { - try { - l.alert(a); - } catch (Throwable e) { - LOG.warn("Error calling alert listener", e); - } - } - } + fireAlert(a); } deque.clear(); @@ -751,6 +743,16 @@ public void run() { t.start(); } + void fireAlert(Alert a) { + for (AlertListener l : listeners) { + try { + l.alert(a); + } catch (Throwable e) { + LOG.warn("Error calling alert listener", e); + } + } + } + private static Map buildCastAlertTable() { Map map = new HashMap(); diff --git a/src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java b/src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java index 00628ca3b..d50e87d32 100644 --- a/src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java +++ b/src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java @@ -29,7 +29,7 @@ public final void alert(Alert alert) { return; } - if (!((TorrentAlert) alert).getSwig().getHandle().op_eq(th.getSwig())) { + if (!((TorrentAlert) alert).getHandle().getSwig().op_eq(th.getSwig())) { return; } @@ -84,6 +84,9 @@ public void fileError(FileErrorAlert alert) { public void trackerAnnounce(TrackerAnnounceAlert alert) { } + public void trackerReply(TrackerReplyAlert alert) { + } + public void trackerWarning(TrackerWarningAlert alert) { } @@ -99,6 +102,15 @@ public void stateChanged(StateChangedAlert alert) { public void dhtReply(DhtReplyAlert alert) { } + public void scrapeReply(ScrapeReplyAlert alert) { + } + + public void scrapeFailed(ScrapeFailedAlert alert) { + } + + public void torrentPrioritize(TorrentPrioritizeAlert alert) { + } + private static Map buildCallAlertTable() { Map map = new HashMap(); diff --git a/src/com/frostwire/jlibtorrent/alerts/AlertType.java b/src/com/frostwire/jlibtorrent/alerts/AlertType.java index 5ab00d5df..63059397c 100644 --- a/src/com/frostwire/jlibtorrent/alerts/AlertType.java +++ b/src/com/frostwire/jlibtorrent/alerts/AlertType.java @@ -26,6 +26,7 @@ public enum AlertType { PORTMAP_ERROR(portmap_error_alert.alert_type), PORTMAP_LOG(portmap_log_alert.alert_type), TRACKER_ANNOUNCE(tracker_announce_alert.alert_type), + TRACKER_REPLY(tracker_reply_alert.alert_type), TRACKER_WARNING(tracker_warning_alert.alert_type), TRACKER_ERROR(tracker_error_alert.alert_type), READ_PIECE(read_piece_alert.alert_type), @@ -36,7 +37,10 @@ public enum AlertType { EXTERNAL_IP(external_ip_alert.alert_type), LISTEN_SUCCEEDED(listen_succeeded_alert.alert_type), STATE_UPDATE(state_update_alert.alert_type), - UNKNOWN(-1); + SCRAPE_REPLY_ALERT(scrape_reply_alert.alert_type), + SCRAPE_FAILED_ALERT(scrape_failed_alert.alert_type), + UNKNOWN(-1), + TORRENT_PRIORITIZE(-2); private AlertType(int swigValue) { this.swigValue = swigValue; diff --git a/src/com/frostwire/jlibtorrent/alerts/ScrapeFailedAlert.java b/src/com/frostwire/jlibtorrent/alerts/ScrapeFailedAlert.java new file mode 100644 index 000000000..29fde2a78 --- /dev/null +++ b/src/com/frostwire/jlibtorrent/alerts/ScrapeFailedAlert.java @@ -0,0 +1,27 @@ +package com.frostwire.jlibtorrent.alerts; + +import com.frostwire.jlibtorrent.swig.scrape_failed_alert; + +/** + * If a scrape request fails, this alert is generated. This might be due + * to the tracker timing out, refusing connection or returning an http response + * code indicating an error. + * + * @author gubatron + * @author aldenml + */ +public final class ScrapeFailedAlert extends TrackerAlert { + + public ScrapeFailedAlert(scrape_failed_alert alert) { + super(alert); + } + + /** + * contains a message describing the error. + * + * @return + */ + public String getMsg() { + return alert.getMsg(); + } +} diff --git a/src/com/frostwire/jlibtorrent/alerts/ScrapeReplyAlert.java b/src/com/frostwire/jlibtorrent/alerts/ScrapeReplyAlert.java new file mode 100644 index 000000000..8e57a5b80 --- /dev/null +++ b/src/com/frostwire/jlibtorrent/alerts/ScrapeReplyAlert.java @@ -0,0 +1,36 @@ +package com.frostwire.jlibtorrent.alerts; + +import com.frostwire.jlibtorrent.swig.scrape_reply_alert; + +/** + * This alert is generated when a scrape request succeeds. + * + * @author gubatron + * @author aldenml + */ +public final class ScrapeReplyAlert extends TrackerAlert { + + public ScrapeReplyAlert(scrape_reply_alert alert) { + super(alert); + } + + /** + * the data returned in the scrape response. These numbers + * may be -1 if the reponse was malformed. + * + * @return + */ + public int getIncomplete() { + return alert.getIncomplete(); + } + + /** + * the data returned in the scrape response. These numbers + * may be -1 if the reponse was malformed. + * + * @return + */ + public int getComplete() { + return alert.getComplete(); + } +} diff --git a/src/com/frostwire/jlibtorrent/alerts/TorrentAlert.java b/src/com/frostwire/jlibtorrent/alerts/TorrentAlert.java index b58b1c1a3..14ae93dac 100644 --- a/src/com/frostwire/jlibtorrent/alerts/TorrentAlert.java +++ b/src/com/frostwire/jlibtorrent/alerts/TorrentAlert.java @@ -7,7 +7,7 @@ * @author gubatron * @author aldenml */ -public class TorrentAlert extends AbstractAlert { +public abstract class TorrentAlert extends AbstractAlert { public TorrentAlert(T alert) { super(alert); diff --git a/src/com/frostwire/jlibtorrent/alerts/TorrentPrioritizeAlert.java b/src/com/frostwire/jlibtorrent/alerts/TorrentPrioritizeAlert.java new file mode 100644 index 000000000..b7f022e2e --- /dev/null +++ b/src/com/frostwire/jlibtorrent/alerts/TorrentPrioritizeAlert.java @@ -0,0 +1,38 @@ +package com.frostwire.jlibtorrent.alerts; + +import com.frostwire.jlibtorrent.TorrentHandle; +import com.frostwire.jlibtorrent.swig.torrent_alert; + +/** + * @author gubatron + * @author aldenml + */ +public final class TorrentPrioritizeAlert extends TorrentAlert { + + private final TorrentHandle th; + + public TorrentPrioritizeAlert(TorrentHandle th) { + super(null); + this.th = th; + } + + @Override + public AlertType getType() { + return AlertType.TORRENT_PRIORITIZE; + } + + @Override + public String getWhat() { + return ""; + } + + @Override + public int getCategory() { + return com.frostwire.jlibtorrent.swig.alert.category_t.stats_notification.swigValue(); + } + + @Override + public TorrentHandle getHandle() { + return th; + } +} diff --git a/src/com/frostwire/jlibtorrent/alerts/TrackerAlert.java b/src/com/frostwire/jlibtorrent/alerts/TrackerAlert.java index a9ed785c4..7cf14497a 100644 --- a/src/com/frostwire/jlibtorrent/alerts/TrackerAlert.java +++ b/src/com/frostwire/jlibtorrent/alerts/TrackerAlert.java @@ -10,7 +10,7 @@ * @author gubatron * @author aldenml */ -public class TrackerAlert extends TorrentAlert { +public abstract class TrackerAlert extends TorrentAlert { public TrackerAlert(T alert) { super(alert); diff --git a/src/com/frostwire/jlibtorrent/alerts/TrackerReplyAlert.java b/src/com/frostwire/jlibtorrent/alerts/TrackerReplyAlert.java new file mode 100644 index 000000000..86895a423 --- /dev/null +++ b/src/com/frostwire/jlibtorrent/alerts/TrackerReplyAlert.java @@ -0,0 +1,29 @@ +package com.frostwire.jlibtorrent.alerts; + +import com.frostwire.jlibtorrent.swig.tracker_reply_alert; + +/** + * This alert is only for informational purpose. It is generated when a tracker announce + * succeeds. It is generated regardless what kind of tracker was used, be it UDP, HTTP or + * the DHT. + * + * @author gubatron + * @author aldenml + */ +public final class TrackerReplyAlert extends TrackerAlert { + + public TrackerReplyAlert(tracker_reply_alert alert) { + super(alert); + } + + /** + * tells how many peers the tracker returned in this response. This is + * not expected to be more thant the ``num_want`` settings. These are not necessarily + * all new peers, some of them may already be connected. + * + * @return + */ + public int getNumPeers() { + return alert.getNum_peers(); + } +}