Browse Source

Added a dummy client adapter for fast UI testing.

pull/11/head
Eric Kok 12 years ago
parent
commit
22cb487080
  1. BIN
      core/libs/transdroid-connect.jar
  2. 2
      core/res/values/strings.xml
  3. 47
      lib/src/org/transdroid/daemon/Daemon.java
  4. 276
      lib/src/org/transdroid/daemon/DummyAdapter.java
  5. 4
      lib/src/org/transdroid/daemon/TorrentFile.java

BIN
core/libs/transdroid-connect.jar

Binary file not shown.

2
core/res/values/strings.xml

@ -219,6 +219,7 @@
<item>Buffalo NAS -1.31</item> <item>Buffalo NAS -1.31</item>
<item>Deluge 1.2+</item> <item>Deluge 1.2+</item>
<item>DLink Router BT</item> <item>DLink Router BT</item>
<item>Dummy</item>
<item>Ktorrent</item> <item>Ktorrent</item>
<item>qBittorrent</item> <item>qBittorrent</item>
<item>rTorrent</item> <item>rTorrent</item>
@ -234,6 +235,7 @@
<item>daemon_buffalonas</item> <item>daemon_buffalonas</item>
<item>daemon_deluge</item> <item>daemon_deluge</item>
<item>daemon_dlinkrouterbt</item> <item>daemon_dlinkrouterbt</item>
<item>daemon_dummy</item>
<item>daemon_ktorrent</item> <item>daemon_ktorrent</item>
<item>daemon_qbittorrent</item> <item>daemon_qbittorrent</item>
<item>daemon_rtorrent</item> <item>daemon_rtorrent</item>

47
lib/src/org/transdroid/daemon/Daemon.java

