Creating Torrents

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