From 25ffa5dfaae3deca4e6f4a54701b1a89cef388fe Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Tue, 25 Feb 2014 18:14:04 +0400 Subject: Minor refactoring --- src/Network/BitTorrent/Exchange/Session.hs | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/Network/BitTorrent/Exchange/Session.hs') diff --git a/src/Network/BitTorrent/Exchange/Session.hs b/src/Network/BitTorrent/Exchange/Session.hs index 12092473..be8d3835 100644 --- a/src/Network/BitTorrent/Exchange/Session.hs +++ b/src/Network/BitTorrent/Exchange/Session.hs @@ -299,6 +299,13 @@ sendBroadcast msg = do -- | Trigger is the reaction of a handler at some event. type Trigger = Wire Session () +interesting :: Trigger +interesting = do + addr <- asks connRemoteAddr + sendMessage (Interested True) + sendMessage (Choking False) + tryFillRequestQueue + fillRequestQueue :: Trigger fillRequestQueue = do maxN <- lift getMaxQueueLength @@ -315,13 +322,6 @@ tryFillRequestQueue = do when allowed $ do fillRequestQueue -interesting :: Trigger -interesting = do - addr <- asks connRemoteAddr - sendMessage (Interested True) - sendMessage (Choking False) - tryFillRequestQueue - {----------------------------------------------------------------------- -- Incoming message handling -----------------------------------------------------------------------} @@ -383,6 +383,16 @@ handleTransfer (Cancel bix) = filterQueue (not . (transferResponse bix)) -----------------------------------------------------------------------} -- TODO introduce new metadata exchange specific exceptions +waitForMetadata :: Trigger +waitForMetadata = do + Session {..} <- asks connSession + needFetch <- liftIO (isEmptyMVar infodict) + when needFetch $ do + canFetch <- allowed ExtMetadata <$> use connExtCaps + if canFetch + then tryRequestMetadataBlock + else liftIO (waitMVar infodict) + tryRequestMetadataBlock :: Trigger tryRequestMetadataBlock = do mpix <- lift $ withMetadataUpdates Metadata.scheduleBlock @@ -415,18 +425,8 @@ handleMetadata (MetadataReject pix) = do handleMetadata (MetadataUnknown _ ) = do logInfoN "Unknown metadata message" -waitForMetadata :: Trigger -waitForMetadata = do - Session {..} <- asks connSession - needFetch <- liftIO (isEmptyMVar infodict) - when needFetch $ do - canFetch <- allowed ExtMetadata <$> use connExtCaps - if canFetch - then tryRequestMetadataBlock - else liftIO (waitMVar infodict) - {----------------------------------------------------------------------- --- Event loop +-- Main entry point -----------------------------------------------------------------------} acceptRehandshake :: ExtendedHandshake -> Trigger -- cgit v1.2.3