, hash = h
, len = pLen })
| (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
- hashes = splitN (fromIntegral numPieces) pieceHash
+ hashes = splitN 20 pieceHash
havePiece :: PieceMap -> Integer -> Bool
havePiece pm index =
Nothing -> putStrLn "Nothing to download"
Just workPiece -> do
let pLen = len (pieceStatus ! workPiece)
+ putStrLn $ "piece length = " ++ show pLen
pBS <- downloadPiece (handle pState) workPiece pLen
- -- if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
- -- then
- -- putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
- -- else do
- writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
- msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
+ if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
+ then
+ putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
+ else do
+ let fileOffset = if workPiece == 0 then 0 else (len (pieceStatus ! (workPiece - 1)))
+ writeFileAtOffset "/tmp/download.file" fileOffset pBS
+ msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
| otherwise = do
msg <- getMsg (handle pState)
putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState)