From 08039c1a7bad8f6a6f14110e1853552b5c5bf618 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 24 Jul 2015 19:49:32 +0530
Subject: [PATCH] return a list of bytestring blocks corresponding to the piece
 index

---
 src/FuncTorrent/Peer.hs | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index d74333a..b9138b7 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -208,10 +208,6 @@ msgLoop pState pieceStatus | meInterested pState == False &&
                                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)
@@ -267,7 +263,7 @@ handlePeerMsgs p m peerId = do
       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
@@ -275,12 +271,9 @@ downloadPiece h index pieceLength = 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)
-- 
2.45.2