From: Ramakrishnan Muthukrishnan Date: Sat, 5 Sep 2015 17:11:47 +0000 (+0530) Subject: Peer: function to calculate the total bytes downloaded so far X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/somewhere?a=commitdiff_plain;h=2c496b1725eadb4c1811dbca3fce9527354f590a;p=functorrent.git Peer: function to calculate the total bytes downloaded so far --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 0b9ece7..9ca40f4 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -36,7 +36,7 @@ data PieceDlState = Pending | Have deriving (Show, Eq) --- todo - map with index to a new data structure (peers who have that piece amd state) +-- todo - map with index to a new data structure (peers who have that piece and state) data PieceData = PieceData { peers :: [Peer] -- ^ list of peers who have this piece , dlstate :: PieceDlState -- ^ state of the piece from download perspective. , hash :: ByteString -- ^ piece hash @@ -106,6 +106,10 @@ pickPiece :: PieceMap -> Maybe Integer pickPiece = (fst `liftM`) . headMay . toList . filter (\v -> dlstate v == Pending) +bytesDownloaded :: PieceMap -> Integer +bytesDownloaded = + sum . (map (len . snd)) . toList . filter (\v -> dlstate v == Have) + updatePieceAvailability :: PieceMap -> Peer -> [Integer] -> PieceMap updatePieceAvailability pieceStatus p pieceList = mapWithKey (\k pd -> if k `elem` pieceList