Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions bencoding/src/main/java/com/turn/ttorrent/bcodec/BEValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public long getLong() throws InvalidBEncodingException {
*/
@SuppressWarnings("unchecked")
public List<BEValue> getList() throws InvalidBEncodingException {
if (this.value instanceof ArrayList) {
return (ArrayList<BEValue>) this.value;
if (this.value != null && List.class.isAssignableFrom(this.value.getClass())) {
return (List<BEValue>) this.value;
} else {
throw new InvalidBEncodingException("Excepted List<BEvalue> !");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ public interface TorrentMetadata extends TorrentHash {
*/
List<TorrentFile> getFiles();

/**
* @return list of web seeds (URLs) or <code>null</code> if web seeds are not specified
*/
@Nullable
List<String> getWebSeeds();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.turn.ttorrent.common;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -20,6 +19,8 @@ public class TorrentMetadataImpl implements TorrentMetadata {
private final int myPieceLength;
private final byte[] myPiecesHashes;
private final String myHexString;
@Nullable
private final List<String> myWebSeeds;

TorrentMetadataImpl(byte[] infoHash,
@Nullable List<List<String>> announceList,
Expand All @@ -31,7 +32,9 @@ public class TorrentMetadataImpl implements TorrentMetadata {
List<TorrentFile> files,
int pieceCount,
int pieceLength,
byte[] piecesHashes) {
byte[] piecesHashes,
@Nullable final List<String> webSeeds
) {
myInfoHash = infoHash;
myAnnounceList = announceList;
myMainAnnounce = mainAnnounce;
Expand All @@ -44,6 +47,7 @@ public class TorrentMetadataImpl implements TorrentMetadata {
myPieceLength = pieceLength;
myPiecesHashes = piecesHashes;
myHexString = TorrentUtils.byteArrayToHexString(myInfoHash);
myWebSeeds = webSeeds;
}

@Override
Expand All @@ -56,6 +60,11 @@ public List<TorrentFile> getFiles() {
return myFiles;
}

@Override
public List<String> getWebSeeds() {
return myWebSeeds;
}

@Nullable
@Override
public List<List<String>> getAnnounceList() {
Expand Down
15 changes: 14 additions & 1 deletion common/src/main/java/com/turn/ttorrent/common/TorrentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public TorrentMetadata parse(byte[] metadata) throws InvalidBEncodingException,
final byte[] piecesHashes = getRequiredValueOrThrowException(infoTable, PIECES).getBytes();

final boolean torrentContainsManyFiles = infoTable.get(FILES) != null;
final List<String> webSeeds = getWebSeeds(dictionaryMetadata);

final String dirName = getRequiredValueOrThrowException(infoTable, NAME).getString();

Expand Down Expand Up @@ -82,10 +83,22 @@ public TorrentMetadata parse(byte[] metadata) throws InvalidBEncodingException,
files,
piecesCount,
pieceLength,
piecesHashes
piecesHashes,
webSeeds
);
}

private List<String> getWebSeeds(Map<String, BEValue> dictionaryMetadata) throws InvalidBEncodingException {
List<String> result = new ArrayList<String>();
final BEValue list = dictionaryMetadata.get(URL_LIST);
if (list != null) {
for (BEValue url : list.getList()) {
result.add(url.getString());
}
}
return result;
}

private List<TorrentFile> parseFiles(Map<String, BEValue> infoTable, boolean torrentContainsManyFiles, String name) throws InvalidBEncodingException {
if (!torrentContainsManyFiles) {
final BEValue md5Sum = infoTable.get(MD5_SUM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public byte[] serialize(TorrentMetadata metadata) throws IOException {
}
infoTable.put(FILES, new BEValue(files));
}
final List<String> webSeeds = metadata.getWebSeeds();
if (webSeeds != null && !webSeeds.isEmpty()) {
mapMetadata.put(URL_LIST, new BEValue(mapStringListToBEValueList(webSeeds)));
}

mapMetadata.put(INFO_TABLE, new BEValue(infoTable));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,37 @@ public void missingRequiredFieldTest() {
fail("", e);
}
}

public void webSeedsTest() throws IOException {
final Map<String, BEValue> metadata = new HashMap<String, BEValue>();
final HashMap<String, BEValue> infoTable = new HashMap<String, BEValue>();

metadata.put("announce", new BEValue("http://localhost/announce"));

infoTable.put("piece length", new BEValue(4));

infoTable.put("pieces", new BEValue(new byte[100]));
infoTable.put("name", new BEValue("test.file"));
infoTable.put("length", new BEValue(19));

List<BEValue> webSeeds = new ArrayList<BEValue>();
webSeeds.add(new BEValue("http://localhost/webseed"));
webSeeds.add(new BEValue("http://localhost/webseed2"));
metadata.put("url-list", new BEValue(webSeeds));

metadata.put("info", new BEValue(infoTable));

final TorrentMetadata torrentMetadata = myTorrentParser.parse(BEncoder.bencode(metadata).array());

assertEquals(torrentMetadata.getPieceLength(), 4);
assertEquals(torrentMetadata.getAnnounce(), "http://localhost/announce");
assertEquals(torrentMetadata.getDirectoryName(), "test.file");
assertNull(torrentMetadata.getAnnounceList());

List<String> actualWebSeeds = torrentMetadata.getWebSeeds();
assertNotNull(actualWebSeeds);
assertEquals(actualWebSeeds.get(0), "http://localhost/webseed");
assertEquals(actualWebSeeds.get(1), "http://localhost/webseed2");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.turn.ttorrent.common;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Test
public class TorrentSerializerTest {

private TorrentSerializer myTorrentSerializer;

@BeforeMethod
public void setUp() {
myTorrentSerializer = new TorrentSerializer();
}

public void testWebSeeds() throws IOException {
List<String> webSeeds = new ArrayList<String>();
webSeeds.add("http://localhost/webseed1");
webSeeds.add("ftp://localhost/webseed2");
webSeeds.add("https://localhost/ABC");

final List<TorrentFile> files = new ArrayList<TorrentFile>();
final List<String> relativePath = new ArrayList<String>();
relativePath.add("file.txt");
files.add(new TorrentFile(relativePath, 10, null));
final TorrentMetadataImpl metadata =
new TorrentMetadataImpl(new byte[0], null, "main-announce", 19,
"comment", "created by", "name", files, 10,
10, new byte[10], webSeeds
);

final byte[] output = myTorrentSerializer.serialize(metadata);
assertNotNull(output);

final String outputString = new String(output);
System.out.println(outputString);

for (String url : webSeeds) {
assertTrue(
outputString.contains(url.length() + ":" + url),
"url not found in output: " + url + ", output: " + outputString
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,18 @@ public void testMultiFileWithOneFileValues() throws IOException {
}

}

public void webSeedsTest() throws IOException {
Map<String, BEValue> metadata = new MetadataBuilder()
.addDataSource(new ByteArrayInputStream(new byte[]{1, 2}), "path/some_file", true)
.setDirectoryName("root")
.addWebSeedUrl("http://localhost:8080")
.addWebSeedUrl("http://localhost:8081")
.buildBEP().getMap();
List<BEValue> urlList = metadata.get(URL_LIST).getList();
assertEquals(urlList.size(), 2);
assertEquals(urlList.get(0).getString(), "http://localhost:8080");
assertEquals(urlList.get(1).getString(), "http://localhost:8081");
}

}
20 changes: 19 additions & 1 deletion ttorrent-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,22 @@

</dependencies>

</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public void stop() {
this.stop(60, TimeUnit.SECONDS);
}

void stop(int timeout, TimeUnit timeUnit) {
public void stop(int timeout, TimeUnit timeUnit) {
boolean wasStopped = this.stop.getAndSet(true);
if (wasStopped) return;

Expand Down