Hosting a tracker

Version 3 (Alan McGovern, 07/30/2009 01:35 AM) → Version 4/6 (Alan McGovern, 07/30/2009 01:42 AM)


h1. Tracker example

[[Tracker Example#Example-1|Example 1]] Shows how to create a basic public tracker, which tracks every torrent which it sees in an announce request
[[Tracker Example#Example-2|Example 2]] Shows how to create a private tracker, which only tracks torrents explicitly registered with it

h2. Example 1

<pre><code class="java">
public void StartTracker ()
{
Tracker tracker = new Tracker();

// When unregistered torrents are allowed, if a client requests peers for
// a torrent which is not currently monitored by the tracker, that torrent
// will be added automatically. This is useful for creating a 'public' tracker.
// Normally, if this is false, announce requests for torrents not already
// registered with the engine are sent an error message.
tracker.AllowUnregisteredTorrents = true;

// Set the interval between announce requests to 1 hour and min interval to 10 mins
tracker.AnnounceInterval = TimeSpan.FromHours(1);
tracker.MinAnnounceInterval = TimeSpan.FromMinutes(10);

// Create a listener which will respond to scrape/announce requests
// You can pass a prefix like: http://153.462.23.1/announce which will
// create a listener on port 80 (the default HTTP port) which will handle
// all announce requests.
HttpListener listener = new HttpListener("http://myserver.com/announce");
tracker.RegisterListener(listener);
listener.Start();
}
</code></pre>

h2. Example 2

<pre><code class="java">
// torrentDirectory is a folder containing .torrent files

// which should be loaded into the engine
public void StartTracker(string torrentDirectory) StartTracker ()
{
// Create the tracker and register a listener as in Example 1
Tracker tracker = new Tracker();
HttpListener listener = new HttpListener("http://myserver.com/announce");
tracker.RegisterListener(listener);
listener.Start();


// If an announce request is received When unregistered torrents are allowed, if a client requests peers for
//
a torrent which is not currently monitored by the tracker, that torrent
// will be added automatically. This is useful for creating a 'public' tracker.
// Normally, if this is false, announce requests for torrents not already
//
registered with the
//the tracker
engine are sent an error will be returned. message.
tracker.AllowUnregisteredTorrents = false; true;

// Load all torrents into Set the engine. Only interval between announce requests for these torrents to 1 hour and min interval to 10 mins
tracker.AnnounceInterval = TimeSpan.FromHours(1);
tracker.MinAnnounceInterval = TimeSpan.FromMinutes(10);

// (or torrents added in the future) Create a listener which will be processed. respond to scrape/announce requests
foreach (string file in Directory.GetFiles(torrentDirectory)) // You can pass a prefix like: http://153.462.23.1/announce which will
{
Torrent torrent = Torrent.Load(file);

// InfoHashTrackable is create a basic subclass of ITrackable. It only stores
listener on port 80 (the default HTTP port) which will handle
// the infohash and name of the torrent. If you need to store additional
// data for each torrent you're tracking, just subclass ITrackable or
// InfoHashTrackable and add the extra data there.
InfoHashTrackable trackable
all announce requests.
HttpListener listener
= new InfoHashTrackable(torrent);
tracker.Add(trackable);
HttpListener("http://myserver.com/announce");
} tracker.RegisterListener(listener);
listener.Start();

}
</code></pre>