From 6b81eef44adef685430b0d3ad58062e05171e54d Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Tue, 15 Sep 2015 17:37:44 +0530
Subject: [PATCH] refactor mkPieceMap -> initPieceMap

---
 src/FuncTorrent/Peer.hs | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 2db5706..61b5112 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -49,14 +49,17 @@ type PieceMap = Map Integer PieceData
 
 -- Make the initial Piece map, with the assumption that no peer has the
 -- piece and that every piece is pending download.
-mkPieceMap :: Integer -> ByteString -> [Integer] -> PieceMap
-mkPieceMap numPieces pieceHash pLengths = fromList kvs
-  where kvs = [(i, PieceData { peers = []
-                             , dlstate = Pending
-                             , hash = h
-                             , len = pLen })
-              | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
-        hashes = splitN 20 pieceHash
+initPieceMap :: ByteString  -> Integer -> Integer -> PieceMap
+initPieceMap pieceHash fileLen pieceLen = fromList kvs
+  where
+    numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
+    kvs = [(i, PieceData { peers = []
+                         , dlstate = Pending
+                         , hash = h
+                         , len = pLen })
+          | (i, h, pLen) <- zip3 [0..numPieces] hashes pLengths]
+    hashes = splitN 20 pieceHash
+    pLengths = (splitNum fileLen pieceLen)
 
 havePiece :: PieceMap -> Integer -> Bool
 havePiece pm index =
@@ -123,11 +126,10 @@ handlePeerMsgs p peerId m = do
   doHandshake h p (infoHash m) peerId
   let pstate = toPeerState h p False False True True
       pieceHash = pieces (info m)
-      numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash
       pLen = pieceLength (info m)
       fileLen = lengthInBytes (info m)
       fileName = name (info m)
-      pieceStatus = mkPieceMap numPieces pieceHash (splitNum fileLen pLen)
+      pieceStatus = initPieceMap pieceHash fileLen pLen
   createDummyFile fileName (fromIntegral fileLen)
   _ <- runStateT (msgLoop pieceStatus fileName) pstate
   return ()
-- 
2.45.2