Creating a custom PiecePicker

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

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