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

« back to all changes in this revision

Viewing changes to bindings/python/src/torrent_handle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright Daniel Wallin 2006. Use, modification and distribution is
 
2
// subject to the Boost Software License, Version 1.0. (See accompanying
 
3
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
4
 
 
5
#include <libtorrent/torrent_handle.hpp>
 
6
#include <boost/python.hpp>
 
7
#include <boost/lexical_cast.hpp>
 
8
#include "gil.hpp"
 
9
 
 
10
using namespace boost::python;
 
11
using namespace libtorrent;
 
12
 
 
13
namespace
 
14
{
 
15
 
 
16
  list url_seeds(torrent_handle& handle)
 
17
  {
 
18
      list ret;
 
19
      std::set<std::string> urls;
 
20
      {
 
21
          allow_threading_guard guard;
 
22
          urls = handle.url_seeds();
 
23
      }
 
24
 
 
25
      for (std::set<std::string>::iterator i(urls.begin())
 
26
          , end(urls.end()); i != end; ++i)
 
27
          ret.append(*i);
 
28
      return ret;
 
29
  }
 
30
 
 
31
  list piece_availability(torrent_handle& handle)
 
32
  {
 
33
      list ret;
 
34
      std::vector<int> avail;
 
35
      {
 
36
          allow_threading_guard guard;
 
37
          handle.piece_availability(avail);
 
38
      }
 
39
 
 
40
      for (std::vector<int>::iterator i(avail.begin())
 
41
          , end(avail.end()); i != end; ++i)
 
42
          ret.append(*i);
 
43
      return ret;
 
44
  }
 
45
 
 
46
  list piece_priorities(torrent_handle& handle)
 
47
  {
 
48
      list ret;
 
49
      std::vector<int> prio;
 
50
      {
 
51
          allow_threading_guard guard;
 
52
          prio = handle.piece_priorities();
 
53
      }
 
54
 
 
55
      for (std::vector<int>::iterator i(prio.begin())
 
56
          , end(prio.end()); i != end; ++i)
 
57
          ret.append(*i);
 
58
      return ret;
 
59
  }
 
60
 
 
61
  std::vector<announce_entry>::const_iterator begin_trackers(torrent_handle& i)
 
62
  {
 
63
      allow_threading_guard guard;
 
64
      return i.trackers().begin();
 
65
  }
 
66
 
 
67
  std::vector<announce_entry>::const_iterator end_trackers(torrent_handle& i)
 
68
  {
 
69
      allow_threading_guard guard;
 
70
      return i.trackers().end();
 
71
  }
 
72
 
 
73
} // namespace unnamed
 
74
 
 
75
list file_progress(torrent_handle& handle)
 
76
{
 
77
    std::vector<float> p;
 
78
 
 
79
    {
 
80
        allow_threading_guard guard;
 
81
        p.reserve(handle.get_torrent_info().num_files());
 
82
        handle.file_progress(p);
 
83
    }
 
84
 
 
85
    list result;
 
86
 
 
87
    for (std::vector<float>::iterator i(p.begin()), e(p.end()); i != e; ++i)
 
88
        result.append(*i);
 
89
 
 
90
    return result;
 
91
}
 
92
 
 
93
list get_peer_info(torrent_handle const& handle)
 
94
{
 
95
    std::vector<peer_info> pi;
 
96
 
 
97
    {
 
98
        allow_threading_guard guard;
 
99
        handle.get_peer_info(pi);
 
100
    }
 
101
 
 
102
    list result;
 
103
 
 
104
    for (std::vector<peer_info>::iterator i = pi.begin(); i != pi.end(); ++i)
 
105
        result.append(*i);
 
106
 
 
107
    return result;
 
108
}
 
109
 
 
110
void prioritize_pieces(torrent_handle& info, object o)
 
111
{
 
112
   std::vector<int> result;
 
113
   try
 
114
   {
 
115
      object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
 
116
      while( 1 )
 
117
      {
 
118
         object obj = extract<object>( iter_obj.attr( "next" )() );
 
119
         result.push_back(extract<int const>( obj ));
 
120
      }
 
121
   }
 
122
   catch( error_already_set )
 
123
   {
 
124
      PyErr_Clear();
 
125
      info.prioritize_pieces(result);
 
126
      return;
 
127
   }
 
128
}
 
