From e8b9e980ca5929abe98ef107a9f2c2eb264a1d3d Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Tue, 2 Jan 2018 09:39:50 +0200 Subject: [PATCH] Set rates --- .../daemon/Deluge/DelugeDirectAdapter.java | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 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 60506bee..06621d3b 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.SetTransferRatesTask; import se.dimovski.rencode.Rencode; /** @@ -209,7 +210,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { case SetFilePriorities: return doSetFilePriorities((SetFilePriorityTask) task); case SetTransferRates: - return notSupported(task); + return doSetTransferRates((SetTransferRatesTask) task); case SetLabel: return doSetLabel((SetLabelTask) task); case SetDownloadLocation: @@ -232,33 +233,6 @@ public class DelugeDirectAdapter implements IDaemonAdapter { } } - private DaemonTaskResult doSetFilePriorities(SetFilePriorityTask task) throws DaemonException { - // 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()}; - final Map optionsArgs = new HashMap<>(); - - // Build a fast access set of file to change - final Set changedFiles = new HashSet<>(); - for (TorrentFile file : task.getForFiles()) { - changedFiles.add(file.getKey()); - } - - // Build array of converted priorities - final ArrayList priorities = new ArrayList<>(); - final Priority newPriority = task.getNewPriority(); - for (TorrentFile file : files) { - priorities.add( - convertPriority(changedFiles.contains(file.getKey()) ? newPriority : file.getPriority())); - } - - optionsArgs.put(RPC_FILE_PRIORITIES, priorities); - sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, torrentIds, optionsArgs); - return new DaemonTaskSuccessResult(task); - } - @Override public Daemon getType() { return settings.getType(); @@ -349,6 +323,42 @@ public class DelugeDirectAdapter implements IDaemonAdapter { return new DaemonTaskSuccessResult(task); } + @NonNull + private DaemonTaskResult doSetFilePriorities(SetFilePriorityTask task) throws DaemonException { + // 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()}; + final Map optionsArgs = new HashMap<>(); + + // Build a fast access set of file to change + final Set changedFiles = new HashSet<>(); + for (TorrentFile file : task.getForFiles()) { + changedFiles.add(file.getKey()); + } + + // Build array of converted priorities + final ArrayList priorities = new ArrayList<>(); + final Priority newPriority = task.getNewPriority(); + for (TorrentFile file : files) { + priorities.add( + convertPriority(changedFiles.contains(file.getKey()) ? newPriority : file.getPriority())); + } + + optionsArgs.put(RPC_FILE_PRIORITIES, priorities); + sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, torrentIds, optionsArgs); + return new DaemonTaskSuccessResult(task); + } + + private DaemonTaskResult doSetTransferRates(SetTransferRatesTask task) throws DaemonException { + final Map config = new HashMap<>(); + config.put(RPC_MAX_DOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate()); + config.put(RPC_MAX_UPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate()); + sendRequest(RPC_METHOD_SETCONFIG, config); + return new DaemonTaskSuccessResult(task); + } + @NonNull private List getTorrents() throws DaemonException { final Map response = (Map) sendRequest( @@ -474,11 +484,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { return files; } - private Object sendRequest(String method, Object... args) throws DaemonException { - return sendRequest(method, new HashMap(), args); - } - - private Object sendRequest(String method, Map kwargs, Object... args) + private Object sendRequest(String method, Object... args) throws DaemonException { final List requests = new ArrayList<>(); final String username = settings.getUsername(); @@ -491,6 +497,7 @@ public class DelugeDirectAdapter implements IDaemonAdapter { new Object[]{username, password}, new HashMap<>()}); } + final HashMap kwargs = new HashMap<>(); requests.add(new Object[]{requestIdCounter.getAndIncrement(), method, args, kwargs}); final byte[] request;