formatting fixes in client test, and made the test build when resolve countries is...
[libtorrent-kjk.git] / docs / examples.html
blobf7bfa1a421a15d1dab31b4b48c3d2fdd176aa291
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
7 <title>libtorrent Examples</title>
8 <meta name="author" content="Arvid Norberg, arvid&#64;rasterbar.com" />
9 <link rel="stylesheet" href="style.css" type="text/css" />
10 </head>
11 <body>
12 <div class="document" id="libtorrent-examples">
13 <h1 class="title">libtorrent Examples</h1>
14 <table class="docinfo" frame="void" rules="none">
15 <col class="docinfo-name" />
16 <col class="docinfo-content" />
17 <tbody valign="top">
18 <tr><th class="docinfo-name">Author:</th>
19 <td>Arvid Norberg, <a class="last reference" href="mailto:arvid&#64;rasterbar.com">arvid&#64;rasterbar.com</a></td></tr>
20 </tbody>
21 </table>
22 <div class="contents topic" id="table-of-contents">
23 <p class="topic-title first"><a name="table-of-contents">Table of contents</a></p>
24 <ul class="simple">
25 <li><a class="reference" href="#examples" id="id2" name="id2">examples</a><ul>
26 <li><a class="reference" href="#dump-torrent" id="id3" name="id3">dump_torrent</a></li>
27 <li><a class="reference" href="#simple-client" id="id4" name="id4">simple client</a></li>
28 <li><a class="reference" href="#make-torrent" id="id5" name="id5">make_torrent</a></li>
29 </ul>
30 </li>
31 </ul>
32 </div>
33 <div class="section">
34 <h1><a id="examples" name="examples">examples</a></h1>
35 <p>Except for the example programs in this manual, there's also a bigger example
36 of a (little bit) more complete client, <tt class="docutils literal"><span class="pre">client_test</span></tt>. There are separate
37 instructions for how to use it <a class="reference" href="client_test.html">here</a> if you'd like to try it. Note that building
38 <tt class="docutils literal"><span class="pre">client_test</span></tt> also requires boost.regex and boost.program_options library.</p>
39 <div class="section">
40 <h2><a id="dump-torrent" name="dump-torrent">dump_torrent</a></h2>
41 <p>This is an example of a program that will take a torrent-file as a parameter and
42 print information about it to std out:</p>
43 <pre class="literal-block">
44 #include &lt;iostream&gt;
45 #include &lt;fstream&gt;
46 #include &lt;iterator&gt;
47 #include &lt;iomanip&gt;
49 #include &quot;libtorrent/entry.hpp&quot;
50 #include &quot;libtorrent/bencode.hpp&quot;
51 #include &quot;libtorrent/torrent_info.hpp&quot;
54 int main(int argc, char* argv[])
56 using namespace libtorrent;
58 if (argc != 2)
60 std::cerr &lt;&lt; &quot;usage: dump_torrent torrent-file\n&quot;;
61 return 1;
64 try
66 std::ifstream in(argv[1], std::ios_base::binary);
67 in.unsetf(std::ios_base::skipws);
68 entry e = bdecode(std::istream_iterator&lt;char&gt;(in)
69 , std::istream_iterator&lt;char&gt;());
71 std::cout &lt;&lt; &quot;\n\n----- raw info -----\n\n&quot;;
72 e.print(std::cout);
74 torrent_info t(e);
76 // print info about torrent
77 std::cout &lt;&lt; &quot;\n\n----- torrent file info -----\n\n&quot;;
78 std::cout &lt;&lt; &quot;nodes:\n&quot;;
79 typedef std::vector&lt;std::pair&lt;std::string, int&gt; &gt; node_vec;
80 node_vec const&amp; nodes = t.nodes();
81 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
82 i != end; ++i)
84 std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot;:&quot; &lt;&lt; i-&gt;second &lt;&lt; &quot;\n&quot;;
87 std::cout &lt;&lt; &quot;trackers:\n&quot;;
88 for (std::vector&lt;announce_entry&gt;::const_iterator i
89 = t.trackers().begin(); i != t.trackers().end(); ++i)
91 std::cout &lt;&lt; i-&gt;tier &lt;&lt; &quot;: &quot; &lt;&lt; i-&gt;url &lt;&lt; &quot;\n&quot;;
94 std::cout &lt;&lt; &quot;number of pieces: &quot; &lt;&lt; t.num_pieces() &lt;&lt; &quot;\n&quot;;
95 std::cout &lt;&lt; &quot;piece length: &quot; &lt;&lt; t.piece_length() &lt;&lt; &quot;\n&quot;;
96 std::cout &lt;&lt; &quot;info hash: &quot; &lt;&lt; t.info_hash() &lt;&lt; &quot;\n&quot;;
97 std::cout &lt;&lt; &quot;comment: &quot; &lt;&lt; t.comment() &lt;&lt; &quot;\n&quot;;
98 std::cout &lt;&lt; &quot;created by: &quot; &lt;&lt; t.creator() &lt;&lt; &quot;\n&quot;;
99 std::cout &lt;&lt; &quot;files:\n&quot;;
100 for (torrent_info::file_iterator i = t.begin_files();
101 i != t.end_files(); ++i)
103 std::cout &lt;&lt; &quot; &quot; &lt;&lt; std::setw(11) &lt;&lt; i-&gt;size
104 &lt;&lt; &quot; &quot; &lt;&lt; i-&gt;path.string() &lt;&lt; &quot;\n&quot;;
108 catch (std::exception&amp; e)
110 std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
113 return 0;
115 </pre>
116 </div>
117 <div class="section">
118 <h2><a id="simple-client" name="simple-client">simple client</a></h2>
119 <p>This is a simple client. It doesn't have much output to keep it simple:</p>
120 <pre class="literal-block">
121 #include &lt;iostream&gt;
122 #include &lt;fstream&gt;
123 #include &lt;iterator&gt;
124 #include &lt;exception&gt;
126 #include &lt;boost/format.hpp&gt;
127 #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
129 #include &quot;libtorrent/entry.hpp&quot;
130 #include &quot;libtorrent/bencode.hpp&quot;
131 #include &quot;libtorrent/session.hpp&quot;
133 int main(int argc, char* argv[])
135 using namespace libtorrent;
137 if (argc != 2)
139 std::cerr &lt;&lt; &quot;usage: ./simple_cient torrent-file\n&quot;
140 &quot;to stop the client, press return.\n&quot;;
141 return 1;
146 session s;
147 s.listen_on(std::make_pair(6881, 6889));
149 std::ifstream in(argv[1], std::ios_base::binary);
150 in.unsetf(std::ios_base::skipws);
151 entry e = bdecode(std::istream_iterator&lt;char&gt;(in)
152 , std::istream_iterator&lt;char&gt;());
153 s.add_torrent(torrent_info(e), &quot;&quot;);
155 // wait for the user to end
156 char a;
157 std::cin.unsetf(std::ios_base::skipws);
158 std::cin &gt;&gt; a;
160 catch (std::exception&amp; e)
162 std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
164 return 0;
166 </pre>
167 </div>
168 <div class="section">
169 <h2><a id="make-torrent" name="make-torrent">make_torrent</a></h2>
170 <p>Shows how to create a torrent from a directory tree:</p>
171 <pre class="literal-block">
172 #include &lt;iostream&gt;
173 #include &lt;fstream&gt;
174 #include &lt;iterator&gt;
175 #include &lt;iomanip&gt;
177 #include &quot;libtorrent/entry.hpp&quot;
178 #include &quot;libtorrent/bencode.hpp&quot;
179 #include &quot;libtorrent/torrent_info.hpp&quot;
180 #include &quot;libtorrent/file.hpp&quot;
181 #include &quot;libtorrent/storage.hpp&quot;
182 #include &quot;libtorrent/hasher.hpp&quot;
183 #include &quot;libtorrent/file_pool.hpp&quot;
185 #include &lt;boost/filesystem/operations.hpp&gt;
186 #include &lt;boost/filesystem/path.hpp&gt;
187 #include &lt;boost/filesystem/fstream.hpp&gt;
189 using namespace boost::filesystem;
190 using namespace libtorrent;
192 void add_files(
193 torrent_info&amp; t
194 , path const&amp; p
195 , path const&amp; l)
197 path f(p / l);
198 if (is_directory(f))
200 for (directory_iterator i(f), end; i != end; ++i)
201 add_files(t, p, l / i-&gt;leaf());
203 else
205 std::cerr &lt;&lt; &quot;adding \&quot;&quot; &lt;&lt; l.string() &lt;&lt; &quot;\&quot;\n&quot;;
206 t.add_file(l, file_size(f));
210 int main(int argc, char* argv[])
212 using namespace libtorrent;
213 using namespace boost::filesystem;
215 path::default_name_check(no_check);
217 if (argc != 4)
219 std::cerr &lt;&lt; &quot;usage: make_torrent &lt;output torrent-file&gt; &quot;
220 &quot;&lt;announce url&gt; &lt;file or directory to create torrent from&gt;\n&quot;;
221 return 1;
226 torrent_info t;
227 path full_path = complete(path(argv[3]));
228 ofstream out(complete(path(argv[1])), std::ios_base::binary);
230 int piece_size = 256 * 1024;
231 char const* creator_str = &quot;libtorrent&quot;;
233 add_files(t, full_path.branch_path(), full_path.leaf());
234 t.set_piece_size(piece_size);
236 file_pool fp;
237 storage st(t, full_path.branch_path(), fp);
238 t.add_tracker(argv[2]);
240 // calculate the hash for all pieces
241 int num = t.num_pieces();
242 std::vector&lt;char&gt; buf(piece_size);
243 for (int i = 0; i &lt; num; ++i)
245 st.read(&amp;buf[0], i, 0, t.piece_size(i));
246 hasher h(&amp;buf[0], t.piece_size(i));
247 t.set_hash(i, h.final());
248 std::cerr &lt;&lt; (i+1) &lt;&lt; &quot;/&quot; &lt;&lt; num &lt;&lt; &quot;\r&quot;;
251 t.set_creator(creator_str);
253 // create the torrent and print it to out
254 entry e = t.create_torrent();
255 libtorrent::bencode(std::ostream_iterator&lt;char&gt;(out), e);
257 catch (std::exception&amp; e)
259 std::cerr &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
262 return 0;
264 </pre>
265 </div>
266 </div>
267 </div>
268 </body>
269 </html>