diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-12 07:59:41 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-12 07:59:41 +0400 |
commit | 69c24795715ecb871433bed46e0f43b777b3ca25 (patch) | |
tree | bffb7b74f1d88fe25da7382996ed3a2460155949 /src/Network | |
parent | 5c3c114e0e84339f88892e08010bd8b1408431d1 (diff) |
~ Fix bugs in tracker communication.
* Fix tracker interval timeout: threadDelay use argument is microseconds
while tracker response is seconds.
* Fix stopped request: tracker might response with empty body.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Tracker.hs | 13 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Protocol.hs | 17 |
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 | |||
164 | getProgress :: TSession -> IO Progress | 164 | getProgress :: TSession -> IO Progress |
165 | getProgress = readTVarIO . seProgress | 165 | getProgress = readTVarIO . seProgress |
166 | 166 | ||
167 | sec :: Int | ||
168 | sec = 1000 * 1000 | ||
169 | |||
167 | waitInterval :: TSession -> IO () | 170 | waitInterval :: TSession -> IO () |
168 | waitInterval = readIORef . seInterval >=> threadDelay | 171 | waitInterval se @ TSession {..} = do |
172 | delay <- readIORef seInterval | ||
173 | print delay | ||
174 | threadDelay (delay * sec) | ||
169 | 175 | ||
170 | withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a | 176 | withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a |
171 | withTracker initProgress conn action = bracket start end (action . fst) | 177 | withTracker 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 |
23 | module Network.BitTorrent.Tracker.Protocol | 23 | module 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] | |||
217 | defaultNumWant :: Int | 217 | defaultNumWant :: Int |
218 | defaultNumWant = 25 | 218 | defaultNumWant = 25 |
219 | 219 | ||
220 | mkHTTPRequest :: URI -> Request ByteString | ||
221 | mkHTTPRequest 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'. | ||
245 | leaveTracker :: TRequest -> IO () | ||
246 | leaveTracker req = do | ||
247 | let r = mkHTTPRequest (encodeRequest req) | ||
248 | |||
249 | rawResp <- simpleHTTP r | ||
250 | respBody <- getResponseBody rawResp | ||
251 | return () | ||