Just workPiece -> do
let pLen = len (pieceStatus ! workPiece)
_ <- downloadPiece (handle pState) workPiece pLen
- -- sendMsg (handle state) (RequestMsg workPiece 0 pLen)
- -- putStrLn $ "--> RequestMsg for Piece " ++ (show workPiece) ++ "to peer: " ++ show (peer state) ++ " of length: " ++ show pLen
- -- msg <- getMsg (handle state)
- -- putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer state)
msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
| otherwise = do
msg <- getMsg (handle pState)
pieceStatus = mkPieceMap numPieces pieceHash (splitNum fileLen pLen)
msgLoop state pieceStatus
-downloadPiece :: Handle -> Integer -> Integer -> IO [PeerMsg]
+downloadPiece :: Handle -> Integer -> Integer -> IO [ByteString]
downloadPiece h index pieceLength = do
let chunks = splitNum pieceLength 16384
forM (zip [0..] chunks) (\(i, pLen) -> do
putStrLn $ "--> " ++ "RequestMsg for Piece "
++ (show index) ++ ", part: " ++ show i ++ " of length: "
++ show pLen
- rMsg <- getMsg h
- case rMsg of
- PieceMsg index begin block ->
- putStrLn $ " <-- PieceMsg for Piece: "
- ++ show index
- ++ ", offset: "
- ++ show begin
- _ -> putStrLn " <-- UnKnown msg from Peer"
- return rMsg)
+ PieceMsg index begin block <- getMsg h
+ putStrLn $ " <-- PieceMsg for Piece: "
+ ++ show index
+ ++ ", offset: "
+ ++ show begin
+ return block)