summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/PeerWire.hs3
-rw-r--r--src/Network/BitTorrent/PeerWire/Selection.hs64
2 files changed, 66 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/PeerWire.hs b/src/Network/BitTorrent/PeerWire.hs
index db9287d8..6d8ff156 100644
--- a/src/Network/BitTorrent/PeerWire.hs
+++ b/src/Network/BitTorrent/PeerWire.hs
@@ -8,7 +8,8 @@
8{-# LANGUAGE DoAndIfThenElse #-} 8{-# LANGUAGE DoAndIfThenElse #-}
9module Network.BitTorrent.PeerWire (module PW) where 9module Network.BitTorrent.PeerWire (module PW) where
10 10
11import Network.BitTorrent.PeerWire.Bitfield as PW
12import Network.BitTorrent.PeerWire.Block as PW 11import Network.BitTorrent.PeerWire.Block as PW
12import Network.BitTorrent.PeerWire.Bitfield as PW
13import Network.BitTorrent.PeerWire.Selection as PW
13import Network.BitTorrent.PeerWire.Message as PW 14import Network.BitTorrent.PeerWire.Message as PW
14import Network.BitTorrent.PeerWire.Handshake as PW 15import Network.BitTorrent.PeerWire.Handshake as PW
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