Managing Torrents
Version 13 (Alan McGovern, 07/30/2009 01:04 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 | 8 | Alan McGovern | // Begin the download  | 
| 35 | 8 | Alan McGovern | manager.Start();  | 
| 36 | 8 | Alan McGovern | }  | 
| 37 | 8 | Alan McGovern | }  | 
| 38 | 8 | Alan McGovern | </code></pre>  | 
| 39 | 5 | olivier dufour | |
| 40 | 13 | Alan McGovern | h2. Example 2  | 
| 41 | 1 | ||
| 42 | 13 | Alan McGovern | <pre><code class="java">  | 
| 43 | 13 | Alan McGovern | public void HashTorrent(TorrentManager manager)  | 
| 44 | 13 | Alan McGovern |         { | 
| 45 | 13 | Alan McGovern | // Note: The manager must be in the 'Stopped' state  | 
| 46 | 13 | Alan McGovern | //in order to perform a hash check.  | 
| 47 | 13 | Alan McGovern | |
| 48 | 13 | Alan McGovern |             manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) { | 
| 49 | 13 | Alan McGovern | int pieceIndex = e.PieceIndex;  | 
| 50 | 13 | Alan McGovern | int totalPieces = e.TorrentManager.Torrent.Pieces.Count;  | 
| 51 | 13 | Alan McGovern | double progress = (double) pieceIndex / totalPieces * 100.0;  | 
| 52 | 13 | Alan McGovern | if (e.HashPassed)  | 
| 53 | 13 | Alan McGovern |                     Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces); | 
| 54 | 13 | Alan McGovern | else  | 
| 55 | 13 | Alan McGovern |                     Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces); | 
| 56 | 13 | Alan McGovern | |
| 57 | 13 | Alan McGovern | // This shows how complete the hashing is.  | 
| 58 | 13 | Alan McGovern |                 Console.WriteLine("Total progress is: {0}%", progress); | 
| 59 | 13 | Alan McGovern | |
| 60 | 13 | Alan McGovern | // This shows the percentage completion of the download. This value  | 
| 61 | 13 | Alan McGovern | // is updated as the torrent is hashed or new pieces are downloaded  | 
| 62 | 13 | Alan McGovern |                 Console.WriteLine("{0}% of the torrent is complete"); | 
| 63 | 13 | Alan McGovern | };  | 
| 64 | 13 | Alan McGovern | |
| 65 | 13 | Alan McGovern | // If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'  | 
| 66 | 13 | Alan McGovern | // state once the hash check is finished. Otherwise it will return to the 'Stopped' state.  | 
| 67 | 13 | Alan McGovern | manager.HashCheck(false);  | 
| 68 | 13 | Alan McGovern | }  | 
| 69 | 13 | Alan McGovern | </code></pre>  | 
| 70 | 5 | olivier dufour | |
| 71 | 1 | <pre><code class="java">  | 
|
| 72 | 1 | ||
| 73 | 1 | ||
| 74 | 1 | ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port));  | 
|
| 75 | 1 | ||
| 76 | 1 | //DHT  | 
|
| 77 | 1 | DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port));  | 
|
| 78 | 5 | olivier dufour | DhtEngine dht = new DhtEngine (dhtListner);  | 
| 79 | 1 | engine.RegisterDht(dht);  | 
|
| 80 | 1 | dhtListner.Start();  | 
|
| 81 | 1 | //byte array of dht nodes can be null if you have never connect to DHT before today!  | 
|
| 82 | 1 | engine.DhtEngine.Start(nodes);  | 
|
| 83 | 1 | ||
| 84 | 1 | ||
| 85 | 1 | torrent = Torrent.Load("test.torrent"); | 
|
| 86 | 5 | olivier dufour | TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);  | 
| 87 | 5 | olivier dufour | |
| 88 | 5 | olivier dufour | //FastResume code need a BencodedDictionnary  | 
| 89 | 1 | //If you jsut start the torrent, you have no fast resume  | 
|
| 90 | 1 | // but if you have download a part of the torrent you can save fast resume data  | 
|
| 91 | 1 | //(manager.SaveFastResume()) and restore it later to do a quicker load  | 
|
| 92 | 1 | manager.LoadFastResume(new FastResume (BEncDictFastResume));  | 
|
| 93 | 1 | ||
| 94 | 1 | engine.Register(manager);  | 
|
| 95 | 2 | olivier dufour | manager.Start ();  | 
| 96 | 1 | ||
| 97 | 1 | </code>  | 
|
| 98 | 1 | </pre>  |