summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-13 08:24:58 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-13 08:24:58 +0400
commit08eeeaa520b08858af9efafe8ad921dc6e7a46e1 (patch)
treec75815fd8cdffc7d45eb3720c138c51bb0915916
parentb3ebd83bda11e5fbf8e749fe4105e7a50522a9a7 (diff)
Expose tracker RPC module
-rw-r--r--bittorrent.cabal3
-rw-r--r--src/Network/BitTorrent/Tracker/RPC.hs64
-rw-r--r--tests/Network/BitTorrent/Exchange/BlockSpec.hs9
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs2
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--
1module Network.BitTorrent.Tracker.RPC 8module 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
6import Network.BitTorrent.Tracker.RPC.Message 15import Control.Applicative
16import Control.Exception
17import Control.Monad.Trans.Resource
18import Network.URI
19
20import Network.BitTorrent.Tracker.Message
7import Network.BitTorrent.Tracker.RPC.HTTP as HTTP 21import Network.BitTorrent.Tracker.RPC.HTTP as HTTP
8import Network.BitTorrent.Tracker.RPC.UDP as UDP 22import Network.BitTorrent.Tracker.RPC.UDP as UDP
9 23
10-- | Set of tracker RPCs.
11class Tracker s where
12 connect :: URI -> IO s
13 announce :: s -> AnnounceQuery -> IO AnnounceInfo
14 scrape :: s -> ScrapeQuery -> IO Scrape
15
16instance Tracker HTTP.Tracker where
17 connect = return . HTTP.Tracker
18 announce = HTTP.announce
19 scrape = undefined
20
21instance Tracker UDP.Tracker where
22 connect = initialTracker
23 announce = announce
24 scrape = undefined
25 24
26data BitTracker = HTTPTr HTTPTracker 25data Tracker
27 | UDPTr UDPTracker 26 = HTracker Connection
27 | UTracker UDPTracker
28 28
29instance Tracker BitTracker where 29connect :: URI -> IO Tracker
30 connect uri @ URI {..} 30connect 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 37announce :: AnnounceQuery -> Tracker -> IO AnnounceInfo
38 announce (UDPTr t) = Tracker.announce t 38announce q (HTracker t) = runResourceT $ HTTP.announce q t
39announce q (UTracker t) = UDP.announce q t
39 40
40 scrape (HTTPTr t) = scrape t 41scrape :: ScrapeQuery -> Tracker -> IO ScrapeInfo
41 scrape (UDPTr t) = scrape t 42scrape q (HTracker t) = runResourceT $ HTTP.scrape q t
43scrape 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 @@
1module Network.BitTorrent.Exchange.BlockSpec (spec) where 1module Network.BitTorrent.Exchange.BlockSpec (spec) where
2import Control.Applicative 2import Control.Applicative
3import Data.Maybe
3import Test.Hspec 4import Test.Hspec
4import Test.QuickCheck 5import Test.QuickCheck
5 6
@@ -12,8 +13,14 @@ instance Arbitrary a => Arbitrary (Block a) where
12instance Arbitrary BlockIx where 13instance Arbitrary BlockIx where
13 arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary 14 arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary
14 15
16instance Arbitrary Bucket where
17 arbitrary = error "arbitrary: block bucket"
18
19instance Show Bucket where
20 show = error "show: bucket"
21
15spec :: Spec 22spec :: Spec
16spec = do 23spec = 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 @@
1module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec) where 1module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec, trackerURIs) where
2 2
3import Control.Monad 3import Control.Monad
4import Control.Monad.Trans 4import 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 #-}
2module Network.BitTorrent.Tracker.RPC.UDPSpec (spec) where 2module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, trackerURIs) where
3 3
4import Control.Monad 4import Control.Monad
5import Data.Default 5import Data.Default