Managing Torrents
Version 18 (Alan McGovern, 07/30/2009 01:18 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 | 17 | Alan McGovern | [[Managing Torrents#Example-3|Example 3]] shows how to stop a torrent and remove it from the engine |
| 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 | 18 | Alan McGovern | // Hook into the TorrentStateChanged event so we can tell when the torrent |
| 92 | 18 | Alan McGovern | // finishes cleanup |
| 93 | 16 | Alan McGovern | manager.TorrentStateChanged += delegate (object o, TorrentStateChangedEventArgs e) { |
| 94 | 16 | Alan McGovern | if (e.NewState == TorrentState.Stopping) |
| 95 | 16 | Alan McGovern | { |
| 96 | 16 | Alan McGovern | Console.WriteLine("Torrent {0} has begun stopping", e.TorrentManager.Torrent.Name); |
| 97 | 16 | Alan McGovern | } |
| 98 | 16 | Alan McGovern | else if (e.NewState == TorrentState.Stopped) |
| 99 | 16 | Alan McGovern | { |
| 100 | 16 | Alan McGovern | // It is now safe to unregister the torrent from the engine and dispose of it (if required) |
| 101 | 16 | Alan McGovern | engine.Unregister(manager); |
| 102 | 16 | Alan McGovern | manager.Dispose(); |
| 103 | 1 | ||
| 104 | 16 | Alan McGovern | Console.WriteLine("Torrent {0} has stopped", e.TorrentManager.Torrent.Name); |
| 105 | 16 | Alan McGovern | } |
| 106 | 16 | Alan McGovern | }; |
| 107 | 18 | Alan McGovern | |
| 108 | 18 | Alan McGovern | // Begin the process to stop the torrent |
| 109 | 18 | Alan McGovern | manager.Stop (); |
| 110 | 16 | Alan McGovern | } |
| 111 | 16 | Alan McGovern | </code></pre> |