Hosting a tracker

Version 4 (Alan McGovern, 07/30/2009 01:42 AM)

1 1
h1. Tracker example
2 1
3 3 Alan McGovern
[[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
4 3 Alan McGovern
[[Tracker Example#Example-2|Example 2]] Shows how to create a private tracker, which only tracks torrents explicitly registered with it
5 1
6 3 Alan McGovern
h2. Example 1
7 1
8 3 Alan McGovern
<pre><code class="java">
9 3 Alan McGovern
        public void StartTracker ()
10 3 Alan McGovern
        {
11 3 Alan McGovern
            Tracker tracker = new Tracker();
12 1
13 3 Alan McGovern
            // When unregistered torrents are allowed, if a client requests peers for
14 3 Alan McGovern
            // a torrent which is not currently monitored by the tracker, that torrent
15 3 Alan McGovern
            // will be added automatically. This is useful for creating a 'public' tracker.
16 3 Alan McGovern
            // Normally, if this is false, announce requests for torrents not already
17 3 Alan McGovern
            // registered with the engine are sent an error message.
18 3 Alan McGovern
            tracker.AllowUnregisteredTorrents = true;
19 1
20 3 Alan McGovern
            // Set the interval between announce requests to 1 hour and min interval to 10 mins
21 3 Alan McGovern
            tracker.AnnounceInterval = TimeSpan.FromHours(1);
22 3 Alan McGovern
            tracker.MinAnnounceInterval = TimeSpan.FromMinutes(10);
23 3 Alan McGovern
           
24 3 Alan McGovern
            // Create a listener which will respond to scrape/announce requests
25 3 Alan McGovern
            // You can pass a prefix like: http://153.462.23.1/announce which will
26 3 Alan McGovern
            // create a listener on port 80 (the default HTTP port) which will handle
27 3 Alan McGovern
            // all announce requests.
28 3 Alan McGovern
            HttpListener listener = new HttpListener("http://myserver.com/announce");
29 3 Alan McGovern
            tracker.RegisterListener(listener);
30 3 Alan McGovern
            listener.Start();
31 3 Alan McGovern
        }
32 3 Alan McGovern
</code></pre>
33 3 Alan McGovern
34 3 Alan McGovern
h2. Example 2
35 3 Alan McGovern
36 3 Alan McGovern
<pre><code class="java">
37 4 Alan McGovern
         // torrentDirectory is a folder containing .torrent files
38 4 Alan McGovern
        // which should be loaded into the engine
39 4 Alan McGovern
        public void StartTracker(string torrentDirectory)
40 1
        {
41 4 Alan McGovern
            // Create the tracker and register a listener as in Example 1
42 1
            Tracker tracker = new Tracker();
43 1
            HttpListener listener = new HttpListener("http://myserver.com/announce");
44 1
            tracker.RegisterListener(listener);
45 1
            listener.Start();
46 4 Alan McGovern
47 4 Alan McGovern
            // If an announce request is received for a torrent which is not registered with the
48 4 Alan McGovern
            //the tracker an error will be returned.
49 4 Alan McGovern
            tracker.AllowUnregisteredTorrents = false;
50 4 Alan McGovern
51 4 Alan McGovern
            // Load all torrents into the engine. Only announce requests for these torrents
52 4 Alan McGovern
            // (or torrents added in the future) will be processed.
53 4 Alan McGovern
            foreach (string file in Directory.GetFiles(torrentDirectory))
54 4 Alan McGovern
            {
55 4 Alan McGovern
                Torrent torrent = Torrent.Load(file);
56 4 Alan McGovern
57 4 Alan McGovern
                // InfoHashTrackable is a basic subclass of ITrackable. It only stores
58 4 Alan McGovern
                // the infohash and name of the torrent. If you need to store additional
59 4 Alan McGovern
                // data for each torrent you're tracking, just subclass ITrackable or
60 4 Alan McGovern
                // InfoHashTrackable and add the extra data there.
61 4 Alan McGovern
                InfoHashTrackable trackable = new InfoHashTrackable(torrent);
62 4 Alan McGovern
                tracker.Add(trackable);
63 4 Alan McGovern
            }
64 3 Alan McGovern
        }
65 1
</code></pre>