Creating a custom PiecePicker

Version 3 (Alan McGovern, 10/19/2009 09:19 AM)

1 1
h1. Creating a custom PiecePicker
2 2 Alan McGovern
3 3 Alan McGovern
[[Creating a custom PiecePicker#Example-1|Example 1]] Creating a picker to prioritise the first piece in a file
4 1
5 1
h2. Example 1
6 1
7 1
<pre><code class="java">
8 1
public class MyPicker : PiecePicker {
9 1
    public MyPicker (PiecePicker basePicker)
10 1
        : base (basePicker)
11 1
    {
12 1
13 1
    }
14 1
15 1
    public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
16 1
    {
17 1
    // If we do not have the first piece and the other peer does have it
18 1
        if (!HaveFirstPieceAlready && peerBitfield[0] == true) {
19 1
            BitField bf = new BitField (peerBitfield.Length);
20 1
            bf [0] = true;
21 1
            MessageBundle msg = base.PickPiece (id, bf, otherPeers, count, startIndex, endIndex);
22 1
            if (msg != null)
23 1
                return msg;
24 1
        }
25 1
        return base.PickPiece (id, peerBitfield, otherPeers, count, startIndex, endIndex);
26 1
    }
27 1
}
28 1
29 1
// This picker can then be added to the default picking pipeline by doing:
30 1
torrentManager.ChangePicker (new MyPicker (torrentManager.CreateDefaultPicker ()));
31 1
</code></pre>