~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to libtorrent/bindings/python/src/peer_plugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
4
 
 
5
 
#include <cctype>
6
 
#include <iostream>
7
 
 
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>
15
 
 
16
 
using namespace boost::python;
17
 
using namespace libtorrent;
18
 
 
19
 
namespace 
20
 
{
21
 
  struct peer_plugin_wrap : peer_plugin, wrapper<peer_plugin>
22
 
  {
23
 
      void add_handshake(entry& e)
24
 
      {
25
 
          if (override f = this->get_override("add_handshake"))
26
 
              e = call<entry>(f.ptr(), e);
27
 
          else
28
 
              peer_plugin::add_handshake(e);
29
 
      }
30
 
 
31
 
      void default_add_handshake(entry& e)
32
 
      {
33
 
          this->peer_plugin::add_handshake(e);
34
 
      }
35
 
 
36
 
      bool on_handshake(char const* reserved_bits)
37
 
      {
38
 
          if (override f = this->get_override("on_handshake"))
39
 
              return f();
40
 
          else
41
 
              return peer_plugin::on_handshake(reserved_bits);
42
 
      }
43
 
 
44
 
      bool default_on_handshake(char const* reserved_bits)
45
 
      {
46
 
          return this->peer_plugin::on_handshake(reserved_bits);
47
 
      }
48
 
 
49
 
      bool on_extension_handshake(lazy_entry const& e)
50
 
      {
51
 
          if (override f = this->get_override("on_extension_handshake"))
52
 
              return f(e);
53
 
          else
54
 
              return peer_plugin::on_extension_handshake(e);
55
 
      }
56
 
 
57
 
      bool default_on_extension_handshake(lazy_entry const& e)
58
 
      {
59
 
          return this->peer_plugin::on_extension_handshake(e);
60
 
      }
61
 
 
62
 
      bool on_choke()
63
 
      {
64
 
          if (override f = this->get_override("on_choke"))
65
 
              return f();
66
 
          else
67
 
              return peer_plugin::on_choke();
68
 
      }
69
 
 
70
 
      bool default_on_choke()
71
 
      {
72
 
          return this->peer_plugin::on_choke();
73
 
      }
74
 
 
75
 
      bool on_unchoke()
76
 
      {
77
 
          if (override f = this->get_override("on_unchoke"))
78
 
              return f();
79
 
          else
80
 
              return peer_plugin::on_unchoke();
81
 
      }
82
 
 
83
 
      bool default_on_unchoke()
84
 
      {
85
 
          return this->peer_plugin::on_unchoke();
86
 
      }
87
 
 
88
 
      bool on_interested()
89
 
      {
90
 
          if (override f = this->get_override("on_interested"))
91
 
              return f();
92
 
          else
93
 
              return peer_plugin::on_interested();
94
 
      }
95
 
 
96
 
      bool default_on_interested()
97
 
      {
98
 
          return this->peer_plugin::on_interested();
99
 
      }
100
 
 
101
 
      bool on_not_interested()
102
 
      {
103
 
          if (override f = this->get_override("on_not_interested"))
104
 
              return f();
105
 
          else
106
 
              return peer_plugin::on_not_interested();
107
 
      }
108
 
 
109
 
      bool default_on_not_interested()
110
 
      {
111
 
          return this->peer_plugin::on_not_interested();
112
 
      }
113
 
 
114
 
      bool on_have(int index)
115
 
      {
116
 
          if (override f = this->get_override("on_have"))
117
 
              return f(index);
118
 
          else
119
 
              return peer_plugin::on_have(index);
120
 
      }
121
 
 
122
 
      bool default_on_have(int index)
123
 
      {
124
 
          return this->peer_plugin::on_have(index);
125
 
      }
126
 
 
127
 
      bool on_bitfield(list _bf)
128
 
      {
129
 
          //Convert list to a bitfield
130
 
          bitfield bf(len(_bf));
131
 
          for (int i = 0; i < len(_bf); ++i)
132
 
          {
133
 
              if (_bf[i])
134
 
                  bf.set_bit(i);
135
 
              else
136
 
                  bf.clear_bit(i);
137
 
          }   
138
 
          if (override f = this->get_override("on_bitfield"))
139
 
              return f(bf);
140
 
          else
141
 
              return peer_plugin::on_bitfield(bf);
142
 
      }
143
 
 
144
 
      bool default_on_bitfield(const bitfield &bf)
145
 
      {
146
 
          return this->peer_plugin::on_bitfield(bf);
147
 
      }
148
 
 
149
 
      bool on_request(peer_request const& req)
150
 
      {
151
 
          if (override f = this->get_override("on_request"))
152
 
              return f(req);
153
 
          else
154
 
              return peer_plugin::on_request(req);
155
 
      }
156
 
 
157
 
      bool default_on_request(peer_request const& req)
158
 
      {
159
 
          return this->peer_plugin::on_request(req);
160
 
      }
161
 
 
162
 
      bool on_piece(peer_request const& piece, disk_buffer_holder& data)
163
 
      {
164
 
          if (override f = this->get_override("on_piece"))
165
 
              return f(piece, data);
166
 
          else
167
 
              return peer_plugin::on_piece(piece, data);
168
 
      }
169
 
 
170
 
      bool default_on_piece(peer_request const& piece, disk_buffer_holder& data)
171
 
      {
172
 
          return this->peer_plugin::on_piece(piece, data);
173
 
      }
174
 
 
175
 
      bool on_cancel(peer_request const& req)
176
 
      {
177
 
          if (override f = this->get_override("on_cancel"))
178
 
              return f(req);
179
 
          else
180
 
              return peer_plugin::on_cancel(req);
181
 
      }
182
 
 
183
 
      bool default_on_cancel(peer_request const& req)
184
 
      {
185
 
          return this->peer_plugin::on_cancel(req);
186
 
      }
187
 
 
188
 
      bool on_extended(int length, int msg, buffer::const_interval body)
189
 
      {
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);
194
 
      }
195
 
 
196
 
      bool default_on_extended(int length, int msg, buffer::const_interval body)
197
 
      {
198
 
          return this->peer_plugin::on_extended(length, msg, body);
199
 
      }
200
 
 
201
 
      bool on_unknown_message(int length, int msg, buffer::const_interval body)
202
 
      {
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);
207
 
      }
