From 5573c240b4c2e87cf2deb55939591edd0851f8b8 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sat, 30 Nov 2013 13:06:09 +0400 Subject: Add basic spec for UDP tracker RPC --- src/Network/BitTorrent/Tracker/RPC/Message.hs | 2 ++ src/Network/BitTorrent/Tracker/RPC/UDP.hs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Network/BitTorrent/Tracker/RPC/Message.hs b/src/Network/BitTorrent/Tracker/RPC/Message.hs index 18c1a4c7..a0691f37 100644 --- a/src/Network/BitTorrent/Tracker/RPC/Message.hs +++ b/src/Network/BitTorrent/Tracker/RPC/Message.hs @@ -482,6 +482,7 @@ parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage type ScrapeQuery = [InfoHash] +-- TODO rename to ScrapeEntry -- | Overall information about particular torrent. data ScrapeInfo = ScrapeInfo { -- | Number of seeders - peers with the entire file. @@ -501,6 +502,7 @@ data ScrapeInfo = ScrapeInfo { $(deriveJSON (L.map toLower . L.dropWhile isLower) ''ScrapeInfo) -- TODO hash map +-- TODO rename to ScrapeInfo -- | Scrape info about a set of torrents. type Scrape = Map InfoHash ScrapeInfo diff --git a/src/Network/BitTorrent/Tracker/RPC/UDP.hs b/src/Network/BitTorrent/Tracker/RPC/UDP.hs index beff6b4f..0336db8d 100644 --- a/src/Network/BitTorrent/Tracker/RPC/UDP.hs +++ b/src/Network/BitTorrent/Tracker/RPC/UDP.hs @@ -39,7 +39,7 @@ import Data.Text.Encoding import Data.Time import Data.Word import Text.Read (readMaybe) -import Network.Socket hiding (Connected) +import Network.Socket hiding (Connected, connect) import Network.Socket.ByteString as BS import Network.URI import System.Entropy @@ -290,8 +290,8 @@ connectUDP tracker = do Failed msg -> throwIO $ userError $ T.unpack msg _ -> throwIO $ userError "message type mismatch" -initialTracker :: URI -> IO UDPTracker -initialTracker uri = do +connect :: URI -> IO UDPTracker +connect uri = do tracker <- UDPTracker uri <$> (newIORef =<< initialConnection) connId <- connectUDP tracker updateConnection connId tracker @@ -305,20 +305,20 @@ freshConnection tracker @ UDPTracker {..} = do connId <- connectUDP tracker updateConnection connId tracker -announce :: UDPTracker -> AnnounceQuery -> IO AnnounceInfo -announce tracker ann = do +announce :: AnnounceQuery -> UDPTracker -> IO AnnounceInfo +announce ann tracker = do freshConnection tracker resp <- transaction tracker (Announce ann) case resp of Announced info -> return info _ -> fail "announce: response type mismatch" -scrape :: UDPTracker -> ScrapeQuery -> IO Scrape -scrape tracker scr = do +scrape :: ScrapeQuery -> UDPTracker -> IO Scrape +scrape ihs tracker = do freshConnection tracker - resp <- transaction tracker (Scrape scr) + resp <- transaction tracker (Scrape ihs) case resp of - Scraped info -> return $ M.fromList $ L.zip scr info + Scraped info -> return $ M.fromList $ L.zip ihs info _ -> fail "scrape: response type mismatch" {----------------------------------------------------------------------- -- cgit v1.2.3