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