Skip to content

Commit

Permalink
Added more alert types.
Browse files Browse the repository at this point in the history
Created a way to notify when a torrent changed priorities via alert.
  • Loading branch information
aldenml committed Oct 2, 2014
1 parent 6e6ffc9 commit 3b93fdc
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/com/frostwire/jlibtorrent/Downloader.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 11 additions & 9 deletions src/com/frostwire/jlibtorrent/Session.java
Expand Up @@ -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();
Expand All @@ -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<Integer, CastAlertFunction> buildCastAlertTable() {
Map<Integer, CastAlertFunction> map = new HashMap<Integer, CastAlertFunction>();

Expand Down
14 changes: 13 additions & 1 deletion src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java
Expand Up @@ -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;
}

Expand Down Expand Up @@ -84,6 +84,9 @@ public void fileError(FileErrorAlert alert) {
public void trackerAnnounce(TrackerAnnounceAlert alert) {
}

public void trackerReply(TrackerReplyAlert alert) {
}

public void trackerWarning(TrackerWarningAlert alert) {
}

Expand All @@ -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<String, CallAlertFunction> buildCallAlertTable() {
Map<String, CallAlertFunction> map = new HashMap<String, CallAlertFunction>();

Expand Down
6 changes: 5 additions & 1 deletion src/com/frostwire/jlibtorrent/alerts/AlertType.java
Expand Up @@ -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),
Expand All @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions 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<scrape_failed_alert> {

public ScrapeFailedAlert(scrape_failed_alert alert) {
super(alert);
}

/**
* contains a message describing the error.
*
* @return
*/
public String getMsg() {
return alert.getMsg();
}
}
36 changes: 36 additions & 0 deletions 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<scrape_reply_alert> {

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();
}
}
2 changes: 1 addition & 1 deletion src/com/frostwire/jlibtorrent/alerts/TorrentAlert.java
Expand Up @@ -7,7 +7,7 @@
* @author gubatron
* @author aldenml
*/
public class TorrentAlert<T extends torrent_alert> extends AbstractAlert<T> {
public abstract class TorrentAlert<T extends torrent_alert> extends AbstractAlert<T> {

public TorrentAlert(T alert) {
super(alert);
Expand Down
38 changes: 38 additions & 0 deletions 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<torrent_alert> {

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;
}
}
2 changes: 1 addition & 1 deletion src/com/frostwire/jlibtorrent/alerts/TrackerAlert.java
Expand Up @@ -10,7 +10,7 @@
* @author gubatron
* @author aldenml
*/
public class TrackerAlert<T extends tracker_alert> extends TorrentAlert<T> {
public abstract class TrackerAlert<T extends tracker_alert> extends TorrentAlert<T> {

public TrackerAlert(T alert) {
super(alert);
Expand Down
29 changes: 29 additions & 0 deletions 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<tracker_reply_alert> {

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();
}
}

0 comments on commit 3b93fdc

Please sign in to comment.