Managing Torrents
Version 8 (Alan McGovern, 07/30/2009 12:52 AM)
1 | 7 | Alan McGovern | h1. Downloading Torrents |
---|---|---|---|
2 | 1 | ||
3 | 5 | olivier dufour | h2. Simple Client |
4 | 5 | olivier dufour | |
5 | 4 | olivier dufour | <pre><code class="java"> |
6 | 8 | Alan McGovern | class MainClass |
7 | 8 | Alan McGovern | { |
8 | 8 | Alan McGovern | ClientEngine engine; |
9 | 8 | Alan McGovern | string savePath; |
10 | 1 | ||
11 | 8 | Alan McGovern | // savePath is the directory where downloads will be stored |
12 | 8 | Alan McGovern | public MainClass(string savePath) |
13 | 8 | Alan McGovern | { |
14 | 8 | Alan McGovern | // Create a basic ClientEngine without changing any settings |
15 | 8 | Alan McGovern | this.engine = new ClientEngine(new EngineSettings()); |
16 | 8 | Alan McGovern | this.savePath = savePath; |
17 | 8 | Alan McGovern | } |
18 | 1 | ||
19 | 8 | Alan McGovern | public void DownloadTorrent(string path) |
20 | 8 | Alan McGovern | { |
21 | 8 | Alan McGovern | // Open the .torrent file |
22 | 8 | Alan McGovern | Torrent torrent = Torrent.Load(path); |
23 | 1 | ||
24 | 8 | Alan McGovern | // Create the manager which will download the torrent to savePath |
25 | 8 | Alan McGovern | // using the default settings. |
26 | 8 | Alan McGovern | TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings()); |
27 | 1 | ||
28 | 8 | Alan McGovern | // Register the manager with the engine |
29 | 8 | Alan McGovern | engine.Register(manager); |
30 | 8 | Alan McGovern | |
31 | 8 | Alan McGovern | // Begin the download |
32 | 8 | Alan McGovern | manager.Start(); |
33 | 8 | Alan McGovern | } |
34 | 8 | Alan McGovern | } |
35 | 8 | Alan McGovern | </code></pre> |
36 | 5 | olivier dufour | |
37 | 5 | olivier dufour | h2. Advanced Client |
38 | 5 | olivier dufour | |
39 | 5 | olivier dufour | |
40 | 1 | <pre><code class="java"> |
|
41 | 1 | ||
42 | 1 | ||
43 | 1 | ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port)); |
|
44 | 1 | ||
45 | 1 | //DHT |
|
46 | 1 | DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port)); |
|
47 | 5 | olivier dufour | DhtEngine dht = new DhtEngine (dhtListner); |
48 | 1 | engine.RegisterDht(dht); |
|
49 | 1 | dhtListner.Start(); |
|
50 | 1 | //byte array of dht nodes can be null if you have never connect to DHT before today! |
|
51 | 1 | engine.DhtEngine.Start(nodes); |
|
52 | 1 | ||
53 | 1 | ||
54 | 1 | torrent = Torrent.Load("test.torrent"); |
|
55 | 5 | olivier dufour | TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults); |
56 | 5 | olivier dufour | |
57 | 5 | olivier dufour | //FastResume code need a BencodedDictionnary |
58 | 1 | //If you jsut start the torrent, you have no fast resume |
|
59 | 1 | // but if you have download a part of the torrent you can save fast resume data |
|
60 | 1 | //(manager.SaveFastResume()) and restore it later to do a quicker load |
|
61 | 1 | manager.LoadFastResume(new FastResume (BEncDictFastResume)); |
|
62 | 1 | ||
63 | 1 | engine.Register(manager); |
|
64 | 2 | olivier dufour | manager.Start (); |
65 | 1 | ||
66 | 1 | </code> |
|
67 | 1 | </pre> |