ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Enabling Peer-to-Peer BitTorrent Downloads with Azureus
Pages: 1, 2, 3, 4

The TorrentSeederTrackerServer Class

The TorrentSeederTrackerServer singleton class forms the core part of the server implementation. Once initialized by the ServerInitialiser ServletContextListener it is placed as an attribute in the web container's ServletContext where it can be accessed by all the various components in the application.

The single most important function of the TorrentSeederTrackerServer instance is to create a torrent file for a new upload, start the tracker server and create an initial seeder that will distribute the file to the swarm. All of the above is accomplished in the seedFile(File upload) method.

public class TorrentSeederTrackerServer {
  public static final String DATA_FILE_NAME = "distributed-data.dat";
  public static final int TRACKER_PORT_NUMBER = 6671; 
  private AzureusCore core;
   * The root directory which this server will use to store all the files
   * and settings required by the Azureus engine.
  private File serverRoot;
   * The file to distribute over BitTorrent
  private File sourceFile;
   * The current torrent being hosted.
  private TRHostTorrent currentTorrent;
  public void seedFile(File upload) throws TRHostTorrentRemovalVetoException, TOTorrentException, TRHostException{
    //Move the source file to a location within our torrent directory.
    sourceFile = new File(serverRoot,DATA_FILE_NAME); 
    TRHost trHost = core.getTrackerHost();
    TOTorrentCreator torrentCreator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(sourceFile, getTrackerURL());
    TOTorrent torrent = torrentCreator.create();
    currentTorrent = trHost.hostTorrent(torrent, true, false);
    //However, we also need to implement a seeder that 
    //will be responsible for uploading the initial 
    //file to the network.
    File bEncodedFile = new File(serverRoot + File.separator + "data.torrent");
    GlobalManager glManager = core.getGlobalManager();
    DownloadManager seederManager = glManager.addDownloadManager(bEncodedFile.getAbsolutePath(), serverRoot.getAbsolutePath());
    seederManager.addListener(new DownloadManagerListener(){
      public void stateChanged(DownloadManager manager, int state) {
          case DownloadManager.STATE_CHECKING:
            lastStatus = "Torrent Checking...";
          case DownloadManager.STATE_DOWNLOADING:
            lastStatus = "Torrent Downloading...";
    running = true;

Once the tracker server and the initial seeder are up and running, clients can call any of the methods below to gather some statistics on the application. In the example application this is done by the various JSP pages.

public long getTotalDownloaded(){}
public int getLeecherCount(){}
public int getPeerCount(){...}
public long getDataSendRate(){...}
public long getSeedFileSize(){...}
public int getNumberOfConnectedPeers(){...}
public int getNumberOfConnectedSeeds(){...}
public int getNumberOfPiecesForFile(){...}
public Date getCreationTime(){..}

The other classes in the server implementation add in the necessary glue code that provides users with the means to manage the TorrentSeederTrackerServer instance. The NewTorrentFileUploadServlet servlet for instance, handles file uploads with the help of the Jakarta Commons File Upload library while the TorrentDownloadServlet servlet is used make the produced torrent file (bEncodedFile) downloadable to clients. The full source code for this article is provided in the Resources section.

The Client Implementation

The client implementation is a Swing application that runs on the machines of all the peers that wish to receive the regular updates.

Figure 2
Figure 2. The client implementation Swing application downloading a torrent file

The client implementation does not only serve to download a file to this user, but also helps to seed the file throughout the rest of the swarm. In fact, it is recommended that the client implementation is run for as long as possible. This will ensure that there is always a great number of peers in the network, which, in turn, ensures that the swarm remains healthy.

Figure 3
Figure 3. The client implementation application seeding a file

Another important feature of the client implementation is the ability to pick up when the torrent file on the tracker server is updated by a new administrator upload. Once this is detected, the client implementation will reset itself and start downloading the latest data file.

Pages: 1, 2, 3, 4

Next Pagearrow