Managing Torrents
Version 12 (Alan McGovern, 07/30/2009 01:03 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 | 5 | olivier dufour | h2. Advanced Client |
41 | 5 | olivier dufour | |
42 | 5 | olivier dufour | |
43 | 1 | <pre><code class="java"> |
|
44 | 1 | ||
45 | 1 | ||
46 | 1 | ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port)); |
|
47 | 1 | ||
48 | 1 | //DHT |
|
49 | 1 | DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port)); |
|
50 | 5 | olivier dufour | DhtEngine dht = new DhtEngine (dhtListner); |
51 | 1 | engine.RegisterDht(dht); |
|
52 | 1 | dhtListner.Start(); |
|
53 | 1 | //byte array of dht nodes can be null if you have never connect to DHT before today! |
|
54 | 1 | engine.DhtEngine.Start(nodes); |
|
55 | 1 | ||
56 | 1 | ||
57 | 1 | torrent = Torrent.Load("test.torrent"); |
|
58 | 5 | olivier dufour | TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults); |
59 | 5 | olivier dufour | |
60 | 5 | olivier dufour | //FastResume code need a BencodedDictionnary |
61 | 1 | //If you jsut start the torrent, you have no fast resume |
|
62 | 1 | // but if you have download a part of the torrent you can save fast resume data |
|
63 | 1 | //(manager.SaveFastResume()) and restore it later to do a quicker load |
|
64 | 1 | manager.LoadFastResume(new FastResume (BEncDictFastResume)); |
|
65 | 1 | ||
66 | 1 | engine.Register(manager); |
|
67 | 2 | olivier dufour | manager.Start (); |
68 | 1 | ||
69 | 1 | </code> |
|
70 | 1 | </pre> |