mpetazzoni / ttorrent

BitTorrent Java library with tracker and download client
http://mpetazzoni.github.com/ttorrent/
Apache License 2.0
1.38k stars 502 forks source link

Transmission download problem #206

Open Hronom opened 7 years ago

Hronom commented 7 years ago

I've got the problem that, Transmission client able to download files from the torrent only when one of clients(ttorrent clients) downloading file(not in seed mode). Please help, I need to seed to the Transmission clients.

Here is my Tracker code:

import com.turn.ttorrent.tracker.TrackedTorrent;
import com.turn.ttorrent.tracker.Tracker;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

public class TtorrentServer {
    public static void main(String[] args)
        throws IOException, NoSuchAlgorithmException, InterruptedException {
        Tracker tracker = new Tracker(new InetSocketAddress(6969));
        FilenameFilter filter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".torrent");
            }
        };

        for (File f : new File(".").listFiles(filter)) {
            TrackedTorrent trackedTorrent = TrackedTorrent.load(f);
            tracker.announce(trackedTorrent);
        }
        tracker.start();
        Thread.sleep(TimeUnit.DAYS.toMillis(100));
        tracker.stop();
    }
}

Here is the code of master client:

import com.turn.ttorrent.client.Client;
import com.turn.ttorrent.client.SharedTorrent;
import com.turn.ttorrent.common.Torrent;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import static java.nio.file.StandardOpenOption.CREATE;

public class TtorrentClientMaster {
    public static void main(String[] args)
        throws IOException, NoSuchAlgorithmException, URISyntaxException, InterruptedException {
        ArrayList<File> files = new ArrayList<>();
        listf("files", files);
        Torrent torrent = SharedTorrent.create(
            new File("files"),
            files,
            new URI("http://0.0.0.0:6969/announce"),
            "Hronom"
        );
        try (OutputStream outputStream = Files.newOutputStream(Paths.get("files.torrent"), CREATE)) {
            torrent.save(outputStream);
        }

        Client client = new Client(null, new SharedTorrent(torrent, new File(".")));
        client.share();
        Thread.sleep(TimeUnit.DAYS.toMillis(100));
        client.stop();
    }

    public static void listf(String directoryName, ArrayList<File> files) throws IOException {
        File directory = new File(directoryName);

        // get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList) {
            if (file.isFile()) {
                files.add(file);
            } else if (file.isDirectory()) {
                listf(file.getPath(), files);
            }
        }
    }
}

Here is the code of my slave client:

import com.turn.ttorrent.client.Client;
import com.turn.ttorrent.client.SharedTorrent;
import com.turn.ttorrent.common.Torrent;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TtorrentClientSlave1 {
    public static void main(String[] args)
        throws IOException, NoSuchAlgorithmException, URISyntaxException, InterruptedException {
        ArrayList<File> files = new ArrayList<>();
        SharedTorrent sharedTorrent =
            SharedTorrent.fromFile(new File("files.torrent"), new File("./output_torrent"));
        Client client = new Client(null, sharedTorrent);
        client.share();
        Thread.sleep(TimeUnit.DAYS.toMillis(100));
        client.stop();
    }
}