summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-07 19:14:41 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-07 19:14:41 +0400
commit13819af69d4a99f97eaf8ae439baeb6de37a1f5f (patch)
tree25710835624effca5d87a633cecdfbc3667e9836 /src/Network/BitTorrent/Tracker.hs
parent7f299646b8c761f28b101b6232cc183712dcfa2e (diff)
~ Throw exception in askTracker.
HTTP client functions throws an exception. If we unable to decode result we return BEncode.Result.Left. So user of this function should check both kinds of errors and this complicate usage. Instead of this we throw IOException too.
Diffstat (limited to 'src/Network/BitTorrent/Tracker.hs')
-rw-r--r--src/Network/BitTorrent/Tracker.hs43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs
index e4bcf2cd..2319a551 100644
--- a/src/Network/BitTorrent/Tracker.hs
+++ b/src/Network/BitTorrent/Tracker.hs
@@ -8,6 +8,7 @@
8-- This module provides high level API for peer->tracker 8-- This module provides high level API for peer->tracker
9-- communication. 9-- communication.
10-- 10--
11{-# LANGUAGE RecordWildCards #-}
11module Network.BitTorrent.Tracker 12module Network.BitTorrent.Tracker
12 ( module Network.BitTorrent.Tracker.Scrape 13 ( module Network.BitTorrent.Tracker.Scrape
13 14
@@ -155,28 +156,26 @@ withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a
155withTracker initProgress conn action = bracket start end (action . fst) 156withTracker initProgress conn action = bracket start end (action . fst)
156 where 157 where
157 start = do 158 start = do
158 res <- sendRequest (startedReq conn initProgress) 159 resp <- askTracker (startedReq conn initProgress)
159 case res of 160 se <- newSession initProgress (respInterval resp) (respPeers resp)
160 Left err -> ioError (userError err) 161 tid <- forkIO (syncSession se)
161 Right (Failure err) -> ioError (userError (show err)) 162 return (se, tid)
162 Right resp -> do 163
163 se <- newSession initProgress (respInterval resp) (respPeers resp) 164 syncSession se @ TSession {..} = forever $ do
164 tid <- forkIO (syncSession se) 165 waitInterval se
165 return (se, tid) 166 pr <- getProgress se
166 167 resp <- tryJust isIOException $ do
167 168 askTracker (regularReq defaultNumWant conn pr)
168 syncSession se = do 169 case resp of
169 waitInterval se 170 Right (OK {..}) -> do
170 pr <- getProgress se 171 writeIORef seInterval respInterval
171 eresp <- sendRequest (regularReq defaultNumWant conn pr) 172 atomically $ writeTVar sePeers respPeers
172 case eresp of 173 _ -> return ()
173 Right (OK { respInterval = i, respPeers = ps }) -> do 174 where
174 writeIORef (seInterval se) i 175 isIOException :: IOException -> Maybe IOException
175 atomically $ writeTVar (sePeers se) ps 176 isIOException = return
176 _ -> return ()
177 syncSession se
178
179 177
180 end (se, tid) = do 178 end (se, tid) = do
181 killThread tid 179 killThread tid
182 getProgress se >>= sendRequest . stoppedReq conn 180 pr <- getProgress se
181 askTracker $ stoppedReq conn pr