208
 
 
209
 
      bool default_on_unknown_message(int length, int msg, buffer::const_interval body)
210
 
      {
211
 
          return this->peer_plugin::on_unknown_message(length, msg, body);
212
 
      }
213
 
 
214
 
      void on_piece_pass(int index)
215
 
      {
216
 
          if (override f = this->get_override("on_piece_pass"))
217
 
              f(index);
218
 
          else
219
 
              peer_plugin::on_piece_pass(index);
220
 
      }
221
 
 
222
 
      void default_on_piece_pass(int index)
223
 
      {
224
 
          this->peer_plugin::on_piece_pass(index);
225
 
      }
226
 
 
227
 
      void on_piece_failed(int index)
228
 
      {
229
 
          if (override f = this->get_override("on_piece_failed"))
230
 
              f(index);
231
 
          else
232
 
              peer_plugin::on_piece_failed(index);
233
 
      }
234
 
 
235
 
      void default_on_piece_failed(int index)
236
 
      {
237
 
          this->peer_plugin::on_piece_failed(index);
238
 
      }
239
 
 
240
 
      void tick()
241
 
      {
242
 
          if (override f = this->get_override("tick"))
243
 
              f();
244
 
          else
245
 
              peer_plugin::tick();
246
 
      }
247
 
 
248
 
      void default_tick()
249
 
      {
250
 
          this->peer_plugin::tick();
251
 
      }
252
 
 
253
 
      bool write_request(peer_request const& req)
