Managing Torrents

Version 15 (Alan McGovern, 07/30/2009 01:08 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 9 Alan McGovern
6 9 Alan McGovern
h2. Example 1
7 5 olivier dufour
8 4 olivier dufour
<pre><code class="java">
9 8 Alan McGovern
    class MainClass
10 8 Alan McGovern
    {
11 8 Alan McGovern
        ClientEngine engine;
12 8 Alan McGovern
        string savePath;
13 1
14 8 Alan McGovern
        // savePath is the directory where downloads will be stored
15 8 Alan McGovern
        public MainClass(string savePath)
16 8 Alan McGovern
        {
17 8 Alan McGovern
            // Create a basic ClientEngine without changing any settings
18 8 Alan McGovern
            this.engine = new ClientEngine(new EngineSettings());
19 8 Alan McGovern
            this.savePath = savePath;
20 8 Alan McGovern
        }
21 1
22 8 Alan McGovern
        public void DownloadTorrent(string path)
23 8 Alan McGovern
        {
24 8 Alan McGovern
            // Open the .torrent file
25 8 Alan McGovern
            Torrent torrent = Torrent.Load(path);
26 1
27 8 Alan McGovern
            // Create the manager which will download the torrent to savePath
28 8 Alan McGovern
            // using the default settings.
29 8 Alan McGovern
            TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());
30 1
31 8 Alan McGovern
            // Register the manager with the engine
32 8 Alan McGovern
            engine.Register(manager);
33 8 Alan McGovern
34 15 Alan McGovern
            // Begin the download. It is not necessary to call HashCheck on the manager
35 15 Alan McGovern
            // before starting the download. If a hash check has not been performed, the
36 15 Alan McGovern
            // manager will enter the Hashing state and perform a hash check before it
37 15 Alan McGovern
            // begins downloading.
38 15 Alan McGovern
39 15 Alan McGovern
            // If the torrent is fully downloaded already, calling 'Start' will place
40 15 Alan McGovern
            // the manager in the Seeding state.
41 8 Alan McGovern
            manager.Start();
42 8 Alan McGovern
        }
43 8 Alan McGovern
    }
44 8 Alan McGovern
</code></pre>
45 5 olivier dufour
46 13 Alan McGovern
h2. Example 2
47 1
48 13 Alan McGovern
<pre><code class="java">
49 13 Alan McGovern
        public void HashTorrent(TorrentManager manager)
50 13 Alan McGovern
        {
51 14 Alan McGovern
            // Note: The manager must be in the 'Stopped' state in order to perform
52 14 Alan McGovern
            // a hash check. Also, to make the sample easier the event handler
53 14 Alan McGovern
            // is not unregistered. In your application be careful you don't
54 14 Alan McGovern
            // accidentally attach a new event handler every time the torrent is hashed
55 13 Alan McGovern
            manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) {
56 13 Alan McGovern
                int pieceIndex = e.PieceIndex;
57 13 Alan McGovern
                int totalPieces = e.TorrentManager.Torrent.Pieces.Count;
58 13 Alan McGovern
                double progress = (double) pieceIndex / totalPieces * 100.0;
59 13 Alan McGovern
                if (e.HashPassed)
60 13 Alan McGovern
                    Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces);
61 13 Alan McGovern
                else
62 13 Alan McGovern
                    Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces);
63 13 Alan McGovern
                
64 13 Alan McGovern
                // This shows how complete the hashing is.
65 13 Alan McGovern
                Console.WriteLine("Total progress is: {0}%", progress);
66 13 Alan McGovern
67 13 Alan McGovern
                // This shows the percentage completion of the download. This value
68 13 Alan McGovern
                // is updated as the torrent is hashed or new pieces are downloaded
69 13 Alan McGovern
                Console.WriteLine("{0}% of the torrent is complete");
70 13 Alan McGovern
            };
71 13 Alan McGovern
72 13 Alan McGovern
            // If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'
73 13 Alan McGovern
            // state once the hash check is finished. Otherwise it will return to the 'Stopped' state.
74 13 Alan McGovern
            manager.HashCheck(false);
75 13 Alan McGovern
        }
76 13 Alan McGovern
</code></pre>
77 5 olivier dufour
78 1
<pre><code class="java">
79 1
80 1
81 1
ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port));
82 1
83 1
//DHT
84 1
DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port));
85 5 olivier dufour
DhtEngine dht = new DhtEngine (dhtListner);
86 1
engine.RegisterDht(dht);
87 1
dhtListner.Start();
88 1
//byte array of dht nodes can be null if you have never connect to DHT before today!
89 1
engine.DhtEngine.Start(nodes);
90 1
91 1
92 1
torrent = Torrent.Load("test.torrent");
93 5 olivier dufour
TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);
94 5 olivier dufour
95 5 olivier dufour
//FastResume code need a BencodedDictionnary
96 1
//If you jsut start the torrent, you have no fast resume
97 1
// but if you have download a part of the torrent you can save fast resume data 
98 1
//(manager.SaveFastResume()) and restore it later to do a quicker load
99 1
manager.LoadFastResume(new FastResume (BEncDictFastResume));
100 1
101 1
engine.Register(manager);
102 2 olivier dufour
manager.Start ();
103 1
104 1
</code>
105 1
</pre>