From 3f672faca33f0521c6fa1212482fd2044b2a951c Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Tue, 2 Jan 2018 14:03:38 +0200 Subject: [PATCH] Make client a field --- .../daemon/Deluge/DelugeRpcAdapter.java | 118 +++++++----------- .../daemon/Deluge/DelugeRpcClient.java | 18 ++- 2 files changed, 54 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java index c598b5f1..51063d10 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java @@ -167,56 +167,53 @@ public class DelugeRpcAdapter implements IDaemonAdapter { }; private final DaemonSettings settings; + private final DelugeRpcClient client; private int version = -1; public DelugeRpcAdapter(DaemonSettings settings) { this.settings = settings; + client = new DelugeRpcClient(settings); } @Override public DaemonTaskResult executeTask(Log log, DaemonTask task) { try { - final DelugeRpcClient client = new DelugeRpcClient( - settings.getAddress(), - settings.getPort(), - settings.getUsername(), - settings.getPassword()); switch (task.getMethod()) { case Retrieve: - return doRetrieve(client, (RetrieveTask) task); + return doRetrieve((RetrieveTask) task); case AddByUrl: - return doAddByUrl(client, (AddByUrlTask) task); + return doAddByUrl((AddByUrlTask) task); case AddByMagnetUrl: - return doAddByMagnetUrl(client, (AddByMagnetUrlTask) task); + return doAddByMagnetUrl((AddByMagnetUrlTask) task); case AddByFile: - return doAddByFile(client, (AddByFileTask) task); + return doAddByFile((AddByFileTask) task); case Remove: - return doRemove(client, (RemoveTask) task); + return doRemove((RemoveTask) task); case Pause: - return doControl(client, task, RPC_METHOD_PAUSE); + return doControl(task, RPC_METHOD_PAUSE); case PauseAll: - return doControlAll(client, task, RPC_METHOD_PAUSE_ALL); + return doControlAll(task, RPC_METHOD_PAUSE_ALL); case Resume: - return doControl(client, task, RPC_METHOD_RESUME); + return doControl(task, RPC_METHOD_RESUME); case ResumeAll: - return doControlAll(client, task, RPC_METHOD_RESUME_ALL); + return doControlAll(task, RPC_METHOD_RESUME_ALL); case GetFileList: - return doGetFileList(client, (GetFileListTask) task); + return doGetFileList((GetFileListTask) task); case SetFilePriorities: - return doSetFilePriorities(client, (SetFilePriorityTask) task); + return doSetFilePriorities((SetFilePriorityTask) task); case SetTransferRates: - return doSetTransferRates(client, (SetTransferRatesTask) task); + return doSetTransferRates((SetTransferRatesTask) task); case SetLabel: - return doSetLabel(client, (SetLabelTask) task); + return doSetLabel((SetLabelTask) task); case SetDownloadLocation: - return doSetDownloadLocation(client, (SetDownloadLocationTask) task); + return doSetDownloadLocation((SetDownloadLocationTask) task); case GetTorrentDetails: - return doGetTorrentDetails(client, (GetTorrentDetailsTask) task); + return doGetTorrentDetails((GetTorrentDetailsTask) task); case SetTrackers: - return doSetTrackers(client, (SetTrackersTask) task); + return doSetTrackers((SetTrackersTask) task); case ForceRecheck: - return doForceRecheck(client, (ForceRecheckTask) task); + return doForceRecheck((ForceRecheckTask) task); default: return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported, @@ -229,7 +226,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @Override public Daemon getType() { - return settings.getType(); + return Daemon.DelugeRpc; } @Override @@ -238,8 +235,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) - throws DaemonException { + private RetrieveTaskSuccessResult doRetrieve(RetrieveTask task) throws DaemonException { final List results = client.sendRequests( new Request(RPC_METHOD_INFO), @@ -256,8 +252,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { return new RetrieveTaskSuccessResult(task, torrents, labels); } - private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails( - DelugeRpcClient client, GetTorrentDetailsTask task) + private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(GetTorrentDetailsTask task) throws DaemonException { //noinspection unchecked final Map response = (Map) client.sendRequest( @@ -278,35 +273,30 @@ public class DelugeRpcAdapter implements IDaemonAdapter { Collections.singletonList((String) response.get(RPC_TRACKER_STATUS)))); } - private GetFileListTaskSuccessResult doGetFileList( - DelugeRpcClient client, GetFileListTask task) throws DaemonException { - final ArrayList files = getTorrentFiles(client, task.getTargetTorrent()); + private GetFileListTaskSuccessResult doGetFileList(GetFileListTask task) throws DaemonException { + final ArrayList files = getTorrentFiles(task.getTargetTorrent()); return new GetFileListTaskSuccessResult(task, files); } - private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task, String method) - throws DaemonException { + private DaemonTaskResult doControl(DaemonTask task, String method) throws DaemonException { client.sendRequest(method, (Object) getTorrentIdsArg(task)); return new DaemonTaskSuccessResult(task); } - private DaemonTaskResult doRemove(DelugeRpcClient client, RemoveTask task) - throws DaemonException { + private DaemonTaskResult doRemove(RemoveTask task) throws DaemonException { client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(), task.includingData()); return new DaemonTaskSuccessResult(task); } @NonNull - private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task, String method) - throws DaemonException { + private DaemonTaskResult doControlAll(DaemonTask task, String method) throws DaemonException { client.sendRequest(method); return new DaemonTaskSuccessResult(task); } @NonNull - private DaemonTaskResult doAddByFile(DelugeRpcClient client, AddByFileTask task) - throws DaemonException { + private DaemonTaskResult doAddByFile(AddByFileTask task) throws DaemonException { final String file = task.getFile(); final String fileContent = Base64.encodeBytes(loadFile(file)); client.sendRequest(RPC_METHOD_ADD_FILE, file, fileContent, new HashMap<>()); @@ -314,22 +304,19 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private DaemonTaskResult doAddByUrl(DelugeRpcClient client, AddByUrlTask task) - throws DaemonException { + private DaemonTaskResult doAddByUrl(AddByUrlTask task) throws DaemonException { client.sendRequest(RPC_METHOD_ADD, task.getUrl(), new HashMap<>()); return new DaemonTaskSuccessResult(task); } @NonNull - private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client, AddByMagnetUrlTask task) - throws DaemonException { + private DaemonTaskResult doAddByMagnetUrl(AddByMagnetUrlTask task) throws DaemonException { client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>()); return new DaemonTaskSuccessResult(task); } @NonNull - private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) - throws DaemonException { + private DaemonTaskResult doSetLabel(SetLabelTask task) throws DaemonException { final String torrentId = task.getTargetTorrent().getUniqueID(); final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); @@ -337,10 +324,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client, - SetFilePriorityTask task) throws DaemonException { + 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(client, task.getTargetTorrent()); + final ArrayList files = getTorrentFiles(task.getTargetTorrent()); // prepare options arg final Map optionsArgs = new HashMap<>(); @@ -356,8 +342,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { final Priority newPriority = task.getNewPriority(); for (TorrentFile file : files) { priorities.add( - convertPriority(client, - changedFiles.contains(file.getKey()) ? newPriority : file.getPriority())); + convertPriority(changedFiles.contains(file.getKey()) ? newPriority : file.getPriority())); } optionsArgs.put(RPC_FILE_PRIORITIES, priorities); @@ -366,8 +351,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private DaemonTaskResult doSetTransferRates(DelugeRpcClient client, SetTransferRatesTask task) - throws DaemonException { + 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()); @@ -376,8 +360,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private DaemonTaskResult doSetTrackers(DelugeRpcClient client, SetTrackersTask task) - throws DaemonException { + 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++) { @@ -391,23 +374,19 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private DaemonTaskResult doForceRecheck(DelugeRpcClient client, ForceRecheckTask task) - throws DaemonException { + private DaemonTaskResult doForceRecheck(ForceRecheckTask task) throws DaemonException { client.sendRequest(RPC_METHOD_FORCERECHECK, getTorrentIdsArg(task)); return new DaemonTaskSuccessResult(task); } @NonNull - private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client, - SetDownloadLocationTask task) - throws DaemonException { + private DaemonTaskResult doSetDownloadLocation(SetDownloadLocationTask task) throws DaemonException { client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation()); return new DaemonTaskSuccessResult(task); } @NonNull - private List getTorrents(Collection> torrentMaps) - throws DaemonException { + private List getTorrents(Collection> torrentMaps) throws DaemonException { final List torrents = new ArrayList<>(); int id = 0; for (Map torrentMap : torrentMaps) { @@ -457,8 +436,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private List