129
 
 
130
void prioritize_files(torrent_handle& info, object o)
 
131
{
 
132
   std::vector<int> result;
 
133
   try
 
134
   {
 
135
      object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
 
136
      while( 1 )
 
137
      {
 
138
         object obj = extract<object>( iter_obj.attr( "next" )() );
 
139
         result.push_back(extract<int const>( obj ));
 
140
      }
 
141
   }
 
142
   catch( error_already_set )
 
143
   {
 
144
      PyErr_Clear();
 
145
      info.prioritize_files(result);
 
146
      return;
 
147
   }
 
148
}
 
149
 
 
150
 
 
151
void replace_trackers(torrent_handle& info, object trackers)
 
152
{
 
153
    object iter(trackers.attr("__iter__")());
 
154
 
 
155
    std::vector<announce_entry> result;
 
156
 
 
157
    for (;;)
 
158
    {
 
159
        handle<> entry(allow_null(PyIter_Next(iter.ptr())));
 
160
 
 
161
        if (entry == handle<>())
 
162
            break;
 
163
 
 
164
        result.push_back(extract<announce_entry const&>(object(entry)));
 
165
    }
 
166
 
 
167
    allow_threading_guard guard;
 
168
    info.replace_trackers(result);
 
169
}
 
170
 
 
171
list get_download_queue(torrent_handle& handle)
 
172
{
 
173
    list ret;
 
174
 
 
175
    std::vector<partial_piece_info> downloading;
 
176
 
 
177
    {
 
178
        allow_threading_guard guard;
 
179
        handle.get_download_queue(downloading);
 
180
    }
 
181
 
 
182
    for (std::vector<partial_piece_info>::iterator i = downloading.begin()
 
183
        , end(downloading.end()); i != end; ++i)
 
184
    {
 
185
        dict partial_piece;
 
186
        partial_piece["piece_index"] = i->piece_index;
 
187
        partial_piece["blocks_in_piece"] = i->blocks_in_piece;
 
188
        list block_list;
 
189
        for (int k = 0; k < i->blocks_in_piece; ++k)
 
190
        {
 
191
            dict block_info;
 
192
            block_info["state"] = i->blocks[k].state;
 
193
            block_info["num_peers"] = i->blocks[k].num_peers;
 
194
            block_info["bytes_progress"] = i->blocks[k].bytes_progress;
 
195
            block_info["block_size"] = i->blocks[k].block_size;
 
196
            block_info["peer"] = std::make_pair(
 
197
                boost::lexical_cast<std::string>(i->blocks[k].peer.address()), i->blocks[k].peer.port());
 
198
            block_list.append(block_info);
 
199
        }
 
200
        partial_piece["blocks"] = block_list;
 
201
 
 
202
        ret.append(partial_piece);
 
203
    }
 
204
 
 
205
    return ret;
 
206
}
 
207
 
 
208
namespace
 
209
{
 
210
    tcp::endpoint tuple_to_endpoint(tuple const& t)
 
211
    {
 
212
        return tcp::endpoint(address::from_string(extract<std::string>(t[0])), extract<int>(t[1]));
 
213
    }
 
214
}
 
215
 
 
216
void force_reannounce(torrent_handle& th, int s)
 
217
{
 
218
    th.force_reannounce(boost::posix_time::seconds(s));
 
219
}
 
220
 
 
221
void connect_peer(torrent_handle& th, tuple ip, int source)
 
222
{
 
223
    th.connect_peer(tuple_to_endpoint(ip), source);
 
224
}
 
225
 
 
226
void set_peer_upload_limit(torrent_handle& th, tuple const& ip, int limit)
 
227
{
 
228
    th.set_peer_upload_limit(tuple_to_endpoint(ip), limit);
 
229
}
 
230
 
 
231
void set_peer_download_limit(torrent_handle& th, tuple const& ip, int limit)
 
232
{
 
233
    th.set_peer_download_limit(tuple_to_endpoint(ip), limit);
 
234
}
 
235
 
 
236
void bind_torrent_handle()
 
