summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/Tracker.hs13
-rw-r--r--src/Network/BitTorrent/Tracker/Protocol.hs17
2 files changed, 24 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs
index e1f9ff76..6bf6de4b 100644
--- a/src/Network/BitTorrent/Tracker.hs
+++ b/src/Network/BitTorrent/Tracker.hs
@@ -164,8 +164,14 @@ getPeerList = getChanContents . sePeers
164getProgress :: TSession -> IO Progress 164getProgress :: TSession -> IO Progress
165getProgress = readTVarIO . seProgress 165getProgress = readTVarIO . seProgress
166 166
167sec :: Int
168sec = 1000 * 1000
169
167waitInterval :: TSession -> IO () 170waitInterval :: TSession -> IO ()
168waitInterval = readIORef . seInterval >=> threadDelay 171waitInterval se @ TSession {..} = do
172 delay <- readIORef seInterval
173 print delay
174 threadDelay (delay * sec)
169 175
170withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a 176withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a
171withTracker initProgress conn action = bracket start end (action . fst) 177withTracker initProgress conn action = bracket start end (action . fst)
@@ -174,7 +180,7 @@ withTracker initProgress conn action = bracket start end (action . fst)
174 resp <- askTracker (startedReq conn initProgress) 180 resp <- askTracker (startedReq conn initProgress)
175 print resp 181 print resp
176 se <- newSession initProgress (respInterval resp) (respPeers resp) 182 se <- newSession initProgress (respInterval resp) (respPeers resp)
177 tid <- forkIO (syncSession se) 183 tid <- forkIO (return ()) -- (syncSession se)
178 return (se, tid) 184 return (se, tid)
179 185
180 syncSession se @ TSession {..} = forever $ do 186 syncSession se @ TSession {..} = forever $ do
@@ -197,7 +203,8 @@ withTracker initProgress conn action = bracket start end (action . fst)
197 end (se, tid) = do 203 end (se, tid) = do
198 killThread tid 204 killThread tid
199 pr <- getProgress se 205 pr <- getProgress se
200 askTracker $ stoppedReq conn pr 206 print "stopping"
207 leaveTracker $ stoppedReq conn pr
201 208
202 209
203 210
diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs
index 13127e7c..efc709e2 100644
--- a/src/Network/BitTorrent/Tracker/Protocol.hs
+++ b/src/Network/BitTorrent/Tracker/Protocol.hs
@@ -22,7 +22,7 @@
22-- TODO: add "compact" field to TRequest 22-- TODO: add "compact" field to TRequest
23module Network.BitTorrent.Tracker.Protocol 23module Network.BitTorrent.Tracker.Protocol
24 ( Event(..), TRequest(..), TResponse(..) 24 ( Event(..), TRequest(..), TResponse(..)
25 , askTracker 25 , askTracker, leaveTracker
26 26
27 -- * Defaults 27 -- * Defaults
28 , defaultPorts, defaultNumWant 28 , defaultPorts, defaultNumWant
@@ -217,6 +217,9 @@ defaultPorts = [6881..6889]
217defaultNumWant :: Int 217defaultNumWant :: Int
218defaultNumWant = 25 218defaultNumWant = 25
219 219
220mkHTTPRequest :: URI -> Request ByteString
221mkHTTPRequest uri = Request uri GET [] ""
222
220-- | Send request and receive response from the tracker specified in 223-- | Send request and receive response from the tracker specified in
221-- announce list. This function throws 'IOException' if it couldn't 224-- announce list. This function throws 'IOException' if it couldn't
222-- send request or receive response or decode response. 225-- send request or receive response or decode response.
@@ -230,11 +233,19 @@ askTracker req = do
230 print $ respBody 233 print $ respBody
231 checkResult $ decoded respBody 234 checkResult $ decoded respBody
232 where 235 where
233 mkHTTPRequest :: URI -> Request ByteString
234 mkHTTPRequest uri = Request uri GET [] ""
235 236
236 checkResult (Left err) 237 checkResult (Left err)
237 = ioError $ userError $ err ++ " in tracker response" 238 = ioError $ userError $ err ++ " in tracker response"
238 checkResult (Right (Failure err)) 239 checkResult (Right (Failure err))
239 = ioError $ userError $ show err ++ " in tracker response" 240 = ioError $ userError $ show err ++ " in tracker response"
240 checkResult (Right resp) = return resp 241 checkResult (Right resp) = return resp
242
243-- | The same as the 'askTracker' but ignore response. Used in
244-- conjunction with 'Stopped'.
245leaveTracker :: TRequest -> IO ()
246leaveTracker req = do
247 let r = mkHTTPRequest (encodeRequest req)
248
249 rawResp <- simpleHTTP r
250 respBody <- getResponseBody rawResp
251 return ()