From e165d7cb468352ee62f7be2c1da7dc8454c2f0e3 Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Tue, 2 Jan 2018 09:58:54 +0200 Subject: [PATCH] Update trackers --- .../daemon/Deluge/DelugeDirectAdapter.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java index 06621d3b..dbc1a6bb 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java @@ -75,6 +75,7 @@ import org.transdroid.daemon.task.RetrieveTask; import org.transdroid.daemon.task.RetrieveTaskSuccessResult; import org.transdroid.daemon.task.SetFilePriorityTask; import org.transdroid.daemon.task.SetLabelTask; +import org.transdroid.daemon.task.SetTrackersTask; import org.transdroid.daemon.task.SetTransferRatesTask; import se.dimovski.rencode.Rencode; @@ -138,6 +139,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { private static final String RPC_PATH = "path"; private static final String RPC_SIZE = "size"; + private static final String RPC_TRACKER_TIER = "tier"; private static final String RPC_TRACKER_URL = "url"; private static final String RPC_MAX_DOWNLOAD = "max_download_speed"; @@ -218,7 +220,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { case GetTorrentDetails: return doGetTorrentDetails((GetTorrentDetailsTask) task); case SetTrackers: - return notSupported(task); + return doSetTrackers((SetTrackersTask) task); case SetAlternativeMode: return notSupported(task); case GetStats: @@ -278,8 +280,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { } private DaemonTaskResult doControl(DaemonTask task, String method) throws DaemonException { - final Object torrentIds = new String[]{task.getTargetTorrent().getUniqueID()}; - sendRequest(method, torrentIds); + sendRequest(method, (Object) getTorrentIdsArg(task)); return new DaemonTaskSuccessResult(task); } @@ -328,8 +329,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { // We first need a listing of all the files (because we can only set the priorities all at once) final ArrayList files = getTorrentFiles(task.getTargetTorrent()); - // prepare args - final String[] torrentIds = {task.getTargetTorrent().getUniqueID()}; + // prepare options arg final Map optionsArgs = new HashMap<>(); // Build a fast access set of file to change @@ -347,10 +347,11 @@ public class DelugeDirectAdapter implements IDaemonAdapter { } optionsArgs.put(RPC_FILE_PRIORITIES, priorities); - sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, torrentIds, optionsArgs); + sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, getTorrentIdsArg(task), optionsArgs); return new DaemonTaskSuccessResult(task); } + @NonNull private DaemonTaskResult doSetTransferRates(SetTransferRatesTask task) throws DaemonException { final Map config = new HashMap<>(); config.put(RPC_MAX_DOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate()); @@ -359,6 +360,20 @@ public class DelugeDirectAdapter implements IDaemonAdapter { return new DaemonTaskSuccessResult(task); } + @NonNull + private DaemonTaskResult doSetTrackers(SetTrackersTask task) throws DaemonException { + final List> trackers = new ArrayList<>(); + final ArrayList newTrackers = task.getNewTrackers(); + for (int i = 0, n = newTrackers.size(); i < n; i++) { + final Map tracker = new HashMap<>(); + tracker.put(RPC_TRACKER_TIER, i); + tracker.put(RPC_TRACKER_URL, newTrackers.get(i)); + trackers.add(tracker); + } + sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); + return new DaemonTaskSuccessResult(task); + } + @NonNull private List getTorrents() throws DaemonException { final Map response = (Map) sendRequest( @@ -719,18 +734,19 @@ public class DelugeDirectAdapter implements IDaemonAdapter { return (int) o; } - private static class MutableInt { + @NonNull + private String[] getTorrentIdsArg(DaemonTask task) { + return new String[]{task.getTargetTorrent().getUniqueID()}; + } + private static class MutableInt { int value = 1; - MutableInt(int value) { this.value = value; } - void increment() { ++value; } - int get() { return value; }