diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/PeerWire.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Selection.hs | 64 |
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 #-} |
9 | module Network.BitTorrent.PeerWire (module PW) where | 9 | module Network.BitTorrent.PeerWire (module PW) where |
10 | 10 | ||
11 | import Network.BitTorrent.PeerWire.Bitfield as PW | ||
12 | import Network.BitTorrent.PeerWire.Block as PW | 11 | import Network.BitTorrent.PeerWire.Block as PW |
12 | import Network.BitTorrent.PeerWire.Bitfield as PW | ||
13 | import Network.BitTorrent.PeerWire.Selection as PW | ||
13 | import Network.BitTorrent.PeerWire.Message as PW | 14 | import Network.BitTorrent.PeerWire.Message as PW |
14 | import Network.BitTorrent.PeerWire.Handshake as PW | 15 | import 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 | -- | ||
24 | module Network.BitTorrent.PeerWire.Selection | ||
25 | ( Selector | ||
26 | , strictFirst, rarestFirst, randomFirst, endGame, autoSelector | ||
27 | ) where | ||
28 | |||
29 | import Network.BitTorrent.PeerWire.Block | ||
30 | import Network.BitTorrent.PeerWire.Message | ||
31 | import Network.BitTorrent.PeerWire.Bitfield | ||
32 | |||
33 | |||
34 | type 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. | ||
41 | strictFirst :: Selector | ||
42 | strictFirst h a _ = findMin (difference a h) | ||
43 | |||
44 | |||
45 | -- | | ||
46 | rarestFirst :: Selector | ||
47 | rarestFirst 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. | ||
55 | randomFirst :: IO Selector | ||
56 | randomFirst = do | ||
57 | -- randomIO | ||
58 | error "randomFirst" | ||
59 | |||
60 | endGame :: Selector | ||
61 | endGame = undefined | ||
62 | |||
63 | autoSelector :: Selector | ||
64 | autoSelector = undefined | ||