diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-30 13:30:19 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-30 13:30:19 +0400 |
commit | 915dab01a9aefd59497ea97e76b45db3a865635f (patch) | |
tree | 502f003d19dc8bb025827e7f8fbe050e045af702 | |
parent | 5573c240b4c2e87cf2deb55939591edd0851f8b8 (diff) |
Rename scrape datatypes
-rw-r--r-- | src/Network/BitTorrent/Tracker/RPC/Message.hs | 52 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/RPC/UDP.hs | 9 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | 10 |
3 files changed, 30 insertions, 41 deletions
diff --git a/src/Network/BitTorrent/Tracker/RPC/Message.hs b/src/Network/BitTorrent/Tracker/RPC/Message.hs index a0691f37..74a3842f 100644 --- a/src/Network/BitTorrent/Tracker/RPC/Message.hs +++ b/src/Network/BitTorrent/Tracker/RPC/Message.hs | |||
@@ -40,8 +40,8 @@ module Network.BitTorrent.Tracker.RPC.Message | |||
40 | 40 | ||
41 | -- * Scrape | 41 | -- * Scrape |
42 | , ScrapeQuery | 42 | , ScrapeQuery |
43 | , ScrapeInfo(..) | 43 | , ScrapeEntry (..) |
44 | , Scrape | 44 | , ScrapeInfo |
45 | ) | 45 | ) |
46 | where | 46 | where |
47 | 47 | ||
@@ -56,7 +56,6 @@ import Data.ByteString.Char8 as BC | |||
56 | import Data.Char as Char | 56 | import Data.Char as Char |
57 | import Data.Convertible | 57 | import Data.Convertible |
58 | import Data.List as L | 58 | import Data.List as L |
59 | import Data.Map as M | ||
60 | import Data.Maybe | 59 | import Data.Maybe |
61 | import Data.Serialize as S hiding (Result) | 60 | import Data.Serialize as S hiding (Result) |
62 | import Data.Text (Text) | 61 | import Data.Text (Text) |
@@ -482,9 +481,8 @@ parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage | |||
482 | 481 | ||
483 | type ScrapeQuery = [InfoHash] | 482 | type ScrapeQuery = [InfoHash] |
484 | 483 | ||
485 | -- TODO rename to ScrapeEntry | ||
486 | -- | Overall information about particular torrent. | 484 | -- | Overall information about particular torrent. |
487 | data ScrapeInfo = ScrapeInfo { | 485 | data ScrapeEntry = ScrapeEntry { |
488 | -- | Number of seeders - peers with the entire file. | 486 | -- | Number of seeders - peers with the entire file. |
489 | siComplete :: {-# UNPACK #-} !Int | 487 | siComplete :: {-# UNPACK #-} !Int |
490 | 488 | ||
@@ -499,43 +497,35 @@ data ScrapeInfo = ScrapeInfo { | |||
499 | , siName :: !(Maybe Text) | 497 | , siName :: !(Maybe Text) |
500 | } deriving (Show, Eq, Typeable) | 498 | } deriving (Show, Eq, Typeable) |
501 | 499 | ||
502 | $(deriveJSON (L.map toLower . L.dropWhile isLower) ''ScrapeInfo) | 500 | $(deriveJSON (L.map toLower . L.dropWhile isLower) ''ScrapeEntry) |
503 | |||
504 | -- TODO hash map | ||
505 | -- TODO rename to ScrapeInfo | ||
506 | -- | Scrape info about a set of torrents. | ||
507 | type Scrape = Map InfoHash ScrapeInfo | ||
508 | 501 | ||
509 | -- | HTTP tracker protocol compatible encoding. | 502 | -- | HTTP tracker protocol compatible encoding. |
510 | instance BEncode ScrapeInfo where | 503 | instance BEncode ScrapeEntry where |
511 | toBEncode ScrapeInfo {..} = toDict $ | 504 | toBEncode ScrapeEntry {..} = toDict $ |
512 | "complete" .=! siComplete | 505 | "complete" .=! siComplete |
513 | .: "downloaded" .=! siDownloaded | 506 | .: "downloaded" .=! siDownloaded |
514 | .: "incomplete" .=! siIncomplete | 507 | .: "incomplete" .=! siIncomplete |
515 | .: "name" .=? siName | 508 | .: "name" .=? siName |
516 | .: endDict | 509 | .: endDict |
517 | 510 | ||
518 | fromBEncode = fromDict $ do | 511 | fromBEncode = fromDict $ ScrapeEntry |
519 | ScrapeInfo <$>! "complete" | 512 | <$>! "complete" |
520 | <*>! "downloaded" | 513 | <*>! "downloaded" |
521 | <*>! "incomplete" | 514 | <*>! "incomplete" |
522 | <*>? "name" | 515 | <*>? "name" |
523 | 516 | ||
524 | -- | UDP tracker protocol compatible encoding. | 517 | -- | UDP tracker protocol compatible encoding. |
525 | instance Serialize ScrapeInfo where | 518 | instance Serialize ScrapeEntry where |
526 | put ScrapeInfo {..} = do | 519 | put ScrapeEntry {..} = do |
527 | putWord32be $ fromIntegral siComplete | 520 | putWord32be $ fromIntegral siComplete |
528 | putWord32be $ fromIntegral siDownloaded | 521 | putWord32be $ fromIntegral siDownloaded |
529 | putWord32be $ fromIntegral siIncomplete | 522 | putWord32be $ fromIntegral siIncomplete |
530 | 523 | ||
531 | get = do | 524 | get = ScrapeEntry |
532 | seeders <- getWord32be | 525 | <$> (fromIntegral <$> getWord32be) |
533 | downTimes <- getWord32be | 526 | <*> (fromIntegral <$> getWord32be) |
534 | leechers <- getWord32be | 527 | <*> (fromIntegral <$> getWord32be) |
535 | 528 | <*> pure Nothing | |
536 | return $ ScrapeInfo { | 529 | |
537 | siComplete = fromIntegral seeders | 530 | -- | Scrape info about a set of torrents. |
538 | , siDownloaded = fromIntegral downTimes | 531 | type ScrapeInfo = [(InfoHash, ScrapeEntry)] |
539 | , siIncomplete = fromIntegral leechers | ||
540 | , siName = Nothing | ||
541 | } | ||
diff --git a/src/Network/BitTorrent/Tracker/RPC/UDP.hs b/src/Network/BitTorrent/Tracker/RPC/UDP.hs index 0336db8d..bb5fe7e3 100644 --- a/src/Network/BitTorrent/Tracker/RPC/UDP.hs +++ b/src/Network/BitTorrent/Tracker/RPC/UDP.hs | |||
@@ -30,7 +30,6 @@ import Control.Monad | |||
30 | import Data.ByteString (ByteString) | 30 | import Data.ByteString (ByteString) |
31 | import Data.IORef | 31 | import Data.IORef |
32 | import Data.List as L | 32 | import Data.List as L |
33 | import Data.Map as M | ||
34 | import Data.Maybe | 33 | import Data.Maybe |
35 | import Data.Monoid | 34 | import Data.Monoid |
36 | import Data.Serialize | 35 | import Data.Serialize |
@@ -94,7 +93,7 @@ data Request = Connect | |||
94 | 93 | ||
95 | data Response = Connected ConnectionId | 94 | data Response = Connected ConnectionId |
96 | | Announced AnnounceInfo | 95 | | Announced AnnounceInfo |
97 | | Scraped [ScrapeInfo] | 96 | | Scraped [ScrapeEntry] |
98 | | Failed Text | 97 | | Failed Text |
99 | deriving Show | 98 | deriving Show |
100 | 99 | ||
@@ -288,7 +287,7 @@ connectUDP tracker = do | |||
288 | case resp of | 287 | case resp of |
289 | Connected cid -> return cid | 288 | Connected cid -> return cid |
290 | Failed msg -> throwIO $ userError $ T.unpack msg | 289 | Failed msg -> throwIO $ userError $ T.unpack msg |
291 | _ -> throwIO $ userError "message type mismatch" | 290 | _ -> throwIO $ userError "connect: response type mismatch" |
292 | 291 | ||
293 | connect :: URI -> IO UDPTracker | 292 | connect :: URI -> IO UDPTracker |
294 | connect uri = do | 293 | connect uri = do |
@@ -313,12 +312,12 @@ announce ann tracker = do | |||
313 | Announced info -> return info | 312 | Announced info -> return info |
314 | _ -> fail "announce: response type mismatch" | 313 | _ -> fail "announce: response type mismatch" |
315 | 314 | ||
316 | scrape :: ScrapeQuery -> UDPTracker -> IO Scrape | 315 | scrape :: ScrapeQuery -> UDPTracker -> IO ScrapeInfo |
317 | scrape ihs tracker = do | 316 | scrape ihs tracker = do |
318 | freshConnection tracker | 317 | freshConnection tracker |
319 | resp <- transaction tracker (Scrape ihs) | 318 | resp <- transaction tracker (Scrape ihs) |
320 | case resp of | 319 | case resp of |
321 | Scraped info -> return $ M.fromList $ L.zip ihs info | 320 | Scraped info -> return $ L.zip ihs info |
322 | _ -> fail "scrape: response type mismatch" | 321 | _ -> fail "scrape: response type mismatch" |
323 | 322 | ||
324 | {----------------------------------------------------------------------- | 323 | {----------------------------------------------------------------------- |
diff --git a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs index 1a893011..73cf07f3 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | |||
@@ -11,7 +11,7 @@ import Test.Hspec | |||
11 | import Test.QuickCheck | 11 | import Test.QuickCheck |
12 | 12 | ||
13 | import Network.BitTorrent.Core.PeerAddr | 13 | import Network.BitTorrent.Core.PeerAddr |
14 | import Network.BitTorrent.Tracker.RPC.Message | 14 | import Network.BitTorrent.Tracker.RPC.Message as Message |
15 | import Network.BitTorrent.Tracker.RPC.UDP | 15 | import Network.BitTorrent.Tracker.RPC.UDP |
16 | import Network.BitTorrent.Tracker.RPC.MessageSpec () | 16 | import Network.BitTorrent.Tracker.RPC.MessageSpec () |
17 | 17 | ||
@@ -27,6 +27,7 @@ trackerURIs = | |||
27 | 27 | ||
28 | -- relation with query: peer id, numwant | 28 | -- relation with query: peer id, numwant |
29 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation | 29 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation |
30 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" | ||
30 | validateInfo AnnounceQuery {..} AnnounceInfo {..} = do | 31 | validateInfo AnnounceQuery {..} AnnounceInfo {..} = do |
31 | respComplete `shouldSatisfy` isJust | 32 | respComplete `shouldSatisfy` isJust |
32 | respIncomplete `shouldSatisfy` isJust | 33 | respIncomplete `shouldSatisfy` isJust |
@@ -44,11 +45,10 @@ spec = do | |||
44 | context (show uri) $ do | 45 | context (show uri) $ do |
45 | describe "announce" $ do | 46 | describe "announce" $ do |
46 | it "have valid response" $ do | 47 | it "have valid response" $ do |
47 | query <- arbitrarySample | 48 | q <- arbitrarySample |
48 | connect uri >>= announce query >>= validateInfo query | 49 | connect uri >>= announce q >>= validateInfo q |
49 | 50 | ||
50 | describe "scrape" $ do | 51 | describe "scrape" $ do |
51 | it "have valid response" $ do | 52 | it "have valid response" $ do |
52 | xs <- connect uri >>= scrape [def] | 53 | xs <- connect uri >>= scrape [def] |
53 | return () | 54 | L.length xs `shouldSatisfy` (>= 1) |
54 | -- L.length xs `shouldSatisfy` (>= 1) | ||