~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to .pc/debian-changes-0.14.10-2/docs/examples.html

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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" type="text/css" href="../../css/base.css" />
10
 
<link rel="stylesheet" type="text/css" href="../../css/rst.css" />
11
 
<link rel="stylesheet" href="style.css" type="text/css" />
12
 
<style type="text/css">
13
 
/* Hides from IE-mac \*/
14
 
* html pre { height: 1%; }
15
 
/* End hide from IE-mac */
16
 
</style>
17
 
</head>
18
 
<body>
19
 
<div class="document" id="libtorrent-examples">
20
 
    <div id="container">
21
 
    <div id="headerNav">
22
 
    <ul>
23
 
    <li class="first"><a href="/">Home</a></li>
24
 
    <li><a href="../../products.html">Products</a></li>
25
 
    <li><a href="../../contact.html">Contact</a></li>
26
 
    </ul>
27
 
    </div>
28
 
    <div id="header">
29
 
    <h1><span>Rasterbar Software</span></h1>
30
 
    <h2><span>Software developement and consulting</span></h2>
31
 
    </div>
32
 
    <div id="main">
33
 
<h1 class="title">libtorrent Examples</h1>
34
 
<table class="docinfo" frame="void" rules="none">
35
 
<col class="docinfo-name" />
36
 
<col class="docinfo-content" />
37
 
<tbody valign="top">
38
 
<tr><th class="docinfo-name">Author:</th>
39
 
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid&#64;rasterbar.com">arvid&#64;rasterbar.com</a></td></tr>
40
 
</tbody>
41
 
</table>
42
 
<div class="contents topic" id="table-of-contents">
43
 
<p class="topic-title first">Table of contents</p>
44
 
<ul class="simple">
45
 
<li><a class="reference internal" href="#examples" id="id2">examples</a><ul>
46
 
<li><a class="reference internal" href="#dump-torrent" id="id3">dump_torrent</a></li>
47
 
<li><a class="reference internal" href="#simple-client" id="id4">simple client</a></li>
48
 
<li><a class="reference internal" href="#make-torrent" id="id5">make_torrent</a></li>
49
 
</ul>
50
 
</li>
51
 
</ul>
52
 
</div>
53
 
<div class="section" id="examples">
54
 
<h1>examples</h1>
55
 
<p>Except for the example programs in this manual, there's also a bigger example
56
 
of a (little bit) more complete client, <tt class="docutils literal"><span class="pre">client_test</span></tt>. There are separate
57
 
instructions for how to use it <a class="reference external" href="client_test.html">here</a> if you'd like to try it. Note that building
58
 
<tt class="docutils literal"><span class="pre">client_test</span></tt> also requires boost.regex and boost.program_options library.</p>
59
 
<div class="section" id="dump-torrent">
60
 
<h2>dump_torrent</h2>
61
 
<p>This is an example of a program that will take a torrent-file as a parameter and
62
 
print information about it to std out:</p>
63
 
<pre class="literal-block">
64
 
#include &lt;iostream&gt;
65
 
#include &lt;fstream&gt;
66
 
#include &lt;iterator&gt;
67
 
#include &lt;iomanip&gt;
68
 
 
69
 
#include &quot;libtorrent/entry.hpp&quot;
70
 
#include &quot;libtorrent/bencode.hpp&quot;
71
 
#include &quot;libtorrent/torrent_info.hpp&quot;
72
 
#include &quot;libtorrent/lazy_entry.hpp&quot;
73
 
#include &lt;boost/filesystem/operations.hpp&gt;
74
 
 
75
 
 
76
 
int main(int argc, char* argv[])
77
 
