fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / bindings / python / src / session.cpp
blob569a85d4804736b2036e1c9cd50c60bf32c43f4e
1 // Copyright Daniel Wallin, Arvid Norberg 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)
5 #include <libtorrent/session.hpp>
6 #include <libtorrent/torrent.hpp>
7 #include <libtorrent/storage.hpp>
8 #include <libtorrent/ip_filter.hpp>
9 #include <boost/python.hpp>
10 #include "gil.hpp"
12 using namespace boost::python;
13 using namespace libtorrent;
15 extern char const* session_status_doc;
16 extern char const* session_status_has_incoming_connections_doc;
17 extern char const* session_status_upload_rate_doc;
18 extern char const* session_status_download_rate_doc;
19 extern char const* session_status_payload_upload_rate_doc;
20 extern char const* session_status_payload_download_rate_doc;
21 extern char const* session_status_total_download_doc;
22 extern char const* session_status_total_upload_doc;
23 extern char const* session_status_total_payload_download_doc;
24 extern char const* session_status_total_payload_upload_doc;
25 extern char const* session_status_num_peers_doc;
26 extern char const* session_status_dht_nodes_doc;
27 extern char const* session_status_cache_nodes_doc;
28 extern char const* session_status_dht_torrents_doc;
30 extern char const* session_doc;
31 extern char const* session_init_doc;
32 extern char const* session_listen_on_doc;
33 extern char const* session_is_listening_doc;
34 extern char const* session_listen_port_doc;
35 extern char const* session_status_m_doc;
36 extern char const* session_start_dht_doc;
37 extern char const* session_stop_dht_doc;
38 extern char const* session_dht_state_doc;
39 extern char const* session_add_dht_router_doc;
40 extern char const* session_add_torrent_doc;
41 extern char const* session_remove_torrent_doc;
42 extern char const* session_set_download_rate_limit_doc;
43 extern char const* session_download_rate_limit_doc;
44 extern char const* session_set_upload_rate_limit_doc;
45 extern char const* session_upload_rate_limit_doc;
46 extern char const* session_set_max_uploads_doc;
47 extern char const* session_set_max_connections_doc;
48 extern char const* session_set_max_half_open_connections_doc;
49 extern char const* session_num_connections_doc;
50 extern char const* session_set_settings_doc;
51 extern char const* session_set_pe_settings_doc;
52 extern char const* session_get_pe_settings_doc;
53 extern char const* session_set_severity_level_doc;
54 extern char const* session_pop_alert_doc;
55 extern char const* session_start_upnp_doc;
56 extern char const* session_start_lsd_doc;
57 extern char const* session_stop_lsd_doc;
58 extern char const* session_stop_upnp_doc;
59 extern char const* session_start_natpmp_doc;
60 extern char const* session_stop_natpmp_doc;
61 extern char const* session_set_ip_filter_doc;
63 namespace
66 bool listen_on(session& s, int min_, int max_, char const* interface)
68 allow_threading_guard guard;
69 return s.listen_on(std::make_pair(min_, max_), interface);
71 void outgoing_ports(session& s, int _min, int _max)
73 allow_threading_guard guard;
74 session_settings settings = s.settings();
75 settings.outgoing_ports = std::make_pair(_min, _max);
76 s.set_settings(settings);
77 return;
79 #ifndef TORRENT_DISABLE_DHT
80 void add_dht_router(session& s, std::string router_, int port_)
82 allow_threading_guard guard;
83 return s.add_dht_router(std::make_pair(router_, port_));
85 #endif
87 struct invoke_extension_factory
89 invoke_extension_factory(object const& callback)
90 : cb(callback)
93 boost::shared_ptr<torrent_plugin> operator()(torrent* t, void*)
95 lock_gil lock;
96 return extract<boost::shared_ptr<torrent_plugin> >(cb(ptr(t)))();
99 object cb;
102 void add_extension(session& s, object const& e)
104 allow_threading_guard guard;
105 s.add_extension(invoke_extension_factory(e));
108 torrent_handle add_torrent(session& s, dict params)
110 add_torrent_params p;
112 if (params.has_key("ti"))
113 p.ti = new torrent_info(extract<torrent_info const&>(params["ti"]));
115 std::string url;
116 if (params.has_key("tracker_url"))
118 url = extract<std::string>(params["tracker_url"]);
119 p.tracker_url = url.c_str();
121 if (params.has_key("info_hash"))
122 p.info_hash = extract<sha1_hash>(params["info_hash"]);
123 std::string name;
124 if (params.has_key("name"))
126 name = extract<std::string>(params["name"]);
127 p.name = name.c_str();
129 p.save_path = fs::path(extract<std::string>(params["save_path"]));
131 std::vector<char> resume_buf;
132 if (params.has_key("resume_data"))
134 std::string resume = extract<std::string>(params["resume_data"]);
135 resume_buf.resize(resume.size());
136 std::memcpy(&resume_buf[0], &resume[0], resume.size());
137 p.resume_data = &resume_buf;
139 p.storage_mode = extract<storage_mode_t>(params["storage_mode"]);
140 p.paused = params["paused"];
141 p.auto_managed = params["auto_managed"];
142 p.duplicate_is_error = params["duplicate_is_error"];
144 return s.add_torrent(p);
147 void start_natpmp(session& s)
149 allow_threading_guard guard;
150 s.start_natpmp();
151 return;
154 void start_upnp(session& s)
156 allow_threading_guard guard;
157 s.start_upnp();
158 return;
161 list get_torrents(session& s)
163 list ret;
164 std::vector<torrent_handle> torrents = s.get_torrents();
166 for (std::vector<torrent_handle>::iterator i = torrents.begin(); i != torrents.end(); ++i)
168 ret.append(*i);
170 return ret;
173 #ifndef TORRENT_DISABLE_GEO_IP
174 bool load_asnum_db(session& s, std::string file)
176 allow_threading_guard guard;
177 return s.load_asnum_db(file.c_str());
180 bool load_country_db(session& s, std::string file)
182 allow_threading_guard guard;
183 return s.load_country_db(file.c_str());
185 #endif
186 } // namespace unnamed
188 void bind_session()
190 class_<session_status>("session_status", session_status_doc)
191 .def_readonly(
192 "has_incoming_connections", &session_status::has_incoming_connections
193 , session_status_has_incoming_connections_doc
195 .def_readonly(
196 "upload_rate", &session_status::upload_rate
197 , session_status_upload_rate_doc
199 .def_readonly(
200 "download_rate", &session_status::download_rate
201 , session_status_download_rate_doc
203 .def_readonly(
204 "payload_upload_rate", &session_status::payload_upload_rate
205 , session_status_payload_upload_rate_doc
207 .def_readonly(
208 "payload_download_rate", &session_status::payload_download_rate
209 , session_status_payload_download_rate_doc
211 .def_readonly(
212 "total_download", &session_status::total_download
213 , session_status_total_download_doc
215 .def_readonly(
216 "total_upload", &session_status::total_upload
217 , session_status_total_upload_doc
219 .def_readonly(
220 "total_payload_download", &session_status::total_payload_download
221 , session_status_total_payload_download_doc
223 .def_readonly(
224 "total_payload_upload", &session_status::total_payload_upload
225 , session_status_total_payload_upload_doc
227 .def_readonly(
228 "num_peers", &session_status::num_peers
229 , session_status_num_peers_doc
231 #ifndef TORRENT_DISABLE_DHT
232 .def_readonly(
233 "dht_nodes", &session_status::dht_nodes
234 , session_status_dht_nodes_doc
236 .def_readonly(
237 "dht_cache_nodes", &session_status::dht_node_cache
238 , session_status_cache_nodes_doc
240 .def_readonly(
241 "dht_torrents", &session_status::dht_torrents
242 , session_status_dht_torrents_doc
244 #endif
247 enum_<storage_mode_t>("storage_mode_t")
248 .value("storage_mode_allocate", storage_mode_allocate)
249 .value("storage_mode_sparse", storage_mode_sparse)
250 .value("storage_mode_compact", storage_mode_compact)
253 enum_<session::options_t>("options_t")
254 .value("none", session::none)
255 .value("delete_files", session::delete_files)
258 class_<session, boost::noncopyable>("session", session_doc, no_init)
259 .def(
260 init<fingerprint>(arg("fingerprint")=fingerprint("LT",0,1,0,0), session_init_doc)
262 .def(
263 "listen_on", &listen_on
264 , (arg("min"), "max", arg("interface") = (char const*)0)
265 , session_listen_on_doc
267 .def("outgoing_ports", &outgoing_ports)
268 .def("is_listening", allow_threads(&session::is_listening), session_is_listening_doc)
269 .def("listen_port", allow_threads(&session::listen_port), session_listen_port_doc)
270 .def("status", allow_threads(&session::status), session_status_m_doc)
271 #ifndef TORRENT_DISABLE_DHT
272 .def(
273 "add_dht_router", &add_dht_router
274 , (arg("router"), "port")
275 , session_add_dht_router_doc
277 .def("start_dht", allow_threads(&session::start_dht), session_start_dht_doc)
278 .def("stop_dht", allow_threads(&session::stop_dht), session_stop_dht_doc)
279 .def("dht_state", allow_threads(&session::dht_state), session_dht_state_doc)
280 .def("set_dht_proxy", allow_threads(&session::set_dht_proxy))
281 #endif
282 .def("add_torrent", &add_torrent, session_add_torrent_doc)
283 .def("remove_torrent", allow_threads(&session::remove_torrent), arg("option") = session::none
284 , session_remove_torrent_doc)
285 .def(
286 "set_download_rate_limit", allow_threads(&session::set_download_rate_limit)
287 , session_set_download_rate_limit_doc
289 .def(
290 "download_rate_limit", allow_threads(&session::download_rate_limit)
291 , session_download_rate_limit_doc
294 .def(
295 "set_upload_rate_limit", allow_threads(&session::set_upload_rate_limit)
296 , session_set_upload_rate_limit_doc
298 .def(
299 "upload_rate_limit", allow_threads(&session::upload_rate_limit)
300 , session_upload_rate_limit_doc
303 .def(
304 "set_max_uploads", allow_threads(&session::set_max_uploads)
305 , session_set_max_uploads_doc
307 .def(
308 "set_max_connections", allow_threads(&session::set_max_connections)
309 , session_set_max_connections_doc
311 .def(
312 "set_max_half_open_connections", allow_threads(&session::set_max_half_open_connections)
313 , session_set_max_half_open_connections_doc
315 .def(
316 "num_connections", allow_threads(&session::num_connections)
317 , session_num_connections_doc
319 .def("set_settings", allow_threads(&session::set_settings), session_set_settings_doc)
320 .def("settings", allow_threads(&session::settings), return_value_policy<copy_const_reference>())
321 #ifndef TORRENT_DISABLE_ENCRYPTION
322 .def("set_pe_settings", allow_threads(&session::set_pe_settings), session_set_pe_settings_doc)
323 .def("get_pe_settings", allow_threads(&session::get_pe_settings), return_value_policy<copy_const_reference>())
324 #endif
325 #ifndef TORRENT_DISABLE_GEO_IP
326 .def("load_asnum_db", &load_asnum_db)
327 .def("load_country_db", &load_country_db)
328 #endif
329 .def("load_state", allow_threads(&session::load_state))
330 .def("state", allow_threads(&session::state))
331 .def(
332 "set_severity_level", allow_threads(&session::set_severity_level)
333 , session_set_severity_level_doc
335 .def("set_alert_mask", allow_threads(&session::set_alert_mask))
336 .def("pop_alert", allow_threads(&session::pop_alert), session_pop_alert_doc)
337 .def("add_extension", &add_extension)
338 .def("set_peer_proxy", allow_threads(&session::set_peer_proxy))
339 .def("set_tracker_proxy", allow_threads(&session::set_tracker_proxy))
340 .def("set_web_seed_proxy", allow_threads(&session::set_web_seed_proxy))
341 .def("start_upnp", &start_upnp, session_start_upnp_doc)
342 .def("stop_upnp", allow_threads(&session::stop_upnp), session_stop_upnp_doc)
343 .def("start_lsd", allow_threads(&session::start_lsd), session_start_lsd_doc)
344 .def("stop_lsd", allow_threads(&session::stop_lsd), session_stop_lsd_doc)
345 .def("start_natpmp", &start_natpmp, session_start_natpmp_doc)
346 .def("stop_natpmp", allow_threads(&session::stop_natpmp), session_stop_natpmp_doc)
347 .def("set_ip_filter", allow_threads(&session::set_ip_filter), session_set_ip_filter_doc)
348 .def("find_torrent", allow_threads(&session::find_torrent))
349 .def("get_torrents", &get_torrents)
350 .def("pause", allow_threads(&session::pause))
351 .def("resume", allow_threads(&session::resume))
352 .def("is_paused", allow_threads(&session::is_paused))
355 register_ptr_to_python<std::auto_ptr<alert> >();