From: Ramakrishnan Muthukrishnan Date: Thu, 3 Sep 2015 12:34:11 +0000 (+0530) Subject: Peer: more refactoring X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/biz?a=commitdiff_plain;h=144c253adea364bdd52027f484020eeafafa20f6;p=functorrent.git Peer: more refactoring --- 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