{
78
 
        using namespace libtorrent;
79
 
        using namespace boost::filesystem;
80
 
 
81
 
        if (argc != 2)
82
 
        {
83
 
                std::cerr &lt;&lt; &quot;usage: dump_torrent torrent-file\n&quot;;
84
 
                return 1;
85
 
        }
86
 
#if BOOST_VERSION &lt; 103400
87
 
        boost::filesystem::path::default_name_check(boost::filesystem::no_check);
88
 
#endif
89
 
 
90
 
#ifndef BOOST_NO_EXCEPTIONS
91
 
        try
92
 
        {
93
 
#endif
94
 
 
95
 
                int size = file_size(argv[1]);
96
 
                if (size &gt; 10 * 1000000)
97
 
                {
98
 
                        std::cerr &lt;&lt; &quot;file too big (&quot; &lt;&lt; size &lt;&lt; &quot;), aborting\n&quot;;
99
 
                        return 1;
100
 
                }
101
 
                std::vector&lt;char&gt; buf(size);
102
 
                std::ifstream(argv[1], std::ios_base::binary).read(&amp;buf[0], size);
103
 
                lazy_entry e;
104
 
                int ret = lazy_bdecode(&amp;buf[0], &amp;buf[0] + buf.size(), e);
105
 
 
106
 
                if (ret != 0)
107
 
                {
108
 
                        std::cerr &lt;&lt; &quot;invalid bencoding: &quot; &lt;&lt; ret &lt;&lt; std::endl;
109
 
                        return 1;
110
 
                }
111
 
 
112
 
                std::cout &lt;&lt; &quot;\n\n----- raw info -----\n\n&quot;;
113
 
                std::cout &lt;&lt; e &lt;&lt; std::endl;
114
 
 
115
 
                torrent_info t(e);
116
 
 
117
 
                // print info about torrent
118
 
                std::cout &lt;&lt; &quot;\n\n----- torrent file info -----\n\n&quot;;
119
 
                std::cout &lt;&lt; &quot;nodes:\n&quot;;
120
 
                typedef std::vector&lt;std::pair&lt;std::string, int&gt; &gt; node_vec;
121
 
                node_vec const&amp; nodes = t.nodes();
122
 
                for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
123
 
                        i != end; ++i)
124
 
                {
125
 
                        std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot;:&quot; &lt;&lt; i-&gt;second &lt;&lt; &quot;\n&quot;;
126
 
                }
127
 
                std::cout &lt;&lt; &quot;trackers:\n&quot;;
128
 
                for (std::vector&lt;announce_entry&gt;::const_iterator i = t.trackers().begin();
129
 
                        i != t.trackers().end(); ++i)
130
 
                {
131
 
                        std::cout &lt;&lt; i-&gt;tier &lt;&lt; &quot;: &quot; &lt;&lt; i-&gt;url &lt;&lt; &quot;\n&quot;;
132
 
                }
133
 
 
134
 
                std::cout &lt;&lt; &quot;number of pieces: &quot; &lt;&lt; t.num_pieces() &lt;&lt; &quot;\n&quot;;
135
 
                std::cout &lt;&lt; &quot;piece length: &quot; &lt;&lt; t.piece_length() &lt;&lt; &quot;\n&quot;;
136
 
                std::cout &lt;&lt; &quot;info hash: &quot; &lt;&lt; t.info_hash() &lt;&lt; &quot;\n&quot;;
137
 
                std::cout &lt;&lt; &quot;comment: &quot; &lt;&lt; t.comment() &lt;&lt; &quot;\n&quot;;
138
 
                std::cout &lt;&lt; &quot;created by: &quot; &lt;&lt; t.creator() &lt;&lt; &quot;\n&quot;;
139
 
                std::cout &lt;&lt; &quot;files:\n&quot;;
140
 
                int index = 0;
141
 
                for (torrent_info::file_iterator i = t.begin_files();
142
 
                        i != t.end_files(); ++i, ++index)
143
 
                {
144
 
                        int first = t.map_file(index, 0, 1).piece;
145
 
                        int last = t.map_file(index, i-&gt;size - 1, 1).piece;
146
 
                        std::cout &lt;&lt; &quot;  &quot; &lt;&lt; std::setw(11) &lt;&lt; i-&gt;size
147
 
                                &lt;&lt; &quot; &quot; &lt;&lt; i-&gt;path.string() &lt;&lt; &quot;[ &quot; &lt;&lt; first &lt;&lt; &quot;, &quot;
148
 
                                &lt;&lt; last &lt;&lt; &quot; ]\n&quot;;
149
 
                }
150
 
 
151
 
#ifndef BOOST_NO_EXCEPTIONS
152
 
        }
153
 
        catch (std::exception&amp; e)
154
 
        {
155
 
                std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
156
 
        }
157
 
#endif
158
 
 
159
 
        return 0;
160
 
}
161
 
</pre>
162
 
</div>
163
 
<div class="section" id="simple-client">
164
 
<h2>simple client</h2>
165
 
<p>This is a simple client. It doesn't have much output to keep it simple:</p>
166
 
<pre class="literal-block">
167
 
int main(int argc, char* argv[])
168
 
{
169
 
        using namespace libtorrent;
170
 
#if BOOST_VERSION &lt; 103400
171
 
        namespace fs = boost::filesystem;
172
 
        fs::path::default_name_check(fs::no_check);
173
 
#endif
174
 
 
175
 
if (argc != 2)
176
 
{
177
 
        std::cerr &lt;&lt; &quot;usage: ./simple_client torrent-file\n&quot;
178
 
                &quot;to stop the client, press return.\n&quot;;
179
 
        return 1;
180
 
}
181
 
 
182
 
#ifndef BOOST_NO_EXCEPTIONS
183
 
        try
184
 
#endif
185
 
        {
186
 
                session s;
187
 
                s.listen_on(std::make_pair(6881, 6889));
188
 
                add_torrent_params p;
189
 
                p.save_path = &quot;./&quot;;
190
 
                p.ti = new torrent_info(argv[1]);
191
 
                s.add_torrent(p);
192
 
 
193
 
                // wait for the user to end
194
 
                char a;
195
 
                std::cin.unsetf(std::ios_base::skipws);
196
 
                std::cin &gt;&gt; a;
197
 
        }
198
 
#ifndef BOOST_NO_EXCEPTIONS
199
 
        catch (std::exception&amp; e)
200
 
        {
201
 
                std::cout &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
202
 
        }
203
 
#endif
204
 
        return 0;
205
 
}
206
 
