Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop' of github.com:se-bastiaan/TorrentStream-Androi…
Browse files Browse the repository at this point in the history
…d into develop
  • Loading branch information
se-bastiaan committed Jun 1, 2016
2 parents 1cfebb4 + e6041be commit dcda55c
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 84 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

Expand All @@ -36,7 +36,7 @@ group='com.github.se_bastiaan'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 15
Expand All @@ -58,7 +58,7 @@ android {
}

ext {
libtorrentVersion = '1.1.0.24'
libtorrentVersion = '1.1.0.31'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,26 @@ public void setSelectedFileIndex(Integer selectedFileIndex) {
this.selectedFileIndex = selectedFileIndex;

Priority[] piecePriorities = torrentHandle.getPiecePriorities();
int firstPieceIndex = -1;
int lastPieceIndex = -1;
int firstPieceIndexLocal = -1;
int lastPieceIndexLocal = -1;
for (int i = 0; i < piecePriorities.length; i++) {
if (piecePriorities[i] != Priority.IGNORE) {
if (firstPieceIndex == -1) {
firstPieceIndex = i;
if (firstPieceIndexLocal == -1) {
firstPieceIndexLocal = i;
}
piecePriorities[i] = Priority.IGNORE;
} else {
if (firstPieceIndex != -1 && lastPieceIndex == -1) {
lastPieceIndex = i - 1;
if (firstPieceIndexLocal != -1 && lastPieceIndexLocal == -1) {
lastPieceIndexLocal = i - 1;
}
}
}

if (lastPieceIndex == -1) {
lastPieceIndex = piecePriorities.length - 1;
if (lastPieceIndexLocal == -1) {
lastPieceIndexLocal = piecePriorities.length - 1;
}
int pieceCount = lastPieceIndex - firstPieceIndex + 1;
int pieceLength = torrentHandle.getTorrentInfo().getPieceLength();
int pieceCount = lastPieceIndexLocal - firstPieceIndexLocal + 1;
int pieceLength = torrentHandle.getTorrentInfo().pieceLength();
int activePieceCount;
if (pieceLength > 0) {
activePieceCount = (int) (prepareSize / pieceLength);
Expand All @@ -214,8 +214,8 @@ public void setSelectedFileIndex(Integer selectedFileIndex) {
activePieceCount = pieceCount / 2;
}

this.firstPieceIndex = firstPieceIndex;
this.lastPieceIndex = lastPieceIndex;
this.firstPieceIndex = firstPieceIndexLocal;
this.lastPieceIndex = lastPieceIndexLocal;
piecesToPrepare = activePieceCount;
}

Expand Down Expand Up @@ -271,7 +271,7 @@ public void startDownload() {
TorrentInfo torrentInfo = torrentHandle.getTorrentInfo();
TorrentStatus status = torrentHandle.getStatus();

double blockCount = indices.size() * torrentInfo.getPieceLength() / status.getBlockSize();
double blockCount = indices.size() * torrentInfo.pieceLength() / status.getBlockSize();

progressStep = 100 / blockCount;

Expand Down Expand Up @@ -387,6 +387,8 @@ public void alert(Alert<?> alert) {
case BLOCK_FINISHED:
blockFinished((BlockFinishedAlert) alert);
break;
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.File;

public class TorrentOptions {
public final class TorrentOptions {

protected String saveLocation = "/";
protected String proxyHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import android.os.Handler;
import android.os.HandlerThread;

import com.frostwire.jlibtorrent.DHT;
import com.frostwire.jlibtorrent.Dht;
import com.frostwire.jlibtorrent.Downloader;
import com.frostwire.jlibtorrent.Priority;
import com.frostwire.jlibtorrent.Session;
Expand Down Expand Up @@ -49,26 +49,43 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class TorrentStream {
public final class TorrentStream {

private static final String LIBTORRENT_THREAD_NAME = "TORRENTSTREAM_LIBTORRENT", STREAMING_THREAD_NAME = "TORRENTSTREAMER_STREAMING";
private static TorrentStream sThis;

private CountDownLatch initialisingLatch;
private Session torrentSession;
private DHT dht;
private Dht dht;
private Boolean initialising = false, initialised = false, isStreaming = false, isCanceled = false;
private TorrentOptions torrentOptions;

private Torrent currentTorrent;
private String currentTorrentUrl;
private Integer dhtNodes = 0;

private List<TorrentListener> listeners = new ArrayList<>();
private final List<TorrentListener> listeners = new ArrayList<>();

private HandlerThread libTorrentThread, streamingThread;
private Handler libTorrentHandler, streamingHandler;

private final DHTStatsAlertListener dhtStatsAlertListener = new DHTStatsAlertListener() {
@Override
public void stats(int totalDhtNodes) {
dhtNodes = totalDhtNodes;
}
};

private final TorrentAddedAlertListener torrentAddedAlertListener = new TorrentAddedAlertListener() {
@Override
public void torrentAdded(TorrentAddedAlert alert) {
InternalTorrentListener listener = new InternalTorrentListener();
TorrentHandle th = torrentSession.findTorrent((alert).handle().getInfoHash());
currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize);
torrentSession.addListener(currentTorrent);
}
};

private TorrentStream(TorrentOptions options) {
torrentOptions = options;
initialise();
Expand All @@ -90,10 +107,8 @@ private void initialise() {
if (libTorrentThread != null && torrentSession != null) {
resumeSession();
} else {
if (initialising || initialised) {
if (libTorrentThread != null) {
libTorrentThread.interrupt();
}
if ((initialising || initialised) && libTorrentThread != null) {
libTorrentThread.interrupt();
}

initialising = true;
Expand All @@ -111,7 +126,7 @@ public void run() {

torrentSession.addListener(dhtStatsAlertListener);

dht = new DHT(torrentSession);
dht = new Dht(torrentSession);
dht.start();

initialising = false;
Expand Down Expand Up @@ -277,19 +292,17 @@ public void run() {
currentTorrentUrl = torrentUrl;

File saveDirectory = new File(torrentOptions.saveLocation);
if (!saveDirectory.isDirectory()) {
if (!saveDirectory.mkdirs()) {
for (final TorrentListener listener : listeners) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onStreamError(null, new DirectoryModifyException());
}
});
}
isStreaming = false;
return;
if (!saveDirectory.isDirectory() && !saveDirectory.mkdirs()) {
for (final TorrentListener listener : listeners) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onStreamError(null, new DirectoryModifyException());
}
});
}
isStreaming = false;
return;
}

torrentSession.removeListener(torrentAddedAlertListener);
Expand Down Expand Up @@ -452,23 +465,6 @@ public void removeListener(TorrentListener listener) {
listeners.remove(listener);
}

private DHTStatsAlertListener dhtStatsAlertListener = new DHTStatsAlertListener() {
@Override
public void stats(int totalDhtNodes) {
dhtNodes = totalDhtNodes;
}
};

private TorrentAddedAlertListener torrentAddedAlertListener = new TorrentAddedAlertListener() {
@Override
public void torrentAdded(TorrentAddedAlert alert) {
InternalTorrentListener listener = new InternalTorrentListener();
TorrentHandle th = torrentSession.findTorrent((alert).getHandle().getInfoHash());
currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize);
torrentSession.addListener(currentTorrent);
}
};

protected class InternalTorrentListener implements TorrentListener {

public void onStreamStarted(final Torrent torrent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
package com.github.se_bastiaan.torrentstream.listeners;

import com.frostwire.jlibtorrent.AlertListener;
import com.frostwire.jlibtorrent.DHTRoutingBucket;
import com.frostwire.jlibtorrent.DhtRoutingBucket;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.AlertType;
import com.frostwire.jlibtorrent.alerts.DhtStatsAlert;
import java.util.ArrayList;

public abstract class DHTStatsAlertListener implements AlertListener {
@Override
Expand All @@ -41,12 +42,11 @@ public void alert(Alert<?> alert) {
public abstract void stats(int totalDhtNodes);

private int countTotalDHTNodes(DhtStatsAlert alert) {
final DHTRoutingBucket[] routingTable = alert.routingTable();
final ArrayList<DhtRoutingBucket> routingTable = alert.routingTable();

int totalNodes = 0;
if (routingTable != null && routingTable.length > 0) {
for (int i = 0; i < routingTable.length; i++) {
DHTRoutingBucket bucket = routingTable[i];
if (routingTable != null) {
for (DhtRoutingBucket bucket : routingTable) {
totalNodes += bucket.numNodes();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void alert(Alert<?> alert) {
case TORRENT_ADDED:
torrentAdded((TorrentAddedAlert) alert);
break;
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

import java.io.File;

public class FileUtils {
public final class FileUtils {

private FileUtils() throws InstantiationException {
throw new InstantiationException("This class is not created for instantiation");
}

/**
* Delete every item below the File location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import android.os.Handler;
import android.os.Looper;

public class ThreadUtils {
public final class ThreadUtils {

private ThreadUtils() throws InstantiationException {
throw new InstantiationException("This class is not created for instantiation");
}
/**
* Execute the given {@link Runnable} on the ui thread.
*
Expand Down
Loading

0 comments on commit dcda55c

Please sign in to comment.