Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Strategy Pattern in the piece selection algorithm #149

Open
wants to merge 61 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
82fb651
Fix read when message size wasn't read in one step
pankdm Apr 10, 2013
728edf8
Throw AnnounceException when current tracker client isn't available.
sroze Jul 18, 2013
9370d86
Fix order of uploaded/downloaded parameters in HTTPAnnounceRequestMes…
mpetazzoni Jul 22, 2013
1ad97ef
Merge pull request #45 from sroze/trackerless-patch
mpetazzoni Jul 22, 2013
2e643f4
add the ability to set upload/download rate-limits (in kB/sec.) on sh…
ptgoetz Jul 19, 2013
b15ea5e
add javadoc documentation for upload/download rate-limit functionality
ptgoetz Jul 22, 2013
73b78f2
reduce rate limit code duplication by refactoring code present in bot…
ptgoetz Jul 22, 2013
3ef9e9b
add example of upload/download rate limiting to the client usage sample.
ptgoetz Jul 22, 2013
13db7ab
add comments documenting the drawbacks/potential improvements for the…
ptgoetz Jul 22, 2013
00bc8f2
minor formatting/style modifications based on feedback from pull requ…
ptgoetz Jul 22, 2013
42775d5
Merge pull request #49 from ptgoetz/rate-limits
mpetazzoni Jul 23, 2013
59a2ec1
Fix a few styling issues from recent contributions
mpetazzoni Jul 23, 2013
c004fa8
Merge pull request #40 from pankdm/fix-incomplete-read
mpetazzoni Jul 23, 2013
64b8865
fixes Issue #47
oxlade39 Jul 22, 2013
a47a275
fixes turn/ttorrent#51
pwoodworth Jul 24, 2013
0dcb033
Merge pull request #50 from oxlade39/bug_fix
mpetazzoni Jul 24, 2013
bf4d679
Merge pull request #53 from pwoodworth/issue-51
mpetazzoni Jul 25, 2013
c1dbcd9
Update pom for compatibility with the maven-release-plugin
pwoodworth Jul 30, 2013
9e8abed
Use maven-shade-plugin to produce executable jar
pwoodworth Jul 30, 2013
5a499ce
Clean up javadoc problems
pwoodworth Jul 30, 2013
c06b110
Merge pull request #54 from pwoodworth/shaded
mpetazzoni Jul 31, 2013
f69190b
Merge pull request #55 from pwoodworth/releaseplugin
mpetazzoni Jul 31, 2013
89a6754
Merge pull request #56 from pwoodworth/javadoc
mpetazzoni Jul 31, 2013
0080296
Fix pom parent relative path
pwoodworth Aug 14, 2013
9e09ee5
Adding more platform agnostic shell scripts
pwoodworth Aug 15, 2013
d50eb99
Removing old shell scripts
pwoodworth Aug 15, 2013
86f4a2f
Merge pull request #57 from pwoodworth/scripts
mpetazzoni Aug 16, 2013
1c8dbfc
Move entry-point main methods to separate package
pwoodworth Aug 26, 2013
d33c361
Merge pull request #59 from pwoodworth/climove
mpetazzoni Aug 26, 2013
dbfd004
Fixes turn/ttorrent#42
pwoodworth Aug 28, 2013
06a3dfc
Merge pull request #60 from pwoodworth/issue-42
mpetazzoni Aug 28, 2013
dd8e7c9
Use commons-io throughout
pwoodworth Sep 11, 2013
f72705f
Merge pull request #63 from pwoodworth/commonsio
mpetazzoni Sep 12, 2013
d243274
Use commons-codec for hex and digest encoding
pwoodworth Aug 28, 2013
6fd448f
Merge pull request #64 from pwoodworth/codec
mpetazzoni Sep 17, 2013
869f664
Fix Piece compareTo ordering
pwoodworth Sep 20, 2013
32c66e1
Correcting improper use of Java 7 API
pwoodworth Sep 26, 2013
d787d9c
Merge pull request #65 from pwoodworth/piececompare
mpetazzoni Sep 30, 2013
cce0c02
Developer contact info updates
mpetazzoni Oct 24, 2013
b540ebd
Change FileCollectionStorage to not write 0 bytes to the next file at…
camerondavison Nov 25, 2013
053b13d
Instead of abusing SocketChannel.read in a tight loop, use a Selector
maxaf Oct 27, 2013
6ff991d
Fixing email address of contributor in README
mpetazzoni Nov 26, 2013
37bff2a
Code style fixes
mpetazzoni Nov 26, 2013
186dbb3
Merge pull request #73 from a86c6f7964/collectionfileselect
mpetazzoni Nov 26, 2013
d403b7a
Create Travis-CI configuration
mpetazzoni Dec 6, 2013
9dac22a
Add Travis-CI status image
mpetazzoni Dec 6, 2013
5c9f3cf
Bump to version 1.4; first release to Maven Central
mpetazzoni Dec 6, 2013
51f0676
[maven-release-plugin] prepare release ttorrent-1.4
mpetazzoni Dec 6, 2013
efe3a26
[maven-release-plugin] prepare for next development iteration
mpetazzoni Dec 6, 2013
9550b79
Add documentation about declaring ttorrent as a POM dependency
mpetazzoni Dec 8, 2013
fd9d585
Update README.md
ralscha Apr 12, 2014
04e9b9a
Merge pull request #93 from ralscha/patch-1
mpetazzoni Apr 12, 2014
755483b
Added support for multiple announce URLs to CLI
Chet-Thales Jul 21, 2014
101007e
Merge pull request #100 from chester8mcgee/master
mpetazzoni Jul 21, 2014
9d3642a
Changing Strategy to ask pieces for peers
Aug 15, 2014
a871771
Changing Strategy to ask pieces for peers
era Aug 15, 2014
2b4bf49
Removing for now the temp file extension
era Aug 15, 2014
83319ca
Add toMagnetURI in Torrent object
era Aug 18, 2014
c49f96c
Using strategy pattern to allow change the piece selection
era Sep 1, 2014
3b1a2e2
Fixing broke cli program
era Sep 1, 2014
4293b2c
Changing the README.md to explain the new constructor
era Sep 1, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Ignore build output
/build/*
/release.properties

# Ignore Javadoc output
/doc/*
Expand All @@ -18,3 +19,4 @@
*.bak
*~
*~
/build/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: java
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Ttorrent, a Java implementation of the BitTorrent protocol
==========================================================

[![Build Status](https://travis-ci.org/mpetazzoni/ttorrent.png)](https://travis-ci.org/mpetazzoni/ttorrent)

Description
-----------

Expand Down Expand Up @@ -28,6 +30,12 @@ Ttorrent supports the following BEPs (BitTorrent enhancement proposals):
Compact peer lists are supported in both the client and the tracker.
Currently the tracker only supports sending back compact peer lists
to an announce request.

About the Fork
=============

This fork was created to allow Java Programmers to choose their own piece selection algorithm. In my own very case, I wanted to download the first pieces first (because I was working in a streaming system like Popcorn-time), so I created the IndexFirstStrategy. The RareFirstStrategy was created by the original author, I just extrated his code from the Piece class and put in the RareFirstStrategy class.


History
-------
Expand Down Expand Up @@ -65,6 +73,22 @@ usage message on the console when invoked with the ``-h`` command-line flag.

### As a library

To use ``ttorrent`` is a library in your project, all you need is to
declare the dependency on the latest version of ``ttorrent``. For
example, if you use Maven, add the following in your POM's dependencies
section:

```xml
<dependencies>
...
<dependency>
<groupId>com.turn</groupId>
<artifactId>ttorrent</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
```

*Thanks to Anatoli Vladev for the code examples in #16.*

#### Client code
Expand All @@ -80,7 +104,17 @@ Client client = new Client(
// output directory. Partials downloads are automatically recovered.
SharedTorrent.fromFile(
new File("/path/to/your.torrent"),
new File("/path/to/output/directory")));
new File("/path/to/output/directory")),

//An implementation of PieceSelectionStrategy
//The RareFirstStrategy is the most commom one
new RareFirstStrategy());

// You can optionally set download/upload rate limits
// in kB/second. Setting a limit to 0.0 disables rate
// limits.
client.setMaxDownloadRate(50.0);
client.setMaxUploadRate(50.0);

// At this point, can you either call download() to download the torrent and
// stop immediately after...
Expand Down Expand Up @@ -134,20 +168,23 @@ License version 2.0. See COPYING file for more details.
Authors and contributors
------------------------

* Maxime Petazzoni <<[email protected]>> (Platform Engineer at Turn, Inc)
* Maxime Petazzoni <<[email protected]>> (Software Engineer at SignalFuse, Inc)
Original author, main developer and maintainer
* David Giffin <<[email protected]>>
Contributed parallel hashing and multi-file torrent support.
* Thomas Zink <<[email protected]>>
Fixed a piece length computation issue when the total torrent size is an
exact multiple of the piece size.
* Johan Parent <<parent_johan@yahoo.com>>
* Johan Parent <<parent\_johan@yahoo.com>>
Fixed a bug in unfresh peer collection and issues on download completion on
Windows platforms.
* Dmitriy Dumanskiy
Contributed the switch from Ant to Maven.
* Alexey Ptashniy
Fixed an integer overflow in the calculation of a torrent's full size.
* Elias Granja <<[email protected]>>

Contributed with the Strategy Pattern.


Caveats
Expand Down
18 changes: 0 additions & 18 deletions bin/tracker

This file was deleted.

73 changes: 73 additions & 0 deletions bin/ttorrent
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh

# Copyright (C) 2012 Turn, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

EXECJAR="ttorrent-*-shaded.jar"

real_path() {
case $1 in
/*)
SCRIPT="$1"
;;
*)
PWD=`pwd`
SCRIPT="$PWD/$1"
;;
esac
CHANGED=true
while [ "X$CHANGED" != "X" ] ; do
# Change spaces to ":" so the tokens can be parsed.
SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
# Get the real path to this script, resolving any symbolic links
TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
REALPATH=
for C in $TOKENS; do
# Change any ":" in the token back to a space.
C=`echo $C | sed -e 's;:; ;g'`
REALPATH="$REALPATH/$C"
# If REALPATH is a sym link, resolve it. Loop for nested links.
while [ -h "$REALPATH" ] ; do
LS="`ls -ld "$REALPATH"`"
LINK="`expr "$LS" : '.*-> \(.*\)$'`"
if expr "$LINK" : '/.*' > /dev/null; then
# LINK is absolute.
REALPATH="$LINK"
else
# LINK is relative.
REALPATH="`dirname "$REALPATH"`""/$LINK"
fi
done
done
if [ "$REALPATH" = "$SCRIPT" ] ; then
CHANGED=""
else
SCRIPT="$REALPATH"
fi
done
echo "$REALPATH"
}

base=$(dirname $(real_path $0))
CPARG=$(find ${base}/../build -name "$EXECJAR" | tail -n 1)
if [ -z "$CPARG" ] ; then
echo "Unable to find $EXECJAR"
exit 1
fi
if [ -z "$MAINCLASS" ] ; then
CPARG="-jar $CPARG"
else
CPARG="-cp $CPARG $MAINCLASS"
fi
exec java $CPARG "$@"
6 changes: 3 additions & 3 deletions bin/client → bin/ttorrent-torrent
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

# Copyright (C) 2012 Turn, Inc.
#
Expand All @@ -14,5 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

base=$(dirname $(readlink -f $0))
exec java -jar $(find ${base}/../build -name "ttorrent-*.jar" | tail -n 1) $*
EXEFILE="${0%-torrent}"
MAINCLASS="com.turn.ttorrent.cli.TorrentMain" "${EXEFILE}" "$@"
6 changes: 3 additions & 3 deletions bin/torrent → bin/ttorrent-tracker
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

# Copyright (C) 2012 Turn, Inc.
#
Expand All @@ -14,5 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

base=$(dirname $(readlink -f $0))
exec java -cp $(find ${base}/../build -name "ttorrent-*.jar" | tail -n 1) com.turn.ttorrent.common.Torrent $*
EXEFILE="${0%-tracker}"
MAINCLASS="com.turn.ttorrent.cli.TrackerMain" "${EXEFILE}" "$@"
76 changes: 43 additions & 33 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
<relativePath />
</parent>

<name>Java BitTorrent library</name>
Expand All @@ -14,20 +14,17 @@
including support for several BEPs. It also provides a standalone client,
a tracker and a torrent manipulation utility.
</description>
<url>http://turn.github.com/ttorrent/</url>
<url>http://mpetazzoni.github.io/ttorrent/</url>
<groupId>com.turn</groupId>
<artifactId>ttorrent</artifactId>
<version>1.2</version>
<version>1.5-SNAPSHOT</version>
<packaging>jar</packaging>

<organization>
<name>Turn, Inc.</name>
<url>http://www.turn.com</url>
</organization>

<scm>
<connection>scm:git:git://github.com/turn/ttorrent.git</connection>
<url>http://github.com/turn/ttorrent</url>
<connection>scm:git:git://github.com/mpetazzoni/ttorrent.git</connection>
<developerConnection>scm:git:ssh://[email protected]/mpetazzoni/ttorrent.git</developerConnection>
<url>http://github.com/mpetazzoni/ttorrent</url>
<tag>master</tag>
</scm>

<licenses>
Expand All @@ -39,17 +36,17 @@

<issueManagement>
<system>GitHub</system>
<url>https://github.com/turn/ttorrent/issues</url>
<url>https://github.com/mpetazzoni/ttorrent/issues</url>
</issueManagement>

<developers>
<developer>
<id>mpetazzoni</id>
<name>Maxime Petazzoni</name>
<email>[email protected]</email>
<email>[email protected]</email>
<url>http://www.bulix.org</url>
<organization>Turn, Inc</organization>
<organizationUrl>http://www.turn.com</organizationUrl>
<organization>SignalFuse, Inc</organization>
<organizationUrl>http://www.signalfuse.com</organizationUrl>
<roles>
<role>maintainer</role>
<role>architect</role>
Expand All @@ -75,10 +72,16 @@
</repositories>

<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
<version>2.4</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -127,6 +130,11 @@
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<includes>
<include>**</include>
</includes>
Expand All @@ -144,28 +152,30 @@
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>false</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<mainClass>com.turn.ttorrent.client.Client</mainClass>
</manifest>
</archive>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.2</version>
</plugin>

<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<!--change assembly to single if you don't want include dependencies in jar-->
<goal>assembly</goal>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-shaded.jar</outputFile>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.turn.ttorrent.cli.ClientMain</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/turn/ttorrent/bcodec/BDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public BEValue bdecodeNumber() throws IOException {
c = this.read();
if (c == '0')
throw new InvalidBEncodingException("Negative zero not allowed");
chars[off] = (char)c;
chars[off] = '-';
off++;
}

Expand Down
Loading