From fad4b6b198b9f17602d272b4b6757909e3c34c8b Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 24 Jul 2015 10:12:39 +0530
Subject: [PATCH] more refactoring around msgLoop

---
 src/FuncTorrent/Peer.hs | 46 ++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 0d70f9f..605d9ff 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -192,28 +192,25 @@ createDummyFile path size =
 
 -- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData.
 -- recvMsg :: Peer -> Handle -> Msg
-msgLoop :: PeerState -> ByteString -> IO ()
-msgLoop state pieceHash =
-  let numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
-      pieceStatus = mkPieceMap numPieces pieceHash
-  in
-   forever $ do
-     -- if meInterested and he NOT Choking, pick a piece to download
-     -- and send a requestmsg.
-     msg <- getMsg (handle state)
-     putStrLn $ "got a " ++ show msg
-     case msg of
-      BitFieldMsg bss -> do
-        let pieceList = bitfieldToList (unpack bss)
-        print pieceList
-        -- for each pieceIndex in pieceList, make an entry in the pieceStatus
-        -- map with pieceIndex as the key and modify the value to add the peer.
-
-        -- download each of the piece in order
-      UnChokeMsg -> do
-        print msg
-        msgLoop (state {heChoking = False}) pieceHash
-      _ -> print msg
+msgLoop :: PeerState -> PieceMap -> IO ()
+msgLoop state pieceStatus =
+  forever $ do
+    -- if meInterested and he NOT Choking, pick a piece to download
+    -- and send a requestmsg.
+    msg <- getMsg (handle state)
+    putStrLn $ "got a " ++ show msg
+    case msg of
+     BitFieldMsg bss -> do
+       let pieceList = bitfieldToList (unpack bss)
+       print pieceList
+       -- for each pieceIndex in pieceList, make an entry in the pieceStatus
+       -- map with pieceIndex as the key and modify the value to add the peer.
+
+       -- download each of the piece in order
+     UnChokeMsg -> do
+       print msg
+       msgLoop (state {heChoking = False}) pieceStatus
+     _ -> print msg
 
 handlePeerMsgs :: Peer -> Metainfo -> String -> (String -> IO ()) -> IO ()
 handlePeerMsgs p m peerId logFn = do
@@ -225,5 +222,8 @@ handlePeerMsgs p m peerId logFn = do
                         , heChoking = True
                         , meInterested = True
                         , meChoking = False }
-  msgLoop state (pieces (info m))
+      pieceHash = (pieces (info m))
+      numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
+      pieceStatus = mkPieceMap numPieces pieceHash
+  msgLoop state pieceStatus
   
-- 
2.45.2