From: Ramakrishnan Muthukrishnan Date: Tue, 15 Sep 2015 12:07:44 +0000 (+0530) Subject: refactor mkPieceMap -> initPieceMap X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/about/provisioning?a=commitdiff_plain;h=6b81eef44adef685430b0d3ad58062e05171e54d;p=functorrent.git refactor mkPieceMap -> initPieceMap --- 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 ()