Creating Torrents

Version 13 (Alan McGovern, 10/25/2010 11:14 PM)

1 2 Alan McGovern
h1. Creating Torrents
2 2 Alan McGovern
3 8 Alan McGovern
[[Creating Torrents#Example-1|Example 1]] shows how to create a simple torrent.
4 9 Alan McGovern
[[Creating Torrents#Example-2|Example 2]] shows how to create a torrent asynchronously so that it does not block the main thread in your application.
5 9 Alan McGovern
[[Creating Torrents#Example-3|Example 3]] shows how to abort the creation of a torrent after it has started
6 11 Alan McGovern
[[Creating Torrents#Example-4|Example 4]] shows how to create a torrent and load it without having to hash the data a second time
7 5 Alan McGovern
8 7 Alan McGovern
h2. Example 1
9 7 Alan McGovern
10 2 Alan McGovern
<pre><code class="java">
11 12 Alan McGovern
// 'path' is the location of the file/folder which is going to be converted
12 12 Alan McGovern
// to a torrent. 'savePath' is where the .torrent file will be saved.
13 12 Alan McGovern
void CreateTorrent(string path, string savePath) {
14 13 Alan McGovern
    // The class used for creating the torrent
15 13 Alan McGovern
    TorrentCreator c = new TorrentCreator();
16 2 Alan McGovern
17 13 Alan McGovern
    // Add one tier which contains two trackers
18 13 Alan McGovern
    RawTrackerTier tier = new RawTrackerTier ();
19 13 Alan McGovern
    tier.Add("http://localhost/announce");
20 1
21 13 Alan McGovern
    c.Announces.Add(tier);
22 13 Alan McGovern
    c.Comment = "This is the comment";
23 13 Alan McGovern
    c.CreatedBy = "Doug using " + VersionInfo.ClientVersion;
24 13 Alan McGovern
    c.Publisher = "www.aaronsen.com";
25 12 Alan McGovern
26 13 Alan McGovern
    // Set the torrent as private so it will not use DHT or peer exchange
27 13 Alan McGovern
    // Generally you will not want to set this.
28 13 Alan McGovern
    c.Private = true;
29 2 Alan McGovern
30 13 Alan McGovern
    // Every time a piece has been hashed, this event will fire. It is an
31 13 Alan McGovern
    // asynchronous event, so you have to handle threading yourself.
32 13 Alan McGovern
    c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {
33 13 Alan McGovern
        Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);
34 13 Alan McGovern
        Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);
35 13 Alan McGovern
        Console.WriteLine("Total data to hash: {0}", e.OverallSize);
36 13 Alan McGovern
    };
37 12 Alan McGovern
38 13 Alan McGovern
    // ITorrentFileSource can be implemented to provide the TorrentCreator
39 13 Alan McGovern
    // with a list of files which will be added to the torrent metadata.
40 13 Alan McGovern
    // The default implementation takes a path to a single file or a path
41 13 Alan McGovern
    // to a directory. If the path is a directory, all files will be
42 13 Alan McGovern
    // recursively added
43 13 Alan McGovern
    ITorrentFileSource fileSource = new TorrentFileSource (path);
44 12 Alan McGovern
45 13 Alan McGovern
    // Create the torrent file and save it directly to the specified path
46 13 Alan McGovern
    // Different overloads of 'Create' can be used to save the data to a Stream
47 13 Alan McGovern
    // or just return it as a BEncodedDictionary (its native format) so it can be
48 13 Alan McGovern
    // processed in memory
49 13 Alan McGovern
    c.Create(fileSource, savePath);
50 13 Alan McGovern
}
51 1
</code></pre>
52 1
53 7 Alan McGovern
h2. Example 2
54 7 Alan McGovern
55 5 Alan McGovern
<pre><code class="java">
56 5 Alan McGovern
        // 'path' is the location of the file/folder which is going to be converted
57 5 Alan McGovern
        // to a torrent. 'savePath' is where the .torrent file will be saved.
58 5 Alan McGovern
        void CreateTorrent(string path, string savePath)
59 5 Alan McGovern
        {
60 5 Alan McGovern
            // The class used for creating the torrent
61 5 Alan McGovern
            TorrentCreator c = new TorrentCreator();
62 5 Alan McGovern
63 5 Alan McGovern
            // Fill in the path, trackers as in Example 1
64 5 Alan McGovern
            FillInStandardInformation(c);
65 5 Alan McGovern
66 5 Alan McGovern
            // Create the torrent asynchronously
67 5 Alan McGovern
            c.BeginCreate(c, TorrentCreated);
68 5 Alan McGovern
        }
69 5 Alan McGovern
70 5 Alan McGovern
        void TorrentCreated(IAsyncResult result)
71 5 Alan McGovern
        {
72 5 Alan McGovern
            TorrentCreator c = (TorrentCreator)result.AsyncState;
73 5 Alan McGovern
74 5 Alan McGovern
            // If any errors occured while creating the torrent, they
75 5 Alan McGovern
            // will be rethrown here.
76 5 Alan McGovern
            try
77 5 Alan McGovern
            {   
78 5 Alan McGovern
                // Open the destination file and use the EndCreate overload which
79 5 Alan McGovern
                // writes the data directly to the Stream. 
80 5 Alan McGovern
                using (FileStream stream = File.OpenWrite(savePath))
81 5 Alan McGovern
                    c.EndCreate(result, stream);
82 5 Alan McGovern
            }
83 5 Alan McGovern
            catch (Exception ex)
84 5 Alan McGovern
            {
85 1
                Console.WriteLine("Error creating torrent: {0}", ex);
86 1
            }
87 9 Alan McGovern
        }
88 9 Alan McGovern
</code></pre>
89 9 Alan McGovern
90 9 Alan McGovern
91 9 Alan McGovern
h2. Example 3
92 9 Alan McGovern
93 9 Alan McGovern
<pre><code class="java">
94 9 Alan McGovern
        // 'path' is the location of the file/folder which is going to be converted
95 9 Alan McGovern
        // to a torrent. 'savePath' is where the .torrent file will be saved.
96 9 Alan McGovern
        void CreateTorrent(string path, string savePath)
97 9 Alan McGovern
        {
98 9 Alan McGovern
            // The class used for creating the torrent
99 9 Alan McGovern
            TorrentCreator c = new TorrentCreator();
100 9 Alan McGovern
101 9 Alan McGovern
            // Fill in the path, trackers as in Example 1
102 9 Alan McGovern
            FillInStandardInformation(c);
103 9 Alan McGovern
104 9 Alan McGovern
            // Create the torrent asynchronously and keep a reference to the
105 9 Alan McGovern
            // IAsyncResult it returns
106 9 Alan McGovern
            TorrentCreatorAsyncResult result = c.BeginCreate(c, TorrentCreated);
107 9 Alan McGovern
108 9 Alan McGovern
            // Abort creation of the torrent (can be called at any stage). Note, you
109 9 Alan McGovern
            // can only abort the creation if the torrent is created asynchronously
110 9 Alan McGovern
            result.Abort();
111 5 Alan McGovern
        }
112 11 Alan McGovern
</code></pre>
113 11 Alan McGovern
114 11 Alan McGovern
115 11 Alan McGovern
h2. Example 4
116 11 Alan McGovern
117 11 Alan McGovern
<pre><code class="java">
118 11 Alan McGovern
public static void CreateAndLoadTorrent (ClientEngine engine)
119 11 Alan McGovern
{
120 11 Alan McGovern
    // Instantiate a torrent creator
121 11 Alan McGovern
    TorrentCreator creator = new TorrentCreator ();
122 11 Alan McGovern
123 11 Alan McGovern
    // Create a TorrentFileSource which is used to populate the .torrent
124 11 Alan McGovern
    ITorrentFileSource files = new TorrentFileSource ("somefile.dat");
125 11 Alan McGovern
126 11 Alan McGovern
    // Create the Torrent metadata blob
127 11 Alan McGovern
    BEncodedDictionary torrentDict = creator.Create (files);
128 11 Alan McGovern
129 11 Alan McGovern
    // Instantiate a torrent
130 11 Alan McGovern
    Torrent torrent = Torrent.Load (torrentDict);
131 11 Alan McGovern
132 11 Alan McGovern
    // Create a fake fast resume data with all the pieces set to true so we
133 11 Alan McGovern
    // don't have to hash the torrent when adding it directly to the engine.
134 11 Alan McGovern
    BitField bitfield = new BitField (torrent.Pieces.Count).Not ();
135 11 Alan McGovern
    FastResume fastResumeData = new FastResume(torrent.InfoHash, bitfield);
136 11 Alan McGovern
137 11 Alan McGovern
    // Create a TorrentManager so the torrent can be downloaded and load
138 11 Alan McGovern
    // the FastResume data so we don't have to hash it again.
139 11 Alan McGovern
    TorrentManager manager = new TorrentManager (torrent, "SavePath", new TorrentSettings ());
140 11 Alan McGovern
    manager.LoadFastResume (fastResumeData);
141 11 Alan McGovern
142 11 Alan McGovern
    // Register it with the engine and start the download
143 11 Alan McGovern
    engine.Register (manager);
144 11 Alan McGovern
    engine.StartAll ();
145 11 Alan McGovern
}
146 5 Alan McGovern
</code></pre>