X-Git-Url: https://git.rkrishnan.org/?a=blobdiff_plain;f=src%2FFuncTorrent%2FPeer.hs;h=6131a6bb58750d6cf2548c9680b81b822ef5f5ab;hb=ebc045715374d3418a0c1466f6ae95252603899c;hp=e4a53495d0949ddda016beaa175914abd79c1a41;hpb=5ad7d5cb250a61b3a889373d86d72a09c3f5ce22;p=functorrent.git diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index e4a5349..6131a6b 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -60,18 +60,18 @@ initPieceMap pieceHash fileLen pieceLen = fromList kvs , len = pLen }) | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths] hashes = splitN 20 pieceHash - pLengths = (splitNum fileLen pieceLen) + pLengths = splitNum fileLen pieceLen pieceMapFromFile :: FilePath -> PieceMap -> IO PieceMap -pieceMapFromFile filePath pieceMap = do +pieceMapFromFile filePath pieceMap = traverseWithKey f pieceMap - where - f k v = do - let offset = if k == 0 then 0 else k * len (pieceMap ! (k - 1)) - isHashValid <- (flip verifyHash) (hash v) <$> (readFileAtOffset filePath offset (len v)) - if isHashValid - then return $ v { dlstate = Have } - else return $ v + where + f k v = do + let offset = if k == 0 then 0 else k * len (pieceMap ! (k - 1)) + isHashValid <- flip verifyHash (hash v) <$> readFileAtOffset filePath offset (len v) + if isHashValid + then return $ v { dlstate = Have } + else return v havePiece :: PieceMap -> Integer -> Bool havePiece pm index = @@ -93,13 +93,13 @@ doHandshake True h peer infoHash peerid = do return () doHandshake False h peer infoHash peerid = do let hs = genHandshakeMsg infoHash peerid - putStrLn $ "waiting for a handshake" + putStrLn "waiting for a handshake" hsMsg <- hGet h (length (unpack hs)) putStrLn $ "<-- handshake from peer: " ++ show peer let rxInfoHash = take 20 $ drop 28 hsMsg if rxInfoHash /= infoHash then do - putStrLn $ "infoHashes does not match" + putStrLn "infoHashes does not match" hClose h return () else do @@ -175,7 +175,7 @@ msgLoop pieceStatus file = do pBS <- liftIO $ downloadPiece h workPiece pLen if not $ verifyHash pBS (hash (pieceStatus ! workPiece)) then - liftIO $ putStrLn $ "Hash mismatch" + liftIO $ putStrLn "Hash mismatch" else do let fileOffset = if workPiece == 0 then 0 else workPiece * len (pieceStatus ! (workPiece - 1)) liftIO $ putStrLn $ "Write into file at offset: " ++ show fileOffset @@ -210,9 +210,9 @@ msgLoop pieceStatus file = do NotInterestedMsg -> do modify (\st -> st {heInterested = False}) msgLoop pieceStatus file - CancelMsg _ _ _ -> do -- check if valid index, begin, length + CancelMsg _ _ _ -> -- check if valid index, begin, length msgLoop pieceStatus file - PortMsg _ -> do + PortMsg _ -> msgLoop pieceStatus file -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here. -- also BitFieldMsg