From bf3005952658130aaa83d8e8678c6ea7b36e45cb Mon Sep 17 00:00:00 2001 From: Sam T Date: Sun, 5 May 2013 02:06:54 +0400 Subject: + Add Selection module skeleton. --- src/Network/BitTorrent/PeerWire/Selection.hs | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Network/BitTorrent/PeerWire/Selection.hs (limited to 'src/Network/BitTorrent/PeerWire') 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 @@ +-- TODO tests +-- | +-- Copyright : (c) Sam T. 2013 +-- License : MIT +-- Maintainer : pxqr.sta@gmail.com +-- Stability : experimental +-- Portability : portable +-- +-- This module provides commonly used piece seletion algorithms +-- which used to find out which one next piece to download. +-- Selectors considered to be used in the following order: +-- +-- * Random first or rarest first selection - at the start. +-- +-- * Rarest first selection - performed to avoid situation when +-- rarest piece is unaccessible. +-- +-- * _End game_ seletion - performed after a peer has requested all +-- the subpieces of the content. +-- +-- Note that BitTorrent applies the strict priority policy for +-- _subpiece_ or _blocks_ selection. +-- +module Network.BitTorrent.PeerWire.Selection + ( Selector + , strictFirst, rarestFirst, randomFirst, endGame, autoSelector + ) where + +import Network.BitTorrent.PeerWire.Block +import Network.BitTorrent.PeerWire.Message +import Network.BitTorrent.PeerWire.Bitfield + + +type Selector = Bitfield -- ^ Indices of client "have" pieces. + -> Bitfield -- ^ Indices of peer "have" pieces. + -> [Bitfield] -- ^ Indices of other peers "have" pieces. + -> Maybe PieceIx -- ^ Zero-based index of piece to request + -- to, if any. + +-- | Select the first available piece. +strictFirst :: Selector +strictFirst h a _ = findMin (difference a h) + + +-- | +rarestFirst :: Selector +rarestFirst h a xs = error "rarestFirst" + -- rarest (frequencies (map (intersection want) xs)) + where + want = difference h a + rarest = undefined + +-- | In general random first is faster than rarest first strategy but +-- only if all pieces are available. +randomFirst :: IO Selector +randomFirst = do +-- randomIO + error "randomFirst" + +endGame :: Selector +endGame = undefined + +autoSelector :: Selector +autoSelector = undefined -- cgit v1.2.3