237
{
 
238
    void (torrent_handle::*force_reannounce0)() const = &torrent_handle::force_reannounce;
 
239
 
 
240
    int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority;
 
241
    void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority;
 
242
 
 
243
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES       
 
244
    bool (torrent_handle::*resolve_countries0)() const = &torrent_handle::resolve_countries;
 
245
    void (torrent_handle::*resolve_countries1)(bool) = &torrent_handle::resolve_countries;
 
246
#endif
 
247
 
 
248
    return_value_policy<copy_const_reference> copy;
 
249
 
 
250
#define _ allow_threads
 
251
 
 
252
    class_<torrent_handle>("torrent_handle")
 
253
        .def("get_peer_info", get_peer_info)
 
254
        .def("status", _(&torrent_handle::status))
 
255
        .def("get_download_queue", get_download_queue)
 
256
        .def("file_progress", file_progress)
 
257
        .def("trackers", range(begin_trackers, end_trackers))
 
258
        .def("replace_trackers", replace_trackers)
 
259
        .def("add_url_seed", _(&torrent_handle::add_url_seed))
 
260
        .def("remove_url_seed", _(&torrent_handle::remove_url_seed))
 
261
        .def("url_seeds", url_seeds)
 
262
        .def("has_metadata", _(&torrent_handle::has_metadata))
 
263
        .def("get_torrent_info", _(&torrent_handle::get_torrent_info), return_internal_reference<>())
 
264
        .def("is_valid", _(&torrent_handle::is_valid))
 
265
        .def("is_seed", _(&torrent_handle::is_seed))
 
266
        .def("is_finished", _(&torrent_handle::is_finished))
 
267
        .def("is_paused", _(&torrent_handle::is_paused))
 
268
        .def("pause", _(&torrent_handle::pause))
 
269
        .def("resume", _(&torrent_handle::resume))
 
270
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES       
 
271
        .def("resolve_countries", _(resolve_countries0))
 
272
        .def("resolve_countries", _(resolve_countries1))
 
273
#endif
 
274
        // deprecated
 
275
        .def("filter_piece", _(&torrent_handle::filter_piece))
 
276
        .def("is_piece_filtered", _(&torrent_handle::is_piece_filtered))
 
277
 
 
278
        .def("piece_availability", piece_availability)
 
279
        .def("piece_priority", _(piece_priority0))
 
280
        .def("piece_priority", _(piece_priority1))
 
281
        .def("prioritize_pieces", prioritize_pieces)
 
282
        .def("piece_prioritize", piece_priorities)
 
283
        .def("prioritize_files", prioritize_files)
 
284
        .def("use_interface", &torrent_handle::use_interface)
 
285
        .def("write_resume_data", _(&torrent_handle::write_resume_data))
 
286
        .def("force_reannounce", _(force_reannounce0))
 
287
        .def("force_reannounce", force_reannounce)
 
288
        .def("scrape_tracker", _(&torrent_handle::scrape_tracker))
 
289
        .def("name", _(&torrent_handle::name))
 
290
        .def("set_upload_limit", _(&torrent_handle::set_upload_limit))
 
291
        .def("upload_limit", _(&torrent_handle::upload_limit))
 
292
        .def("set_download_limit", _(&torrent_handle::set_download_limit))
 
293
        .def("download_limit", _(&torrent_handle::download_limit))
 
294
        .def("set_sequenced_download_threshold", _(&torrent_handle::set_sequenced_download_threshold))
 
295
        .def("set_peer_upload_limit", set_peer_upload_limit)
 
296
        .def("set_peer_download_limit", set_peer_download_limit)
 
297
        .def("connect_peer", connect_peer)
 
298
        .def("set_ratio", _(&torrent_handle::set_ratio))
 
299
        .def("save_path", _(&torrent_handle::save_path))
 
300
        .def("set_max_uploads", _(&torrent_handle::set_max_uploads))
 
301
        .def("set_max_connections", _(&torrent_handle::set_max_connections))
 
302
        .def("set_tracker_login", _(&torrent_handle::set_tracker_login))
 
303
        .def("move_storage", _(&torrent_handle::move_storage))
 
304
        .def("info_hash", _(&torrent_handle::info_hash), copy)
 
305
        ;
 
306
}
 
307