diff options
-rw-r--r-- | src/Network/BitTorrent/Tracker/UDP.hs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/Network/BitTorrent/Tracker/UDP.hs b/src/Network/BitTorrent/Tracker/UDP.hs index 7d413a9c..6143a1e1 100644 --- a/src/Network/BitTorrent/Tracker/UDP.hs +++ b/src/Network/BitTorrent/Tracker/UDP.hs | |||
@@ -19,6 +19,7 @@ module Network.BitTorrent.Tracker.UDP | |||
19 | , putTracker | 19 | , putTracker |
20 | , connectUDP | 20 | , connectUDP |
21 | , freshConnection | 21 | , freshConnection |
22 | , announceUDP | ||
22 | ) where | 23 | ) where |
23 | 24 | ||
24 | import Control.Applicative | 25 | import Control.Applicative |
@@ -30,7 +31,7 @@ import Data.List as L | |||
30 | import Data.Maybe | 31 | import Data.Maybe |
31 | import Data.Monoid | 32 | import Data.Monoid |
32 | import Data.Serialize | 33 | import Data.Serialize |
33 | import Data.Text | 34 | import Data.Text as T |
34 | import Data.Text.Encoding | 35 | import Data.Text.Encoding |
35 | import Data.Time | 36 | import Data.Time |
36 | import Data.Word | 37 | import Data.Word |
@@ -264,7 +265,7 @@ putTracker UDPTracker {..} = do | |||
264 | print trackerURI | 265 | print trackerURI |
265 | print =<< readIORef trackerConnection | 266 | print =<< readIORef trackerConnection |
266 | 267 | ||
267 | transaction :: UDPTracker -> Request -> IO (Transaction Response) | 268 | transaction :: UDPTracker -> Request -> IO Response |
268 | transaction tracker @ UDPTracker {..} request = do | 269 | transaction tracker @ UDPTracker {..} request = do |
269 | cid <- getConnectionId tracker | 270 | cid <- getConnectionId tracker |
270 | tid <- genTransactionId | 271 | tid <- genTransactionId |
@@ -273,16 +274,18 @@ transaction tracker @ UDPTracker {..} request = do | |||
273 | addr <- getTrackerAddr trackerURI | 274 | addr <- getTrackerAddr trackerURI |
274 | res <- call addr (encode trans) | 275 | res <- call addr (encode trans) |
275 | case decode res of | 276 | case decode res of |
276 | Right (responseT @ TransactionR {..}) | 277 | Right (TransactionR {..}) |
277 | | tid == transIdR -> return responseT | 278 | | tid == transIdR -> return response |
278 | | otherwise -> throwIO $ userError "transaction id mismatch" | 279 | | otherwise -> throwIO $ userError "transaction id mismatch" |
279 | Left msg -> throwIO $ userError msg | 280 | Left msg -> throwIO $ userError msg |
280 | 281 | ||
281 | connectUDP :: UDPTracker -> IO ConnectionId | 282 | connectUDP :: UDPTracker -> IO ConnectionId |
282 | connectUDP tracker = do | 283 | connectUDP tracker = do |
283 | TransactionR tid resp <- transaction tracker Connect | 284 | resp <- transaction tracker Connect |
284 | case resp of | 285 | case resp of |
285 | Connected cid -> return cid | 286 | Connected cid -> return cid |
287 | Failed msg -> throwIO $ userError $ T.unpack msg | ||
288 | _ -> throwIO $ userError "message type mismatch" | ||
286 | 289 | ||
287 | initialTracker :: URI -> IO UDPTracker | 290 | initialTracker :: URI -> IO UDPTracker |
288 | initialTracker uri = do | 291 | initialTracker uri = do |
@@ -299,22 +302,19 @@ freshConnection tracker @ UDPTracker {..} = do | |||
299 | connId <- connectUDP tracker | 302 | connId <- connectUDP tracker |
300 | updateConnection connId tracker | 303 | updateConnection connId tracker |
301 | 304 | ||
302 | {- | ||
303 | |||
304 | announceUDP :: UDPTracker -> AnnounceQuery -> IO AnnounceInfo | 305 | announceUDP :: UDPTracker -> AnnounceQuery -> IO AnnounceInfo |
305 | announceUDP t query = do | 306 | announceUDP tracker ann = do |
306 | Transaction tid cid resp <- call transaction (Announce query) | 307 | resp <- transaction tracker (Announce ann) |
307 | case resp of | 308 | case resp of |
308 | Announced info -> return info | 309 | Announced info -> return info |
309 | _ -> fail "response type mismatch" | 310 | _ -> fail "response type mismatch" |
310 | 311 | ||
311 | scrapeUDP :: UDPTracker -> ScrapeQuery -> IO Scrape | 312 | scrapeUDP :: UDPTracker -> ScrapeQuery -> IO Scrape |
312 | scrapeUDP UDPTracker {..} query = do | 313 | scrapeUDP tracker scr = do |
313 | resp <- call trackerURI $ Scrape query | 314 | resp <- transaction tracker (Scrape scr) |
314 | case resp of | 315 | case resp of |
315 | Scraped scrape -> return undefined | 316 | Scraped scrape -> return undefined |
316 | 317 | ||
317 | instance Tracker UDPTracker where | 318 | instance Tracker UDPTracker where |
318 | announce = announceUDP | 319 | announce = announceUDP |
319 | scrape_ = scrapeUDP | 320 | scrape_ = scrapeUDP |
320 | -} \ No newline at end of file | ||