diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-13 08:24:58 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-13 08:24:58 +0400 |
commit | 08eeeaa520b08858af9efafe8ad921dc6e7a46e1 (patch) | |
tree | c75815fd8cdffc7d45eb3720c138c51bb0915916 | |
parent | b3ebd83bda11e5fbf8e749fe4105e7a50522a9a7 (diff) |
Expose tracker RPC module
-rw-r--r-- | bittorrent.cabal | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/RPC.hs | 64 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Exchange/BlockSpec.hs | 9 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | 2 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | 2 |
5 files changed, 45 insertions, 35 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index f1a4f545..e420beba 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -68,7 +68,7 @@ library | |||
68 | Network.BitTorrent.Exchange.Wire | 68 | Network.BitTorrent.Exchange.Wire |
69 | -- Network.BitTorrent.Tracker | 69 | -- Network.BitTorrent.Tracker |
70 | Network.BitTorrent.Tracker.Message | 70 | Network.BitTorrent.Tracker.Message |
71 | -- Network.BitTorrent.Tracker.RPC | 71 | Network.BitTorrent.Tracker.RPC |
72 | Network.BitTorrent.Tracker.RPC.HTTP | 72 | Network.BitTorrent.Tracker.RPC.HTTP |
73 | Network.BitTorrent.Tracker.RPC.UDP | 73 | Network.BitTorrent.Tracker.RPC.UDP |
74 | Network.BitTorrent.Tracker.Wai | 74 | Network.BitTorrent.Tracker.Wai |
@@ -163,6 +163,7 @@ test-suite spec | |||
163 | Network.BitTorrent.Core.PeerIdSpec | 163 | Network.BitTorrent.Core.PeerIdSpec |
164 | Network.BitTorrent.Core.FingerprintSpec | 164 | Network.BitTorrent.Core.FingerprintSpec |
165 | Network.BitTorrent.Tracker.MessageSpec | 165 | Network.BitTorrent.Tracker.MessageSpec |
166 | Network.BitTorrent.Tracker.RPCSpec | ||
166 | Network.BitTorrent.Tracker.RPC.HTTPSpec | 167 | Network.BitTorrent.Tracker.RPC.HTTPSpec |
167 | Network.BitTorrent.Tracker.RPC.UDPSpec | 168 | Network.BitTorrent.Tracker.RPC.UDPSpec |
168 | Network.BitTorrent.Exchange.MessageSpec | 169 | Network.BitTorrent.Exchange.MessageSpec |
diff --git a/src/Network/BitTorrent/Tracker/RPC.hs b/src/Network/BitTorrent/Tracker/RPC.hs index c5aaeb03..04dbec1b 100644 --- a/src/Network/BitTorrent/Tracker/RPC.hs +++ b/src/Network/BitTorrent/Tracker/RPC.hs | |||
@@ -1,41 +1,43 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
1 | module Network.BitTorrent.Tracker.RPC | 8 | module Network.BitTorrent.Tracker.RPC |
2 | ( module Network.BitTorrent.Tracker.RPC.Message | 9 | ( Tracker |
3 | , TrackerRPC (..) | 10 | , Network.BitTorrent.Tracker.RPC.connect |
11 | , Network.BitTorrent.Tracker.RPC.announce | ||
12 | , Network.BitTorrent.Tracker.RPC.scrape | ||
4 | ) where | 13 | ) where |
5 | 14 | ||
6 | import Network.BitTorrent.Tracker.RPC.Message | 15 | import Control.Applicative |
16 | import Control.Exception | ||
17 | import Control.Monad.Trans.Resource | ||
18 | import Network.URI | ||
19 | |||
20 | import Network.BitTorrent.Tracker.Message | ||
7 | import Network.BitTorrent.Tracker.RPC.HTTP as HTTP | 21 | import Network.BitTorrent.Tracker.RPC.HTTP as HTTP |
8 | import Network.BitTorrent.Tracker.RPC.UDP as UDP | 22 | import Network.BitTorrent.Tracker.RPC.UDP as UDP |
9 | 23 | ||
10 | -- | Set of tracker RPCs. | ||
11 | class Tracker s where | ||
12 | connect :: URI -> IO s | ||
13 | announce :: s -> AnnounceQuery -> IO AnnounceInfo | ||
14 | scrape :: s -> ScrapeQuery -> IO Scrape | ||
15 | |||
16 | instance Tracker HTTP.Tracker where | ||
17 | connect = return . HTTP.Tracker | ||
18 | announce = HTTP.announce | ||
19 | scrape = undefined | ||
20 | |||
21 | instance Tracker UDP.Tracker where | ||
22 | connect = initialTracker | ||
23 | announce = announce | ||
24 | scrape = undefined | ||
25 | 24 | ||
26 | data BitTracker = HTTPTr HTTPTracker | 25 | data Tracker |
27 | | UDPTr UDPTracker | 26 | = HTracker Connection |
27 | | UTracker UDPTracker | ||
28 | 28 | ||
29 | instance Tracker BitTracker where | 29 | connect :: URI -> IO Tracker |
30 | connect uri @ URI {..} | 30 | connect uri @ URI {..} |
31 | | uriScheme == "udp:" = UDPTr <$> connect uri | 31 | | uriScheme == "http:" = HTracker <$> runResourceT (HTTP.connect uri) |
32 | | uriScheme == "http:" = HTTPTr <$> connect uri | 32 | | uriScheme == "udp:" = UTracker <$> UDP.connect uri |
33 | | otherwise = throwIO $ userError msg | 33 | | otherwise = throwIO $ userError msg |
34 | where | 34 | where |
35 | msg = "unknown tracker protocol scheme: " ++ show uriScheme | 35 | msg = "unknown tracker protocol scheme: " ++ show uriScheme |
36 | 36 | ||
37 | announce (HTTPTr t) = Tracker.announce t | 37 | announce :: AnnounceQuery -> Tracker -> IO AnnounceInfo |
38 | announce (UDPTr t) = Tracker.announce t | 38 | announce q (HTracker t) = runResourceT $ HTTP.announce q t |
39 | announce q (UTracker t) = UDP.announce q t | ||
39 | 40 | ||
40 | scrape (HTTPTr t) = scrape t | 41 | scrape :: ScrapeQuery -> Tracker -> IO ScrapeInfo |
41 | scrape (UDPTr t) = scrape t | 42 | scrape q (HTracker t) = runResourceT $ HTTP.scrape q t |
43 | scrape q (UTracker t) = UDP.scrape q t | ||
diff --git a/tests/Network/BitTorrent/Exchange/BlockSpec.hs b/tests/Network/BitTorrent/Exchange/BlockSpec.hs index 158a30b4..f4301452 100644 --- a/tests/Network/BitTorrent/Exchange/BlockSpec.hs +++ b/tests/Network/BitTorrent/Exchange/BlockSpec.hs | |||
@@ -1,5 +1,6 @@ | |||
1 | module Network.BitTorrent.Exchange.BlockSpec (spec) where | 1 | module Network.BitTorrent.Exchange.BlockSpec (spec) where |
2 | import Control.Applicative | 2 | import Control.Applicative |
3 | import Data.Maybe | ||
3 | import Test.Hspec | 4 | import Test.Hspec |
4 | import Test.QuickCheck | 5 | import Test.QuickCheck |
5 | 6 | ||
@@ -12,8 +13,14 @@ instance Arbitrary a => Arbitrary (Block a) where | |||
12 | instance Arbitrary BlockIx where | 13 | instance Arbitrary BlockIx where |
13 | arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary | 14 | arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary |
14 | 15 | ||
16 | instance Arbitrary Bucket where | ||
17 | arbitrary = error "arbitrary: block bucket" | ||
18 | |||
19 | instance Show Bucket where | ||
20 | show = error "show: bucket" | ||
21 | |||
15 | spec :: Spec | 22 | spec :: Spec |
16 | spec = do | 23 | spec = do |
17 | describe "bucket" $ do | 24 | describe "bucket" $ do |
18 | it "render to piece when it is full" $ property $ \ bkt -> | 25 | it "render to piece when it is full" $ property $ \ bkt -> |
19 | if full bkt then isJust (toPiece bkt) \ No newline at end of file | 26 | full bkt == isJust (toPiece bkt) \ No newline at end of file |
diff --git a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs index 6ff68cdf..37029b75 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec) where | 1 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec, trackerURIs) where |
2 | 2 | ||
3 | import Control.Monad | 3 | import Control.Monad |
4 | import Control.Monad.Trans | 4 | import Control.Monad.Trans |
diff --git a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs index 53babbb3..e46f15b6 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | |||
@@ -1,5 +1,5 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | module Network.BitTorrent.Tracker.RPC.UDPSpec (spec) where | 2 | module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, trackerURIs) where |
3 | 3 | ||
4 | import Control.Monad | 4 | import Control.Monad |
5 | import Data.Default | 5 | import Data.Default |