@ -59,6 +59,11 @@ public enum Daemon {
return new DelugeAdapter(settings); return new DelugeAdapter(settings);
} }
}, },
Dummy {
public IDaemonAdapter createAdapter(DaemonSettings settings) {
return new DummyAdapter(settings);
}
},
DLinkRouterBT { DLinkRouterBT {
public IDaemonAdapter createAdapter(DaemonSettings settings) { public IDaemonAdapter createAdapter(DaemonSettings settings) {
return new DLinkRouterBTAdapter(settings); return new DLinkRouterBTAdapter(settings);
@ -132,6 +137,8 @@ public enum Daemon {
return "daemon_deluge"; return "daemon_deluge";
case DLinkRouterBT: case DLinkRouterBT:
return "daemon_dlinkrouterbt"; return "daemon_dlinkrouterbt";
case Dummy:
return "daemon_dummy";
case KTorrent: case KTorrent:
return "daemon_ktorrent"; return "daemon_ktorrent";
case qBittorrent: case qBittorrent:
@ -178,6 +185,9 @@ public enum Daemon {
if (daemonCode.equals("daemon_dlinkrouterbt")) { if (daemonCode.equals("daemon_dlinkrouterbt")) {
return DLinkRouterBT; return DLinkRouterBT;
} }
if (daemonCode.equals("daemon_dummy")) {
return Dummy;
}
if (daemonCode.equals("daemon_ktorrent")) { if (daemonCode.equals("daemon_ktorrent")) {
return KTorrent; return KTorrent;
} }
@ -217,6 +227,7 @@ public enum Daemon {
case BuffaloNas: case BuffaloNas:
return 8080; return 8080;
case DLinkRouterBT: case DLinkRouterBT:
case Dummy:
case rTorrent: case rTorrent:
case Tfb4rt: case Tfb4rt:
case BitComet: case BitComet:
@ -240,19 +251,19 @@ public enum Daemon {
} }
public static boolean supportsStats(Daemon type) { public static boolean supportsStats(Daemon type) {
return type == Transmission || type == Bitflu; return type == Transmission || type == Bitflu || type == Dummy;
} }
public static boolean supportsAvailability(Daemon type) { public static boolean supportsAvailability(Daemon type) {
return type == uTorrent || type == BitTorrent || type == DLinkRouterBT || type == Transmission || type == Vuze || type == BuffaloNas; return type == uTorrent || type == BitTorrent || type == DLinkRouterBT || type == Transmission || type == Vuze || type == BuffaloNas || type == Dummy;
} }
public static boolean supportsFileListing(Daemon type) { public static boolean supportsFileListing(Daemon type) {
return type == Synology || type == Transmission || type == uTorrent || type == BitTorrent || type == KTorrent || type == Deluge || type == rTorrent || type == Vuze || type == DLinkRouterBT || type == Bitflu || type == qBittorrent || type == BuffaloNas || type == BitComet; return type == Synology || type == Transmission || type == uTorrent || type == BitTorrent || type == KTorrent || type == Deluge || type == rTorrent || type == Vuze || type == DLinkRouterBT || type == Bitflu || type == qBittorrent || type == BuffaloNas || type == BitComet || type == Dummy;
} }
public static boolean supportsFineDetails(Daemon type) { public static boolean supportsFineDetails(Daemon type) {
return type == uTorrent || type == BitTorrent || type == Daemon.Transmission || type == Deluge || type == rTorrent || type == qBittorrent; return type == uTorrent || type == BitTorrent || type == Daemon.Transmission || type == Deluge || type == rTorrent || type == qBittorrent || type == Dummy;
} }
public static boolean needsManualPathSpecified(Daemon type) { public static boolean needsManualPathSpecified(Daemon type) {
@ -260,23 +271,23 @@ public enum Daemon {
} }
public static boolean supportsFilePaths(Daemon type) { public static boolean supportsFilePaths(Daemon type) {
return type == uTorrent || type == BitTorrent || type == Vuze || type == Deluge || type == Transmission || type == rTorrent || type == KTorrent || type == BuffaloNas; return type == uTorrent || type == BitTorrent || type == Vuze || type == Deluge || type == Transmission || type == rTorrent || type == KTorrent || type == BuffaloNas || type == Dummy;
} }
public static boolean supportsStoppingStarting(Daemon type) { public static boolean supportsStoppingStarting(Daemon type) {
return type == uTorrent || type == rTorrent || type == BitTorrent || type == BitComet; return type == uTorrent || type == rTorrent || type == BitTorrent || type == BitComet || type == Dummy;
} }
public static boolean supportsForcedStarting(Daemon type) { public static boolean supportsForcedStarting(Daemon type) {
return type == uTorrent || type == BitTorrent; return type == uTorrent || type == BitTorrent || type == Dummy;
} }
public static boolean supportsCustomFolder(Daemon type) { public static boolean supportsCustomFolder(Daemon type) {
return type == rTorrent || type == Tfb4rt || type == Bitflu || type == Deluge || type == Transmission; return type == rTorrent || type == Tfb4rt || type == Bitflu || type == Deluge || type == Transmission || type == Dummy;
} }
public static boolean supportsSetTransferRates(Daemon type) { public static boolean supportsSetTransferRates(Daemon type) {
return type == Deluge || type == Transmission || type == uTorrent || type == BitTorrent || type == Deluge || type == rTorrent || type == Vuze || type == BuffaloNas || type == BitComet; return type == Deluge || type == Transmission || type == uTorrent || type == BitTorrent || type == Deluge || type == rTorrent || type == Vuze || type == BuffaloNas || type == BitComet || type == Dummy;
} }
public static boolean supportsAddByFile(Daemon type) { public static boolean supportsAddByFile(Daemon type) {
@ -285,39 +296,39 @@ public enum Daemon {
} }
public static boolean supportsAddByMagnetUrl(Daemon type) { public static boolean supportsAddByMagnetUrl(Daemon type) {
return type == uTorrent || type == BitTorrent || type == Transmission || type == Synology || type == Deluge || type == Bitflu || type == KTorrent || type == rTorrent || type == qBittorrent || type == BitComet; return type == uTorrent || type == BitTorrent || type == Transmission || type == Synology || type == Deluge || type == Bitflu || type == KTorrent || type == rTorrent || type == qBittorrent || type == BitComet || type == Dummy;
} }
public static boolean supportsRemoveWithData(Daemon type) { public static boolean supportsRemoveWithData(Daemon type) {
return type == uTorrent || type == Vuze || type == Transmission || type == Deluge || type == BitTorrent || type == Tfb4rt || type == DLinkRouterBT || type == Bitflu || type == qBittorrent || type == BuffaloNas || type == BitComet || type == rTorrent; return type == uTorrent || type == Vuze || type == Transmission || type == Deluge || type == BitTorrent || type == Tfb4rt || type == DLinkRouterBT || type == Bitflu || type == qBittorrent || type == BuffaloNas || type == BitComet || type == rTorrent || type == Dummy;
} }
public static boolean supportsFilePrioritySetting(Daemon type) { public static boolean supportsFilePrioritySetting(Daemon type) {
return type == BitTorrent || type == uTorrent || type == Transmission || type == KTorrent || type == rTorrent || type == Vuze || type == Deluge || type == qBittorrent; return type == BitTorrent || type == uTorrent || type == Transmission || type == KTorrent || type == rTorrent || type == Vuze || type == Deluge || type == qBittorrent || type == Dummy;
} }
public static boolean supportsDateAdded(Daemon type) { public static boolean supportsDateAdded(Daemon type) {
return type == Vuze || type == Transmission || type == rTorrent || type == Bitflu || type == BitComet; return type == Vuze || type == Transmission || type == rTorrent || type == Bitflu || type == BitComet || type == Dummy;
} }
public static boolean supportsLabels(Daemon type) { public static boolean supportsLabels(Daemon type) {
return type == uTorrent || type == BitTorrent || type == Deluge || type == BitComet || type == rTorrent; // || type == Vuze return type == uTorrent || type == BitTorrent || type == Deluge || type == BitComet || type == rTorrent || type == Dummy; // || type == Vuze
} }
public static boolean supportsSetLabel(Daemon type) { public static boolean supportsSetLabel(Daemon type) {
return type == uTorrent || type == BitTorrent || type == rTorrent; return type == uTorrent || type == BitTorrent || type == rTorrent || type == Dummy;
} }
public static boolean supportsSetDownloadLocation(Daemon type) { public static boolean supportsSetDownloadLocation(Daemon type) {
return type == Transmission || type == Deluge; return type == Transmission || type == Deluge || type == Dummy;
} }
public static boolean supportsSetAlternativeMode(Daemon type) { public static boolean supportsSetAlternativeMode(Daemon type) {
return type == Transmission; return type == Transmission || type == Dummy;
} }
public static boolean supportsSetTrackers(Daemon type) { public static boolean supportsSetTrackers(Daemon type) {
return type == uTorrent || type == BitTorrent || type == Deluge; return type == uTorrent || type == BitTorrent || type == Deluge || type == Dummy;
} }
public static boolean supportsExtraPassword(Daemon type) { public static boolean supportsExtraPassword(Daemon type) {

276
lib/src/org/transdroid/daemon/DummyAdapter.java

@ -0,0 +1,276 @@
/*
* This file is part of Transdroid <http://www.transdroid.org>
*
* Transdroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Transdroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.transdroid.daemon;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.AddByMagnetUrlTask;
import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
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.util.DLog;
import android.net.Uri;
/**
* A dummy adapter that does not communicate with some server, but maintains a local list of dummy data (reset every
* time it is recreated) to simplify testing.
* @author erickok
*/
public class DummyAdapter implements IDaemonAdapter {
private static final String LOG_NAME = "Dummy daemon";
private DaemonSettings settings;
private List<Torrent> dummyTorrents;
private List<Label> dummyLabels;
/**
* Initialises a dummy adapter with some dummy data that may be manipulated.
*/
public DummyAdapter(DaemonSettings settings) {
this.settings = settings;
this.dummyTorrents = new ArrayList<Torrent>();
this.dummyLabels = new ArrayList<Label>();
String[] names = new String[] { "Documentary ", "Book ", "CD Image ", "Mix tape ", "App " };
String[] labels = new String[] { "docs", "books", "isos", "music", "software" };
TorrentStatus[] statuses = new TorrentStatus[] { TorrentStatus.Seeding, TorrentStatus.Downloading, TorrentStatus.Paused, TorrentStatus.Queued, TorrentStatus.Downloading, TorrentStatus.Seeding, TorrentStatus.Error };
Random random = new Random();
for (int i = 0; i < 25; i++) {
String name = names[i % names.length] + Integer.toString(i);
TorrentStatus status = statuses[i % statuses.length];
int peersGetting = status == TorrentStatus.Downloading ? i * random.nextInt(16) : 0;
int peersSending = status == TorrentStatus.Downloading ? i * random.nextInt(16) : 0;
long size = (long) (1024D * 1024D * 1024D * i * random.nextDouble());
long left = status == TorrentStatus.Downloading ? (long) ((double)(size) / random.nextDouble()) : 0;
int rateDownload = status == TorrentStatus.Downloading ? (int) (1024D * 1024D * i * random.nextDouble())
: 0;
int rateUpload = status == TorrentStatus.Downloading || status == TorrentStatus.Seeding ? (int) (1024D * 1024D * i * random
.nextDouble()) : 0;
this.dummyTorrents.add(
new Torrent(
i,
"torrent_" + i,
name,
status,
"/downloads/" + name.replace(" ", "_"),
rateDownload,
rateUpload,
peersGetting,
peersSending,
peersGetting + peersSending, // Total connections
(peersGetting + peersSending) * 2, // Twice the total connections
(int) (status == TorrentStatus.Downloading?
left / rateDownload: 0), // Eta
size - left,
(long)((double)(size - left * 2) * random.nextDouble()), // Up to twice the amount downloaded
size,
(float)(size - left) / size, // Part done
1F, // Always 100% available
labels[i % labels.length],
new Date(System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000)), // Last week
null,
status == TorrentStatus.Error?
"Dummy error": null,
settings.getType()));
}
for (String label : labels) {
// TODO: // Add count?
dummyLabels.add(new Label(label, 0));
}
}
@Override
public DaemonTaskResult executeTask(DaemonTask task) {
try {
switch (task.getMethod()) {
case Retrieve:
return new RetrieveTaskSuccessResult((RetrieveTask) task, dummyTorrents, dummyLabels);
case GetTorrentDetails:
return new GetTorrentDetailsTaskSuccessResult((GetTorrentDetailsTask) task, new TorrentDetails(
Arrays.asList("udp://tracker.com/announce:80", "https://torrents.org/announce:443"),
task.getTargetTorrent().getStatusCode() == TorrentStatus.Error ?
Arrays.asList("Trackers not working.", "Files not available.") : null));
case GetFileList:
Torrent t = task.getTargetTorrent();
List<TorrentFile> dummyFiles = new ArrayList<TorrentFile>();
Priority priorities[] = new Priority[] { Priority.Normal, Priority.High, Priority.High, Priority.Low, Priority.Normal };
for (int i = 0; i < 25; i++) {
String fileName = "file_" + i + ".ext";
// Every file has equal part in the total size
long size = t.getTotalSize() / 25;
long done = t.getDownloadedEver() / 25;
Priority priority = priorities[i % priorities.length];
dummyFiles.add(new TorrentFile("file_" + i, t.getName() + " file " + i, fileName, t
.getLocationDir() + "/" + fileName, size, done, priority));
}
return new GetFileListTaskSuccessResult((GetFileListTask) task, dummyFiles);
case AddByFile:
String file = ((AddByFileTask) task).getFile();
DLog.d(LOG_NAME, "Adding torrent " + file);
File upload = new File(URI.create(file));
dummyTorrents.add(new Torrent(0, "torrent_file", upload.getName(), TorrentStatus.Queued, "/downloads/"
+ file, 0, 0, 0, 0, 0, 0, -1, 0, 0, 1024 * 1024 * 1000, 0, 1F, "isos", new Date(), null, null,
settings.getType()));
return new DaemonTaskSuccessResult(task);
case AddByUrl:
String url = ((AddByUrlTask) task).getUrl();
DLog.d(LOG_NAME, "Adding torrent " + url);
if (url == null || url.equals(""))
throw new DaemonException(DaemonException.ExceptionType.ParsingFailed, "No url specified");
Uri uri = Uri.parse(url);
dummyTorrents.add(new Torrent(0, "torrent_byurl", uri.getLastPathSegment(), TorrentStatus.Queued,
"/downloads/" + uri.getLastPathSegment(), 0, 0, 0, 0, 0, 0, -1, 0, 0, 1024 * 1024 * 1000, 0,
1F, "music", new Date(), null, null, settings.getType()));
return new DaemonTaskSuccessResult(task);
case AddByMagnetUrl:
String magnet = ((AddByMagnetUrlTask) task).getUrl();
DLog.d(LOG_NAME, "Adding torrent " + magnet);
Uri magnetUri = Uri.parse(magnet);
dummyTorrents.add(new Torrent(0, "torrent_magnet", magnetUri.getLastPathSegment(),
TorrentStatus.Queued, "/downloads/" + magnetUri.getLastPathSegment(), 0, 0, 0, 0, 0, 0, -1, 0,
0, 1024 * 1024 * 1000, 0, 1F, "books", new Date(), null, null, settings.getType()));
return new DaemonTaskSuccessResult(task);
case Remove:
dummyTorrents.remove(task.getTargetTorrent());
return new DaemonTaskSuccessResult(task);
case Pause:
task.getTargetTorrent().mimicPause();
return new DaemonTaskSuccessResult(task);
case PauseAll:
for (Torrent torrent: dummyTorrents) {
torrent.mimicPause();
}
return new DaemonTaskSuccessResult(task);
case Resume:
task.getTargetTorrent().mimicPause();
return new DaemonTaskSuccessResult(task);
case ResumeAll:
for (Torrent torrent: dummyTorrents) {
torrent.mimicResume();
}
return new DaemonTaskSuccessResult(task);
case Stop:
task.getTargetTorrent().mimicStop();
return new DaemonTaskSuccessResult(task);
case StopAll:
for (Torrent torrent: dummyTorrents) {
torrent.mimicStop();
}
return new DaemonTaskSuccessResult(task);
case Start:
task.getTargetTorrent().mimicStart();
return new DaemonTaskSuccessResult(task);
case StartAll:
for (Torrent torrent: dummyTorrents) {
torrent.mimicStart();
}
return new DaemonTaskSuccessResult(task);
case SetFilePriorities:
SetFilePriorityTask prioTask = (SetFilePriorityTask) task;
for (TorrentFile forFile : prioTask.getForFiles()) {
forFile.mimicPriority(prioTask.getNewPriority());
}
return new DaemonTaskSuccessResult(task);
case SetTransferRates:
// No action, as the result in not visible anyway
return new DaemonTaskSuccessResult(task);
case SetLabel:
SetLabelTask labelTask = (SetLabelTask) task;
task.getTargetTorrent().mimicNewLabel(labelTask.getNewLabel());
return new DaemonTaskSuccessResult(task);
case SetTrackers:
// TODO: Override the trackers?
return new DaemonTaskSuccessResult(task);
default:
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported,
task.getMethod() + " is not supported by " + getType()));
}
} catch (DaemonException e) {
return new DaemonTaskFailureResult(task, e);
}
}
@Override
public Daemon getType() {
return settings.getType();
}
@Override
public DaemonSettings getSettings() {
return this.settings;
}
}

4
lib/src/org/transdroid/daemon/TorrentFile.java

@ -84,6 +84,10 @@ public final class TorrentFile implements Parcelable, Comparable<TorrentFile> {
return priority; return priority;
} }
public void mimicPriority(Priority newPriority) {
priority = newPriority;
}
public float getPartDone() { public float getPartDone() {
return (float)downloaded / (float)totalSize; return (float)downloaded / (float)totalSize;
} }

Loading…
Cancel
Save