setBits ++ go bs' (pos + 1)
-- helper functions to manipulate PeerState
-toPeerState :: Handle -> Peer -> Bool -> Bool -> Bool -> Bool -> PState
+toPeerState :: Handle
+ -> Peer
+ -> Bool -- ^ meChoking
+ -> Bool -- ^ meInterested
+ -> Bool -- ^ heChoking
+ -> Bool -- ^ heInterested
+ -> PState
toPeerState h p meCh meIn heCh heIn =
PState { handle = h
, peer = p
return ()
msgLoop :: PieceMap -> FilePath -> StateT PState IO ()
-msgLoop pieceStatus file =
- StateT(\pState -> do
- let h = handle pState
- msg <- getMsg h
- liftIO $ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState)
- case msg of
- KeepAliveMsg -> do
- sendMsg h KeepAliveMsg
- liftIO $ putStrLn $ "--> " ++ "KeepAliveMsg to peer: " ++ show (peer pState)
- runStateT (msgLoop pieceStatus file) pState)
+msgLoop pieceStatus file = do
+ h <- gets handle
+ msg <- liftIO $ getMsg h
+ gets peer >>= (\p -> liftIO $ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show p)
+ case msg of
+ KeepAliveMsg -> do
+ liftIO $ sendMsg h KeepAliveMsg
+ gets peer >>= (\p -> liftIO $ putStrLn $ "--> " ++ "KeepAliveMsg to peer: " ++ show p)
+ msgLoop pieceStatus file
downloadPiece :: Handle -> Integer -> Integer -> IO ByteString
downloadPiece h index pieceLength = do