254
 
      {
255
 
          if (override f = this->get_override("write_request"))
256
 
              return f(req);
257
 
          else
258
 
              return peer_plugin::write_request(req);
259
 
      }
260
 
 
261
 
      bool default_write_request(peer_request const& req)
262
 
      {
263
 
          return this->peer_plugin::write_request(req);
264
 
      }
265
 
  };
266
 
 
267
 
  object get_buffer()
268
 
  {
269
 
      static char const data[] = "foobar";
270
 
      return object(handle<>(PyBuffer_FromMemory((void*)data, 6)));
271
 
  }
272
 
 
273
 
} // namespace unnamed
274
 
 
275
 
void bind_peer_plugin()
276
 
{
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
283
 
        )
284
 
        .def(
285
 
            "on_handshake"
286
 
          , &peer_plugin::on_handshake, &peer_plugin_wrap::default_on_handshake
287
 
        )
288
 
        .def(
289
 
            "on_extension_handshake"
290
 
          , &peer_plugin::on_extension_handshake
291
 
          , &peer_plugin_wrap::default_on_extension_handshake
292
 
        )
293
 
        .def(
294
 
            "on_choke"
295
 
          , &peer_plugin::on_choke, &peer_plugin_wrap::default_on_choke
296
 
        )
297
 
        .def(
298
 
            "on_unchoke"
299
 
          , &peer_plugin::on_unchoke, &peer_plugin_wrap::default_on_unchoke
300
 
        )
301
 
        .def(
302
 
            "on_interested"
303
 
          , &peer_plugin::on_interested, &peer_plugin_wrap::default_on_interested
304
 
        )
305
 
        .def(
306
 
            "on_not_interested"
307
 
          , &peer_plugin::on_not_interested, &peer_plugin_wrap::default_on_not_interested
308
 
        )
309
 
        .def(
310
 
            "on_have"
311
 
          , &peer_plugin::on_have, &peer_plugin_wrap::default_on_have
312
 
        )
313
 
        .def(
314
 
            "on_bitfield"
315
 
          , &peer_plugin::on_bitfield, &peer_plugin_wrap::default_on_bitfield
316
 
        )
317
 
        .def(
318
 
            "on_request"
319
 
          , &peer_plugin::on_request, &peer_plugin_wrap::default_on_request
320
 
        )
321
 
        .def(
322
 
            "on_piece"
323
 
          , &peer_plugin::on_piece, &peer_plugin_wrap::default_on_piece
324
 
        )
325
 
        .def(
326
 
            "on_cancel"
327
 
          , &peer_plugin::on_cancel, &peer_plugin_wrap::default_on_cancel
328
 
        )
329
 
        .def(
330
 
            "on_piece_pass"
331
 
          , &peer_plugin::on_piece_pass, &peer_plugin_wrap::default_on_piece_pass
332
 
        )
333
 
        .def(
334
 
            "on_piece_failed"
335
 
          , &peer_plugin::on_piece_failed, &peer_plugin_wrap::default_on_piece_failed
336
 
        )
337
 
        .def(
338
 
            "tick"
339
 
          , &peer_plugin::tick, &peer_plugin_wrap::default_tick
340
 
        )
341
 
        .def(
342
 
            "write_request"
343
 
          , &peer_plugin::write_request, &peer_plugin_wrap::default_write_request
344
 
        )
345
 
        // These seem to make VC7.1 freeze. Needs special handling.
346
 
        
347
 
        /*.def(
348
 
            "on_extended"
349
 
          , &peer_plugin::on_extended, &peer_plugin_wrap::default_on_extended
350
 
        )
351
 
        .def(
352
 
            "on_unknown_message"
353
 
          , &peer_plugin::on_unknown_message, &peer_plugin_wrap::default_on_unknown_message
354
 
        )*/
355
 
        ;
356
 
 
357
 
    def("get_buffer", &get_buffer);
358
 
}
359