fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / bindings / python / src / peer_plugin.cpp
blob6ba48ff80ad503588e2086790835f7141cdc19ac
1 // Copyright Daniel Wallin 2007. 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 <cctype>
6 #include <iostream>
8 #include <libtorrent/extensions.hpp>
9 #include <libtorrent/entry.hpp>
10 #include <libtorrent/lazy_entry.hpp>
11 #include <libtorrent/peer_request.hpp>
12 #include <libtorrent/disk_buffer_holder.hpp>
13 #include <libtorrent/bitfield.hpp>
14 #include <boost/python.hpp>
16 using namespace boost::python;
17 using namespace libtorrent;
19 namespace
21 struct peer_plugin_wrap : peer_plugin, wrapper<peer_plugin>
23 void add_handshake(entry& e)
25 if (override f = this->get_override("add_handshake"))
26 e = call<entry>(f.ptr(), e);
27 else
28 peer_plugin::add_handshake(e);
31 void default_add_handshake(entry& e)
33 this->peer_plugin::add_handshake(e);
36 bool on_handshake(char const* reserved_bits)
38 if (override f = this->get_override("on_handshake"))
39 return f();
40 else
41 return peer_plugin::on_handshake(reserved_bits);
44 bool default_on_handshake(char const* reserved_bits)
46 return this->peer_plugin::on_handshake(reserved_bits);
49 bool on_extension_handshake(lazy_entry const& e)
51 if (override f = this->get_override("on_extension_handshake"))
52 return f(e);
53 else
54 return peer_plugin::on_extension_handshake(e);
57 bool default_on_extension_handshake(lazy_entry const& e)
59 return this->peer_plugin::on_extension_handshake(e);
62 bool on_choke()
64 if (override f = this->get_override("on_choke"))
65 return f();
66 else
67 return peer_plugin::on_choke();
70 bool default_on_choke()
72 return this->peer_plugin::on_choke();
75 bool on_unchoke()
77 if (override f = this->get_override("on_unchoke"))
78 return f();
79 else
80 return peer_plugin::on_unchoke();
83 bool default_on_unchoke()
85 return this->peer_plugin::on_unchoke();
88 bool on_interested()
90 if (override f = this->get_override("on_interested"))
91 return f();
92 else
93 return peer_plugin::on_interested();
96 bool default_on_interested()
98 return this->peer_plugin::on_interested();
101 bool on_not_interested()
103 if (override f = this->get_override("on_not_interested"))
104 return f();
105 else
106 return peer_plugin::on_not_interested();
109 bool default_on_not_interested()
111 return this->peer_plugin::on_not_interested();
114 bool on_have(int index)
116 if (override f = this->get_override("on_have"))
117 return f(index);
118 else
119 return peer_plugin::on_have(index);
122 bool default_on_have(int index)
124 return this->peer_plugin::on_have(index);
127 bool on_bitfield(list _bf)
129 //Convert list to a bitfield
130 bitfield bf(len(_bf));
131 for (int i = 0; i < len(_bf); ++i)
133 if (_bf[i])
134 bf.set_bit(i);
135 else
136 bf.clear_bit(i);
138 if (override f = this->get_override("on_bitfield"))
139 return f(bf);
140 else
141 return peer_plugin::on_bitfield(bf);
144 bool default_on_bitfield(const bitfield &bf)
146 return this->peer_plugin::on_bitfield(bf);
149 bool on_request(peer_request const& req)
151 if (override f = this->get_override("on_request"))
152 return f(req);
153 else
154 return peer_plugin::on_request(req);
157 bool default_on_request(peer_request const& req)
159 return this->peer_plugin::on_request(req);
162 bool on_piece(peer_request const& piece, disk_buffer_holder& data)
164 if (override f = this->get_override("on_piece"))
165 return f(piece, data);
166 else
167 return peer_plugin::on_piece(piece, data);
170 bool default_on_piece(peer_request const& piece, disk_buffer_holder& data)
172 return this->peer_plugin::on_piece(piece, data);
175 bool on_cancel(peer_request const& req)
177 if (override f = this->get_override("on_cancel"))
178 return f(req);
179 else
180 return peer_plugin::on_cancel(req);
183 bool default_on_cancel(peer_request const& req)
185 return this->peer_plugin::on_cancel(req);
188 bool on_extended(int length, int msg, buffer::const_interval body)
190 if (override f = this->get_override("on_extended"))
191 return f(length, msg, body);
192 else
193 return peer_plugin::on_extended(length, msg, body);
196 bool default_on_extended(int length, int msg, buffer::const_interval body)
198 return this->peer_plugin::on_extended(length, msg, body);
201 bool on_unknown_message(int length, int msg, buffer::const_interval body)
203 if (override f = this->get_override("on_unknown_message"))
204 return f(length, msg, body);
205 else
206 return peer_plugin::on_unknown_message(length, msg, body);
209 bool default_on_unknown_message(int length, int msg, buffer::const_interval body)
211 return this->peer_plugin::on_unknown_message(length, msg, body);
214 void on_piece_pass(int index)
216 if (override f = this->get_override("on_piece_pass"))
217 f(index);
218 else
219 peer_plugin::on_piece_pass(index);
222 void default_on_piece_pass(int index)
224 this->peer_plugin::on_piece_pass(index);
227 void on_piece_failed(int index)
229 if (override f = this->get_override("on_piece_failed"))
230 f(index);
231 else
232 peer_plugin::on_piece_failed(index);
235 void default_on_piece_failed(int index)
237 this->peer_plugin::on_piece_failed(index);
240 void tick()
242 if (override f = this->get_override("tick"))
243 f();
244 else
245 peer_plugin::tick();
248 void default_tick()
250 this->peer_plugin::tick();
253 bool write_request(peer_request const& req)
255 if (override f = this->get_override("write_request"))
256 return f(req);
257 else
258 return peer_plugin::write_request(req);
261 bool default_write_request(peer_request const& req)
263 return this->peer_plugin::write_request(req);
267 object get_buffer()
269 static char const data[] = "foobar";
270 return object(handle<>(PyBuffer_FromMemory((void*)data, 6)));
273 } // namespace unnamed
275 void bind_peer_plugin()
277 class_<
278 peer_plugin_wrap, boost::shared_ptr<peer_plugin_wrap>, boost::noncopyable
279 >("peer_plugin")
280 .def(
281 "add_handshake"
282 , &peer_plugin::add_handshake, &peer_plugin_wrap::default_add_handshake
284 .def(
285 "on_handshake"
286 , &peer_plugin::on_handshake, &peer_plugin_wrap::default_on_handshake
288 .def(
289 "on_extension_handshake"
290 , &peer_plugin::on_extension_handshake
291 , &peer_plugin_wrap::default_on_extension_handshake
293 .def(
294 "on_choke"
295 , &peer_plugin::on_choke, &peer_plugin_wrap::default_on_choke
297 .def(
298 "on_unchoke"
299 , &peer_plugin::on_unchoke, &peer_plugin_wrap::default_on_unchoke
301 .def(
302 "on_interested"
303 , &peer_plugin::on_interested, &peer_plugin_wrap::default_on_interested
305 .def(
306 "on_not_interested"
307 , &peer_plugin::on_not_interested, &peer_plugin_wrap::default_on_not_interested
309 .def(
310 "on_have"
311 , &peer_plugin::on_have, &peer_plugin_wrap::default_on_have
313 .def(
314 "on_bitfield"
315 , &peer_plugin::on_bitfield, &peer_plugin_wrap::default_on_bitfield
317 .def(
318 "on_request"
319 , &peer_plugin::on_request, &peer_plugin_wrap::default_on_request
321 .def(
322 "on_piece"
323 , &peer_plugin::on_piece, &peer_plugin_wrap::default_on_piece
325 .def(
326 "on_cancel"
327 , &peer_plugin::on_cancel, &peer_plugin_wrap::default_on_cancel
329 .def(
330 "on_piece_pass"
331 , &peer_plugin::on_piece_pass, &peer_plugin_wrap::default_on_piece_pass
333 .def(
334 "on_piece_failed"
335 , &peer_plugin::on_piece_failed, &peer_plugin_wrap::default_on_piece_failed
337 .def(
338 "tick"
339 , &peer_plugin::tick, &peer_plugin_wrap::default_tick
341 .def(
342 "write_request"
343 , &peer_plugin::write_request, &peer_plugin_wrap::default_write_request
345 // These seem to make VC7.1 freeze. Needs special handling.
347 /*.def(
348 "on_extended"
349 , &peer_plugin::on_extended, &peer_plugin_wrap::default_on_extended
351 .def(
352 "on_unknown_message"
353 , &peer_plugin::on_unknown_message, &peer_plugin_wrap::default_on_unknown_message
357 def("get_buffer", &get_buffer);