import Control.Applicative ((<$>), liftA3)
import Data.Bits
import Data.Word (Word8)
-import Data.Map (Map(..), fromList, (!), mapWithKey)
+import Data.Map (Map(..), fromList, toList, (!), mapWithKey)
import FuncTorrent.Metainfo (Info(..), Metainfo(..))
import FuncTorrent.Utils (splitN)
_ -> do
msgLoop state pieceStatus
+-- simple algorithm to pick piece.
+-- pick the first piece from 0 that is not downloaded yet.
+pickPiece :: PieceMap -> Maybe Integer
+pickPiece m =
+ let pieceList = toList m
+ allPending = filter (\(k, v) -> state v == Pending) pieceList
+ in
+ case allPending of
+ [] -> Nothing
+ ((i, _):_) -> Just i
+
updatePieceAvailability :: PieceMap -> Peer -> [Integer] -> PieceMap
updatePieceAvailability pieceStatus p pieceList =
mapWithKey (\k pd -> if k `elem` pieceList