Creating Torrents

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