-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
+msgLoop state pieceStatus = do
+ -- if meInterested and he NOT Choking, pick a piece to download
+ -- and send a requestmsg.
+ let isMeInterested = meInterested state
+ isHeChoking = heChoking state
+ if (isMeInterested && isHeChoking)
+ then
+ do
+ let h = handle state
+ sendMsg h InterestedMsg
+ putStrLn $ "--> InterestedMsg to peer: " ++ show (peer state)
+ msgLoop state pieceStatus
+ else
+ do
+ msg <- getMsg (handle state)
+ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer state)
+ case msg of
+ KeepAliveMsg -> do
+ sendMsg (handle state) KeepAliveMsg
+ msgLoop state pieceStatus
+ 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
+
+ msgLoop state pieceStatus
+ UnChokeMsg -> do
+ msgLoop (state {heChoking = False}) pieceStatus
+ _ -> do
+ msgLoop state pieceStatus