summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire/Selection.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Selection.hs')
-rw-r--r--src/Network/BitTorrent/PeerWire/Selection.hs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Selection.hs b/src/Network/BitTorrent/PeerWire/Selection.hs
new file mode 100644
index 00000000..04049812
--- /dev/null
+++ b/src/Network/BitTorrent/PeerWire/Selection.hs
@@ -0,0 +1,64 @@
1-- TODO tests
2-- |
3-- Copyright : (c) Sam T. 2013
4-- License : MIT
5-- Maintainer : pxqr.sta@gmail.com
6-- Stability : experimental
7-- Portability : portable
8--
9-- This module provides commonly used piece seletion algorithms
10-- which used to find out which one next piece to download.
11-- Selectors considered to be used in the following order:
12--
13-- * Random first or rarest first selection - at the start.
14--
15-- * Rarest first selection - performed to avoid situation when
16-- rarest piece is unaccessible.
17--
18-- * _End game_ seletion - performed after a peer has requested all
19-- the subpieces of the content.
20--
21-- Note that BitTorrent applies the strict priority policy for
22-- _subpiece_ or _blocks_ selection.
23--
24module Network.BitTorrent.PeerWire.Selection
25 ( Selector
26 , strictFirst, rarestFirst, randomFirst, endGame, autoSelector
27 ) where
28
29import Network.BitTorrent.PeerWire.Block
30import Network.BitTorrent.PeerWire.Message
31import Network.BitTorrent.PeerWire.Bitfield
32
33
34type Selector = Bitfield -- ^ Indices of client "have" pieces.
35 -> Bitfield -- ^ Indices of peer "have" pieces.
36 -> [Bitfield] -- ^ Indices of other peers "have" pieces.
37 -> Maybe PieceIx -- ^ Zero-based index of piece to request
38 -- to, if any.
39
40-- | Select the first available piece.
41strictFirst :: Selector
42strictFirst h a _ = findMin (difference a h)
43
44
45-- |
46rarestFirst :: Selector
47rarestFirst h a xs = error "rarestFirst"
48 -- rarest (frequencies (map (intersection want) xs))
49 where
50 want = difference h a
51 rarest = undefined
52
53-- | In general random first is faster than rarest first strategy but
54-- only if all pieces are available.
55randomFirst :: IO Selector
56randomFirst = do
57-- randomIO
58 error "randomFirst"
59
60endGame :: Selector
61endGame = undefined
62
63autoSelector :: Selector
64autoSelector = undefined