From: Ramakrishnan Muthukrishnan Date: Fri, 24 Jul 2015 04:42:39 +0000 (+0530) Subject: more refactoring around msgLoop X-Git-Url: https://git.rkrishnan.org/pf/components/com_hotproperty//%22file:/%22?a=commitdiff_plain;h=fad4b6b198b9f17602d272b4b6757909e3c34c8b;p=functorrent.git more refactoring around msgLoop --- 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