-- Make the initial Piece map, with the assumption that no peer has the
-- piece and that every piece is pending download.
-mkPieceMap :: Integer -> ByteString -> [Integer] -> PieceMap
-mkPieceMap numPieces pieceHash pLengths = fromList kvs
- where kvs = [(i, PieceData { peers = []
- , dlstate = Pending
- , hash = h
- , len = pLen })
- | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
- hashes = splitN 20 pieceHash
+initPieceMap :: ByteString -> Integer -> Integer -> PieceMap
+initPieceMap pieceHash fileLen pieceLen = fromList kvs
+ where
+ numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
+ kvs = [(i, PieceData { peers = []
+ , dlstate = Pending
+ , hash = h
+ , len = pLen })
+ | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
+ hashes = splitN 20 pieceHash
+ pLengths = (splitNum fileLen pieceLen)
havePiece :: PieceMap -> Integer -> Bool
havePiece pm index =
doHandshake h p (infoHash m) peerId
let pstate = toPeerState h p False False True True
pieceHash = pieces (info m)
- numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
pLen = pieceLength (info m)
fileLen = lengthInBytes (info m)
fileName = name (info m)
- pieceStatus = mkPieceMap numPieces pieceHash (splitNum fileLen pLen)
+ pieceStatus = initPieceMap pieceHash fileLen pLen
createDummyFile fileName (fromIntegral fileLen)
_ <- runStateT (msgLoop pieceStatus fileName) pstate
return ()