</pre>
207
 
</div>
208
 
<div class="section" id="make-torrent">
209
 
<h2>make_torrent</h2>
210
 
<p>Shows how to create a torrent from a directory tree:</p>
211
 
<pre class="literal-block">
212
 
#include &lt;iostream&gt;
213
 
#include &lt;fstream&gt;
214
 
#include &lt;iterator&gt;
215
 
#include &lt;iomanip&gt;
216
 
 
217
 
#include &quot;libtorrent/entry.hpp&quot;
218
 
#include &quot;libtorrent/bencode.hpp&quot;
219
 
#include &quot;libtorrent/torrent_info.hpp&quot;
220
 
#include &quot;libtorrent/file.hpp&quot;
221
 
#include &quot;libtorrent/storage.hpp&quot;
222
 
#include &quot;libtorrent/hasher.hpp&quot;
223
 
#include &quot;libtorrent/create_torrent.hpp&quot;
224
 
 
225
 
#include &lt;boost/filesystem/operations.hpp&gt;
226
 
#include &lt;boost/filesystem/path.hpp&gt;
227
 
#include &lt;boost/filesystem/fstream.hpp&gt;
228
 
#include &lt;boost/bind.hpp&gt;
229
 
 
230
 
using namespace boost::filesystem;
231
 
using namespace libtorrent;
232
 
 
233
 
// do not include files and folders whose
234
 
// name starts with a .
235
 
bool file_filter(boost::filesystem::path const&amp; filename)
236
 
{
237
 
        if (filename.leaf()[0] == '.') return false;
238
 
        std::cerr &lt;&lt; filename &lt;&lt; std::endl;
239
 
        return true;
240
 
}
241
 
 
242
 
void print_progress(int i, int num)
243
 
{
244
 
        std::cerr &lt;&lt; &quot;\r&quot; &lt;&lt; (i+1) &lt;&lt; &quot;/&quot; &lt;&lt; num;
245
 
}
246
 
 
247
 
int main(int argc, char* argv[])
248
 
{
249
 
        using namespace libtorrent;
250
 
        using namespace boost::filesystem;
251
 
 
252
 
        int piece_size = 256 * 1024;
253
 
        char const* creator_str = &quot;libtorrent&quot;;
254
 
 
255
 
        path::default_name_check(no_check);
256
 
 
257
 
        if (argc != 4 &amp;&amp; argc != 5)
258
 
        {
259
 
                std::cerr &lt;&lt; &quot;usage: make_torrent &lt;output torrent-file&gt; &quot;
260
 
                &quot;&lt;announce url&gt; &lt;file or directory to create torrent from&gt; &quot;
261
 
                &quot;[url-seed]\n&quot;;
262
 
        return 1;
263
 
}
264
 
 
265
 
#ifndef BOOST_NO_EXCEPTIONS
266
 
        try
267
 
        {
268
 
#endif
269
 
                file_storage fs;
270
 
                file_pool fp;
271
 
                path full_path = complete(path(argv[3]));
272
 
 
273
 
                add_files(fs, full_path, file_filter);
274
 
 
275
 
                create_torrent t(fs, piece_size);
276
 
                t.add_tracker(argv[2]);
277
 
                set_piece_hashes(t, full_path.branch_path()
278
 
                        , boost::bind(&amp;print_progress, _1, t.num_pieces()));
279
 
                std::cerr &lt;&lt; std::endl;
280
 
                t.set_creator(creator_str);
281
 
 
282
 
                if (argc == 5) t.add_url_seed(argv[4]);
283
 
 
284
 
                // create the torrent and print it to out
285
 
                ofstream out(complete(path(argv[1])), std::ios_base::binary);
286
 
                bencode(std::ostream_iterator&lt;char&gt;(out), t.generate());
287
 
#ifndef BOOST_NO_EXCEPTIONS
288
 
        }
289
 
        catch (std::exception&amp; e)
290
 
        {
291
 
                std::cerr &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
292
 
        }
293
 
#endif
294
 
 
295
 
        return 0;
296
 
}
297
 
</pre>
298
 
</div>
299
 
</div>
300
 
    </div>
301
 
    <div id="footer">
302
 
    <span>Copyright &copy; 2005 Rasterbar Software.</span>
303
 
    </div>
304
 
    </div>
305
 
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
306
 
    </script>
307
 
    <script type="text/javascript">
308
 
    _uacct = "UA-1599045-1";
309
 
    urchinTracker();
310
 
    </script>
311
 
</div>
312
 
</body>
313
 
</html>