Managing Torrents

Version 10 (Alan McGovern, 07/30/2009 12:54 AM)

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