From 144c253adea364bdd52027f484020eeafafa20f6 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Thu, 3 Sep 2015 18:04:11 +0530 Subject: [PATCH] Peer: more refactoring --- src/FuncTorrent/Peer.hs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 65372d9..99906b0 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -84,7 +84,13 @@ bitfieldToList bs = go bs 0 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 @@ -173,16 +179,15 @@ handlePeerMsgs p m peerId = do 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 -- 2.45.2