-- 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
, 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