From 3819be8760ea8ba847cd370d78861637dda09759 Mon Sep 17 00:00:00 2001 From: Sam T Date: Sat, 20 Jul 2013 17:09:08 +0400 Subject: ~ Remove announce URI from TRequest. --- src/Network/BitTorrent/Tracker.hs | 9 ++++----- src/Network/BitTorrent/Tracker/Protocol.hs | 30 ++++++++++++------------------ 2 files changed, 16 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs index b737a3df..25d2c358 100644 --- a/src/Network/BitTorrent/Tracker.hs +++ b/src/Network/BitTorrent/Tracker.hs @@ -90,8 +90,7 @@ tconnection t = TConnection (tAnnounce t) (tInfoHash t) -- | used to avoid boilerplate; do NOT export me genericReq :: TConnection -> Progress -> TRequest genericReq ses pr = TRequest { - reqAnnounce = tconnAnnounce ses - , reqInfoHash = tconnInfoHash ses + reqInfoHash = tconnInfoHash ses , reqPeerId = tconnPeerId ses , reqPort = tconnPort ses @@ -221,7 +220,7 @@ withTracker :: Progress -> TConnection -> (TSession -> IO a) -> IO a withTracker initProgress conn action = bracket start end (action . fst) where start = do - resp <- askTracker (startedReq conn initProgress) + resp <- askTracker (tconnAnnounce conn) (startedReq conn initProgress) se <- newSession defaultChanSize initProgress (respInterval resp) (respPeers resp) @@ -232,7 +231,7 @@ withTracker initProgress conn action = bracket start end (action . fst) waitInterval se pr <- getProgress se resp <- tryJust isIOException $ do - askTracker (regularReq defaultNumWant conn pr) + askTracker (tconnAnnounce conn) (regularReq defaultNumWant conn pr) case resp of Right (OK {..}) -> do writeIORef seInterval respInterval @@ -252,7 +251,7 @@ withTracker initProgress conn action = bracket start end (action . fst) end (se, tid) = do killThread tid pr <- getProgress se - leaveTracker $ stoppedReq conn pr + leaveTracker (tconnAnnounce conn) (stoppedReq conn pr) {----------------------------------------------------------------------- Scrape diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs index d61a269c..e844f8b8 100644 --- a/src/Network/BitTorrent/Tracker/Protocol.hs +++ b/src/Network/BitTorrent/Tracker/Protocol.hs @@ -73,11 +73,8 @@ data Event = Started -- the torrent. The most important, requests are used by the tracker -- to keep track lists of active peer for a particular torrent. -- -data TRequest = TRequest { -- TODO peer here -- TODO detach announce - reqAnnounce :: !URI - -- ^ Announce url of the torrent usually obtained from 'Torrent'. - - , reqInfoHash :: !InfoHash +data TRequest = TRequest { -- TODO peer here + reqInfoHash :: !InfoHash -- ^ Hash of info part of the torrent usually obtained from -- 'Torrent'. @@ -162,7 +159,6 @@ instance BEncodable TResponse where <*> d >--? "complete" <*> d >--? "incomplete" <*> getPeers (M.lookup "peers" d) - where getPeers :: Maybe BEncode -> Result [PeerAddr] getPeers (Just (BList l)) = fromBEncode (BList l) @@ -196,9 +192,9 @@ instance URLEncode TRequest where ] where s :: String -> String; s = id; {-# INLINE s #-} -encodeRequest :: TRequest -> URI -encodeRequest req = URL.urlEncode req - `addToURI` reqAnnounce req +encodeRequest :: URI -> TRequest -> URI +encodeRequest announce req = URL.urlEncode req + `addToURI` announce `addHashToURI` reqInfoHash req {----------------------------------------------------------------------- @@ -259,9 +255,7 @@ instance Serialize TRequest where port <- get return $ TRequest { - -- TODO remove reqAnnounce field from TRequest - reqAnnounce = error "tracker request decode" - , reqInfoHash = ih + reqInfoHash = ih , reqPeerId = pid , reqPort = port , reqUploaded = fromIntegral up @@ -322,9 +316,9 @@ mkHTTPRequest uri = Request uri GET [] "" -- announce list. This function throws 'IOException' if it couldn't -- send request or receive response or decode response. -- -askTracker :: TRequest -> IO TResponse -askTracker req = do - let r = mkHTTPRequest (encodeRequest req) +askTracker :: URI -> TRequest -> IO TResponse +askTracker announce req = do + let r = mkHTTPRequest (encodeRequest announce req) rawResp <- simpleHTTP r respBody <- getResponseBody rawResp @@ -339,7 +333,7 @@ askTracker req = do -- | The same as the 'askTracker' but ignore response. Used in -- conjunction with 'Stopped'. -leaveTracker :: TRequest -> IO () -leaveTracker req = do - let r = mkHTTPRequest (encodeRequest req) +leaveTracker :: URI -> TRequest -> IO () +leaveTracker announce req = do + let r = mkHTTPRequest (encodeRequest announce req) void $ simpleHTTP r >>= getResponseBody -- cgit v1.2.3