Creating a custom PiecePicker

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


h1. Creating a custom PiecePicker


[[Tracker Example#Example-1|Example 1]] Creating a picker to prioritise the first piece in a file

h2. Example 1

<pre><code class="java">
public class MyPicker : PiecePicker {
public MyPicker (PiecePicker basePicker)
: base (basePicker)
{

}

public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
{
// If we do not have the first piece and the other peer does have it
if (!HaveFirstPieceAlready && peerBitfield[0] == true) {
BitField bf = new BitField (peerBitfield.Length);
bf [0] = true;
MessageBundle msg = base.PickPiece (id, bf, otherPeers, count, startIndex, endIndex);
if (msg != null)
return msg;
}
return base.PickPiece (id, peerBitfield, otherPeers, count, startIndex, endIndex);
}
}

// This picker can then be added to the default picking pipeline by doing:
torrentManager.ChangePicker (new MyPicker (torrentManager.CreateDefaultPicker ()));
</code></pre>