Browse Source

Make client a field

pull/407/head
Alon Albert 7 years ago
parent
commit
3f672faca3
  1. 118
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java
  2. 18
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcClient.java

118
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 DaemonSettings settings;
private final DelugeRpcClient client;
private int version = -1; private int version = -1;
public DelugeRpcAdapter(DaemonSettings settings) { public DelugeRpcAdapter(DaemonSettings settings) {
this.settings = settings; this.settings = settings;
client = new DelugeRpcClient(settings);
} }
@Override @Override
public DaemonTaskResult executeTask(Log log, DaemonTask task) { public DaemonTaskResult executeTask(Log log, DaemonTask task) {
try { try {
final DelugeRpcClient client = new DelugeRpcClient(
settings.getAddress(),
settings.getPort(),
settings.getUsername(),
settings.getPassword());
switch (task.getMethod()) { switch (task.getMethod()) {
case Retrieve: case Retrieve:
return doRetrieve(client, (RetrieveTask) task); return doRetrieve((RetrieveTask) task);
case AddByUrl: case AddByUrl:
return doAddByUrl(client, (AddByUrlTask) task); return doAddByUrl((AddByUrlTask) task);
case AddByMagnetUrl: case AddByMagnetUrl:
return doAddByMagnetUrl(client, (AddByMagnetUrlTask) task); return doAddByMagnetUrl((AddByMagnetUrlTask) task);
case AddByFile: case AddByFile:
return doAddByFile(client, (AddByFileTask) task); return doAddByFile((AddByFileTask) task);
case Remove: case Remove:
return doRemove(client, (RemoveTask) task); return doRemove((RemoveTask) task);
case Pause: case Pause:
return doControl(client, task, RPC_METHOD_PAUSE); return doControl(task, RPC_METHOD_PAUSE);
case PauseAll: case PauseAll:
return doControlAll(client, task, RPC_METHOD_PAUSE_ALL); return doControlAll(task, RPC_METHOD_PAUSE_ALL);
case Resume: case Resume:
return doControl(client, task, RPC_METHOD_RESUME); return doControl(task, RPC_METHOD_RESUME);
case ResumeAll: case ResumeAll:
return doControlAll(client, task, RPC_METHOD_RESUME_ALL); return doControlAll(task, RPC_METHOD_RESUME_ALL);
case GetFileList: case GetFileList:
return doGetFileList(client, (GetFileListTask) task); return doGetFileList((GetFileListTask) task);
case SetFilePriorities: case SetFilePriorities:
return doSetFilePriorities(client, (SetFilePriorityTask) task); return doSetFilePriorities((SetFilePriorityTask) task);
case SetTransferRates: case SetTransferRates:
return doSetTransferRates(client, (SetTransferRatesTask) task); return doSetTransferRates((SetTransferRatesTask) task);
case SetLabel: case SetLabel:
return doSetLabel(client, (SetLabelTask) task); return doSetLabel((SetLabelTask) task);
case SetDownloadLocation: case SetDownloadLocation:
return doSetDownloadLocation(client, (SetDownloadLocationTask) task); return doSetDownloadLocation((SetDownloadLocationTask) task);
case GetTorrentDetails: case GetTorrentDetails:
return doGetTorrentDetails(client, (GetTorrentDetailsTask) task); return doGetTorrentDetails((GetTorrentDetailsTask) task);
case SetTrackers: case SetTrackers:
return doSetTrackers(client, (SetTrackersTask) task); return doSetTrackers((SetTrackersTask) task);
case ForceRecheck: case ForceRecheck:
return doForceRecheck(client, (ForceRecheckTask) task); return doForceRecheck((ForceRecheckTask) task);
default: default:
return new DaemonTaskFailureResult(task, return new DaemonTaskFailureResult(task,
new DaemonException(ExceptionType.MethodUnsupported, new DaemonException(ExceptionType.MethodUnsupported,
@ -229,7 +226,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
@Override @Override
public Daemon getType() { public Daemon getType() {
return settings.getType(); return Daemon.DelugeRpc;
} }
@Override @Override
@ -238,8 +235,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) private RetrieveTaskSuccessResult doRetrieve(RetrieveTask task) throws DaemonException {
throws DaemonException {
final List<Object> results = client.sendRequests( final List<Object> results = client.sendRequests(
new Request(RPC_METHOD_INFO), new Request(RPC_METHOD_INFO),
@ -256,8 +252,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
return new RetrieveTaskSuccessResult(task, torrents, labels); return new RetrieveTaskSuccessResult(task, torrents, labels);
} }
private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails( private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(GetTorrentDetailsTask task)
DelugeRpcClient client, GetTorrentDetailsTask task)
throws DaemonException { throws DaemonException {
//noinspection unchecked //noinspection unchecked
final Map<String, Object> response = (Map<String, Object>) client.sendRequest( final Map<String, Object> response = (Map<String, Object>) client.sendRequest(
@ -278,35 +273,30 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
Collections.singletonList((String) response.get(RPC_TRACKER_STATUS)))); Collections.singletonList((String) response.get(RPC_TRACKER_STATUS))));
} }
private GetFileListTaskSuccessResult doGetFileList( private GetFileListTaskSuccessResult doGetFileList(GetFileListTask task) throws DaemonException {
DelugeRpcClient client, GetFileListTask task) throws DaemonException { final ArrayList<TorrentFile> files = getTorrentFiles(task.getTargetTorrent());
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent());
return new GetFileListTaskSuccessResult(task, files); return new GetFileListTaskSuccessResult(task, files);
} }
private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task, String method) private DaemonTaskResult doControl(DaemonTask task, String method) throws DaemonException {
throws DaemonException {
client.sendRequest(method, (Object) getTorrentIdsArg(task)); client.sendRequest(method, (Object) getTorrentIdsArg(task));
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
private DaemonTaskResult doRemove(DelugeRpcClient client, RemoveTask task) private DaemonTaskResult doRemove(RemoveTask task) throws DaemonException {
throws DaemonException {
client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(), client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(),
task.includingData()); task.includingData());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task, String method) private DaemonTaskResult doControlAll(DaemonTask task, String method) throws DaemonException {
throws DaemonException {
client.sendRequest(method); client.sendRequest(method);
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private DaemonTaskResult doAddByFile(DelugeRpcClient client, AddByFileTask task) private DaemonTaskResult doAddByFile(AddByFileTask task) throws DaemonException {
throws DaemonException {
final String file = task.getFile(); final String file = task.getFile();
final String fileContent = Base64.encodeBytes(loadFile(file)); final String fileContent = Base64.encodeBytes(loadFile(file));
client.sendRequest(RPC_METHOD_ADD_FILE, file, fileContent, new HashMap<>()); client.sendRequest(RPC_METHOD_ADD_FILE, file, fileContent, new HashMap<>());
@ -314,22 +304,19 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private DaemonTaskResult doAddByUrl(DelugeRpcClient client, AddByUrlTask task) private DaemonTaskResult doAddByUrl(AddByUrlTask task) throws DaemonException {
throws DaemonException {
client.sendRequest(RPC_METHOD_ADD, task.getUrl(), new HashMap<>()); client.sendRequest(RPC_METHOD_ADD, task.getUrl(), new HashMap<>());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client, AddByMagnetUrlTask task) private DaemonTaskResult doAddByMagnetUrl(AddByMagnetUrlTask task) throws DaemonException {
throws DaemonException {
client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>()); client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) private DaemonTaskResult doSetLabel(SetLabelTask task) throws DaemonException {
throws DaemonException {
final String torrentId = task.getTargetTorrent().getUniqueID(); final String torrentId = task.getTargetTorrent().getUniqueID();
final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); final String label = task.getNewLabel() == null ? "" : task.getNewLabel();
client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label);
@ -337,10 +324,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client, private DaemonTaskResult doSetFilePriorities(SetFilePriorityTask task) throws DaemonException {
SetFilePriorityTask task) throws DaemonException {
// We first need a listing of all the files (because we can only set the priorities all at once) // We first need a listing of all the files (because we can only set the priorities all at once)
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent()); final ArrayList<TorrentFile> files = getTorrentFiles(task.getTargetTorrent());
// prepare options arg // prepare options arg
final Map<String, Object> optionsArgs = new HashMap<>(); final Map<String, Object> optionsArgs = new HashMap<>();
@ -356,8 +342,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final Priority newPriority = task.getNewPriority(); final Priority newPriority = task.getNewPriority();
for (TorrentFile file : files) { for (TorrentFile file : files) {
priorities.add( priorities.add(
convertPriority(client, convertPriority(changedFiles.contains(file.getKey()) ? newPriority : file.getPriority()));
changedFiles.contains(file.getKey()) ? newPriority : file.getPriority()));
} }
optionsArgs.put(RPC_FILE_PRIORITIES, priorities); optionsArgs.put(RPC_FILE_PRIORITIES, priorities);
@ -366,8 +351,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private DaemonTaskResult doSetTransferRates(DelugeRpcClient client, SetTransferRatesTask task) private DaemonTaskResult doSetTransferRates(SetTransferRatesTask task) throws DaemonException {
throws DaemonException {
final Map<String, Object> config = new HashMap<>(); final Map<String, Object> config = new HashMap<>();
config.put(RPC_MAX_DOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate()); config.put(RPC_MAX_DOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate());
config.put(RPC_MAX_UPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate()); config.put(RPC_MAX_UPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate());
@ -376,8 +360,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private DaemonTaskResult doSetTrackers(DelugeRpcClient client, SetTrackersTask task) private DaemonTaskResult doSetTrackers(SetTrackersTask task) throws DaemonException {
throws DaemonException {
final List<Map<String, Object>> trackers = new ArrayList<>(); final List<Map<String, Object>> trackers = new ArrayList<>();
final ArrayList<String> newTrackers = task.getNewTrackers(); final ArrayList<String> newTrackers = task.getNewTrackers();
for (int i = 0, n = newTrackers.size(); i < n; i++) { for (int i = 0, n = newTrackers.size(); i < n; i++) {
@ -391,23 +374,19 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private DaemonTaskResult doForceRecheck(DelugeRpcClient client, ForceRecheckTask task) private DaemonTaskResult doForceRecheck(ForceRecheckTask task) throws DaemonException {
throws DaemonException {
client.sendRequest(RPC_METHOD_FORCERECHECK, getTorrentIdsArg(task)); client.sendRequest(RPC_METHOD_FORCERECHECK, getTorrentIdsArg(task));
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client, private DaemonTaskResult doSetDownloadLocation(SetDownloadLocationTask task) throws DaemonException {
SetDownloadLocationTask task)
throws DaemonException {
client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation()); client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
@NonNull @NonNull
private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) throws DaemonException {
throws DaemonException {
final List<Torrent> torrents = new ArrayList<>(); final List<Torrent> torrents = new ArrayList<>();
int id = 0; int id = 0;
for (Map<String, Object> torrentMap : torrentMaps) { for (Map<String, Object> torrentMap : torrentMaps) {
@ -457,8 +436,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) throws DaemonException {
throws DaemonException {
// First get all labels that torrents and count them // First get all labels that torrents and count them
final Map<String, MutableInt> labelCounters = new HashMap<>(); final Map<String, MutableInt> labelCounters = new HashMap<>();
for (Torrent torrent : torrents) { for (Torrent torrent : torrents) {
@ -487,8 +465,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
@NonNull @NonNull
private ArrayList<TorrentFile> getTorrentFiles(DelugeRpcClient client, Torrent torrent) private ArrayList<TorrentFile> getTorrentFiles(Torrent torrent) throws DaemonException {
throws DaemonException {
final ArrayList<TorrentFile> files = new ArrayList<>(); final ArrayList<TorrentFile> files = new ArrayList<>();
//noinspection unchecked //noinspection unchecked
final Map<String, Object> response = (Map<String, Object>) client.sendRequest( final Map<String, Object> response = (Map<String, Object>) client.sendRequest(
@ -518,7 +495,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
torrent.getLocationDir() + path, torrent.getLocationDir() + path,
size, size,
(long) (size * progress), (long) (size * progress),
convertDelugePriority(client, priority))); convertDelugePriority(priority)));
} }
return files; return files;
} }
@ -575,9 +552,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
// TODO: Move method to a common file used by both Adapters. // TODO: Move method to a common file used by both Adapters.
@NonNull @NonNull
private Priority convertDelugePriority(DelugeRpcClient client, int priority) private Priority convertDelugePriority(int priority)
throws DaemonException { throws DaemonException {
ensureVersion(client); ensureVersion();
if (version >= 10303) { if (version >= 10303) {
// Priority codes changes from Deluge 1.3.3 onwards // Priority codes changes from Deluge 1.3.3 onwards
switch (priority) { switch (priority) {
@ -605,8 +582,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
// TODO: Move method to a common file used by both Adapters. // TODO: Move method to a common file used by both Adapters.
private int convertPriority(DelugeRpcClient client, Priority priority) throws DaemonException { private int convertPriority(Priority priority) throws DaemonException {
ensureVersion(client); ensureVersion();
if (version >= 10303) { if (version >= 10303) {
// Priority codes changes from Deluge 1.3.3 onwards // Priority codes changes from Deluge 1.3.3 onwards
switch (priority) { switch (priority) {
@ -633,7 +610,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
} }
} }
private void ensureVersion(DelugeRpcClient client) throws DaemonException { private void ensureVersion() throws DaemonException {
if (version > 0) { if (version > 0) {
return; return;
} }
@ -673,5 +650,4 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
private Object getTorrentIdsArg(DaemonTask task) { private Object getTorrentIdsArg(DaemonTask task) {
return new String[]{task.getTargetTorrent().getUniqueID()}; return new String[]{task.getTargetTorrent().getUniqueID()};
} }
} }

18
app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcClient.java

@ -20,6 +20,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import org.transdroid.daemon.DaemonException; import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType; import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import se.dimovski.rencode.Rencode; import se.dimovski.rencode.Rencode;
/** /**
@ -30,16 +31,10 @@ class DelugeRpcClient {
private static final String RPC_METHOD_LOGIN = "daemon.login"; private static final String RPC_METHOD_LOGIN = "daemon.login";
private static final int RPC_ERROR = 2; private static final int RPC_ERROR = 2;
private final String address; private final DaemonSettings settings;
private final int port;
private final String username;
private final String password;
public DelugeRpcClient(String address, int port, String username, String password) { public DelugeRpcClient(DaemonSettings settings) {
this.address = address; this.settings = settings;
this.port = port;
this.username = username;
this.password = password;
} }
@NonNull @NonNull
@ -53,8 +48,9 @@ class DelugeRpcClient {
final List<Object> requestObjects = new ArrayList<>(); final List<Object> requestObjects = new ArrayList<>();
int loginRequestId = -1; int loginRequestId = -1;
final String username = settings.getUsername();
if (!TextUtils.isEmpty(username)) { if (!TextUtils.isEmpty(username)) {
final Request loginRequest = new Request(RPC_METHOD_LOGIN, username, password); final Request loginRequest = new Request(RPC_METHOD_LOGIN, username, settings.getPassword());
requestObjects.add(loginRequest.toObject()); requestObjects.add(loginRequest.toObject());
loginRequestId = loginRequest.getId(); loginRequestId = loginRequest.getId();
} }
@ -149,7 +145,7 @@ class DelugeRpcClient {
final SSLContext sslContext = SSLContext.getInstance("TLSv1"); final SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext.getSocketFactory().createSocket(address, port); return sslContext.getSocketFactory().createSocket(settings.getAddress(), settings.getPort());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new DaemonException(ExceptionType.ConnectionError, throw new DaemonException(ExceptionType.ConnectionError,
"Failed to open socket: " + e.getMessage()); "Failed to open socket: " + e.getMessage());

Loading…
Cancel
Save