]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Peer.hs
Peer: export bytesDownloaded function
[functorrent.git] / src / FuncTorrent / Peer.hs
index 0b9ece75bc9cc88db2fc5783255ea5cc103fb436..ae95f838b38215279ca3150a6002a1086caefd1a 100644 (file)
@@ -1,7 +1,8 @@
 {-# LANGUAGE OverloadedStrings #-}
 module FuncTorrent.Peer
     (Peer(..),
-     handlePeerMsgs
+     handlePeerMsgs,
+     bytesDownloaded
     ) where
 
 import Prelude hiding (lookup, concat, replicate, splitAt, take, filter)
@@ -32,11 +33,11 @@ data PState = PState { handle :: Handle
 type PeerState = State PState
 
 data PieceDlState = Pending
-                  | InProgress
+                  | Downloading
                   | 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 +107,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