]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Peer.hs
bugfix: use the previous piece length times number of pieces to calculate offset
[functorrent.git] / src / FuncTorrent / Peer.hs
index 62090175fc193dae18ffb39933834fd107e83c1e..9984a2ac2ed979df480dd25f26c6515e02f809e8 100644 (file)
@@ -76,7 +76,7 @@ mkPieceMap numPieces pieceHash pLengths = fromList kvs
                              , 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 =
@@ -214,13 +214,15 @@ msgLoop pState pieceStatus | meInterested pState == False &&
                                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)