mpetazzoni / ttorrent

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

JetBrains team project

Ttorrent, a Java implementation of the BitTorrent protocol

Note

It's Ttorrent library version 2.0 which has a lot of improvements and may not be compatible with previous version (stored in v1.6 branch)

See this issue for details

Description

Ttorrent is a pure-Java implementation of the BitTorrent protocol, providing a BitTorrent tracker, a BitTorrent client and the related Torrent metainfo files creation and parsing capabilities. It is designed to be embedded into larger applications, but its components can also be used as standalone programs.

Ttorrent supports the following BEPs (BitTorrent enhancement proposals):

History

This tool suite was implemented as part of Turn's (http://www.turn.com) release distribution and deployment system and is used to distribute new build tarballs to a large number of machines inside a datacenter as efficiently as possible. At the time this project was started, few Java implementations of the BitTorrent protocol existed and unfortunately none of them fit our needs:

This implementation aims at providing a down-to-earth, simple to use library. No fancy protocol extensions are implemented here: just the basics that allows for the exchange and distribution of files through the BitTorrent protocol.

How to use

As a library

Client code

// First, instantiate the Client object.
SimpleClient client = new SimpleClient();

// This is the interface the client will listen on (you might need something
// else than localhost here because other peers cannot connect to localhost).
InetAddress address = InetAddress.getLocalHost();

//Start download. Thread is blocked here
try {
  client.downloadTorrent("/path/to/filed.torrent",
          "/path/to/output/directory",
          address);
  //download finished
} catch (Exception e) {
  //download failed, see exception for details
  e.printStackTrace();
}
//If you don't want to seed the torrent you can stop client
client.stop();

Tracker code

// First, instantiate a Tracker object with the port you want it to listen on.
// The default tracker port recommended by the BitTorrent protocol is 6969.
Tracker tracker = new Tracker(6969);

// Then, for each torrent you wish to announce on this tracker, simply created
// a TrackedTorrent object and pass it to the tracker.announce() method:
FilenameFilter filter = new FilenameFilter() {
  @Override
  public boolean accept(File dir, String name) {
    return name.endsWith(".torrent");
  }
};

for (File f : new File("/path/to/torrent/files").listFiles(filter)) {
  tracker.announce(TrackedTorrent.load(f));
}

//Also you can enable accepting foreign torrents.
//if tracker accepts request for unknown torrent it starts tracking the torrent automatically
tracker.setAcceptForeignTorrents(true);

// Once done, you just have to start the tracker's main operation loop:
tracker.start(true);

// You can stop the tracker when you're done with:
tracker.stop();

License

This BitTorrent library is distributed under the terms of the Apache Software License version 2.0. See COPYING file for more details.

Authors and contributors

Caveats

Contributions are welcome in all areas, even more so for these few points above!