Managing Torrents

Version 16 (Alan McGovern, 07/30/2009 01:17 AM)

1 10 Alan McGovern
h1. Managing Torrents
2 1
3 11 Alan McGovern
[[Managing Torrents#Example-1|Example 1]] shows how to add a torrent to the engine and start the download
4 12 Alan McGovern
[[Managing Torrents#Example-2|Example 2]] shows how to hash check a torrent and show a progress indicator
5 16 Alan McGovern
[[Managing Torrents#Example-3|Example 3]] shows how to stop a torrent
6 9 Alan McGovern
7 9 Alan McGovern
h2. Example 1
8 5 olivier dufour
9 4 olivier dufour
<pre><code class="java">
10 8 Alan McGovern
    class MainClass
11 8 Alan McGovern
    {
12 8 Alan McGovern
        ClientEngine engine;
13 8 Alan McGovern
        string savePath;
14 1
15 8 Alan McGovern
        // savePath is the directory where downloads will be stored
16 8 Alan McGovern
        public MainClass(string savePath)
17 8 Alan McGovern
        {
18 8 Alan McGovern
            // Create a basic ClientEngine without changing any settings
19 8 Alan McGovern
            this.engine = new ClientEngine(new EngineSettings());
20 8 Alan McGovern
            this.savePath = savePath;
21 8 Alan McGovern
        }
22 1
23 8 Alan McGovern
        public void DownloadTorrent(string path)
24 8 Alan McGovern
        {
25 8 Alan McGovern
            // Open the .torrent file
26 8 Alan McGovern
            Torrent torrent = Torrent.Load(path);
27 1
28 8 Alan McGovern
            // Create the manager which will download the torrent to savePath
29 8 Alan McGovern
            // using the default settings.
30 8 Alan McGovern
            TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());
31 1
32 8 Alan McGovern
            // Register the manager with the engine
33 8 Alan McGovern
            engine.Register(manager);
34 8 Alan McGovern
35 15 Alan McGovern
            // Begin the download. It is not necessary to call HashCheck on the manager
36 15 Alan McGovern
            // before starting the download. If a hash check has not been performed, the
37 15 Alan McGovern
            // manager will enter the Hashing state and perform a hash check before it
38 15 Alan McGovern
            // begins downloading.
39 15 Alan McGovern
40 15 Alan McGovern
            // If the torrent is fully downloaded already, calling 'Start' will place
41 15 Alan McGovern
            // the manager in the Seeding state.
42 8 Alan McGovern
            manager.Start();
43 8 Alan McGovern
        }
44 8 Alan McGovern
    }
45 8 Alan McGovern
</code></pre>
46 5 olivier dufour
47 13 Alan McGovern
h2. Example 2
48 1
49 13 Alan McGovern
<pre><code class="java">
50 13 Alan McGovern
        public void HashTorrent(TorrentManager manager)
51 13 Alan McGovern
        {
52 14 Alan McGovern
            // Note: The manager must be in the 'Stopped' state in order to perform
53 14 Alan McGovern
            // a hash check. Also, to make the sample easier the event handler
54 14 Alan McGovern
            // is not unregistered. In your application be careful you don't
55 14 Alan McGovern
            // accidentally attach a new event handler every time the torrent is hashed
56 13 Alan McGovern
            manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) {
57 13 Alan McGovern
                int pieceIndex = e.PieceIndex;
58 13 Alan McGovern
                int totalPieces = e.TorrentManager.Torrent.Pieces.Count;
59 13 Alan McGovern
                double progress = (double) pieceIndex / totalPieces * 100.0;
60 13 Alan McGovern
                if (e.HashPassed)
61 13 Alan McGovern
                    Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces);
62 13 Alan McGovern
                else
63 13 Alan McGovern
                    Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces);
64 13 Alan McGovern
                
65 13 Alan McGovern
                // This shows how complete the hashing is.
66 13 Alan McGovern
                Console.WriteLine("Total progress is: {0}%", progress);
67 13 Alan McGovern
68 13 Alan McGovern
                // This shows the percentage completion of the download. This value
69 13 Alan McGovern
                // is updated as the torrent is hashed or new pieces are downloaded
70 13 Alan McGovern
                Console.WriteLine("{0}% of the torrent is complete");
71 13 Alan McGovern
            };
72 13 Alan McGovern
73 13 Alan McGovern
            // If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'
74 13 Alan McGovern
            // state once the hash check is finished. Otherwise it will return to the 'Stopped' state.
75 13 Alan McGovern
            manager.HashCheck(false);
76 13 Alan McGovern
        }
77 13 Alan McGovern
</code></pre>
78 1
79 1
80 16 Alan McGovern
h2. Example 3
81 1
82 16 Alan McGovern
<pre><code class="java">
83 16 Alan McGovern
        public void StopTorrent(TorrentManager manager)
84 16 Alan McGovern
        {
85 16 Alan McGovern
            // When stopping a torrent, certain cleanup tasks need to be perfomed
86 16 Alan McGovern
            // such as flushing unwritten data to disk and informing the tracker
87 16 Alan McGovern
            // the client is no longer downloading/seeding the torrent. To allow for
88 16 Alan McGovern
            // this, when Stop is called the manager enters a 'Stopping' state. Once
89 16 Alan McGovern
            // all the tasks are completed the manager will enter the 'Stopped' state.
90 1
91 16 Alan McGovern
            manager.TorrentStateChanged += delegate (object o, TorrentStateChangedEventArgs e) {
92 16 Alan McGovern
                if (e.NewState == TorrentState.Stopping)
93 16 Alan McGovern
                {
94 16 Alan McGovern
                    Console.WriteLine("Torrent {0} has begun stopping", e.TorrentManager.Torrent.Name);
95 16 Alan McGovern
                }
96 16 Alan McGovern
                else if (e.NewState == TorrentState.Stopped)
97 16 Alan McGovern
                {
98 16 Alan McGovern
                    // It is now safe to unregister the torrent from the engine and dispose of it (if required)
99 16 Alan McGovern
                    engine.Unregister(manager);
100 16 Alan McGovern
                    manager.Dispose();
101 1
102 16 Alan McGovern
                    Console.WriteLine("Torrent {0} has stopped", e.TorrentManager.Torrent.Name);
103 16 Alan McGovern
                }
104 16 Alan McGovern
            };
105 16 Alan McGovern
        }
106 16 Alan McGovern
</code></pre>