Hosting a tracker

Version 4 (Alan McGovern, 07/30/2009 01:42 AM) → Version 5/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 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 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)
{
// 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 for a torrent which is not registered with the
//the tracker an error will be returned.
tracker.AllowUnregisteredTorrents = false;

// Load all torrents into the engine. Only announce requests for these torrents
// (or torrents added in the future) will be processed.
foreach (string file in Directory.GetFiles(torrentDirectory))
{
Torrent torrent = Torrent.Load(file);

// InfoHashTrackable is a basic subclass of ITrackable. It only stores
// 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 = new InfoHashTrackable(torrent);
tracker.Add(trackable);
}
}
</code></pre>