summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker/RPC.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-02-06 05:34:31 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-02-06 05:34:31 +0400
commit285a01b62a2cf42d3548a0af6235f7ae2e3d1603 (patch)
tree2f0fcaaf4e0481a3edbb81cb43d4ad1b071bc599 /src/Network/BitTorrent/Tracker/RPC.hs
parentf138b7d6444b2de6f1ceab115b07011131b477d3 (diff)
Add unified tracker RpcExceptions
Diffstat (limited to 'src/Network/BitTorrent/Tracker/RPC.hs')
-rw-r--r--src/Network/BitTorrent/Tracker/RPC.hs29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/Tracker/RPC.hs b/src/Network/BitTorrent/Tracker/RPC.hs
index bd8da02b..48048964 100644
--- a/src/Network/BitTorrent/Tracker/RPC.hs
+++ b/src/Network/BitTorrent/Tracker/RPC.hs
@@ -7,6 +7,7 @@
7-- 7--
8-- Protocol independent bittorrent tracker API. 8-- Protocol independent bittorrent tracker API.
9-- 9--
10{-# LANGUAGE DeriveDataTypeable #-}
10module Network.BitTorrent.Tracker.RPC 11module Network.BitTorrent.Tracker.RPC
11 ( PeerInfo (..) 12 ( PeerInfo (..)
12 13
@@ -19,12 +20,14 @@ module Network.BitTorrent.Tracker.RPC
19 20
20 -- * RPC 21 -- * RPC
21 , SAnnounceQuery (..) 22 , SAnnounceQuery (..)
23 , RpcException (..)
22 , announce 24 , announce
23 , scrape 25 , scrape
24 ) where 26 ) where
25 27
26import Control.Exception 28import Control.Exception
27import Data.Default 29import Data.Default
30import Data.Typeable
28import Network 31import Network
29import Network.URI 32import Network.URI
30import Network.Socket (HostAddress) 33import Network.Socket (HostAddress)
@@ -114,17 +117,31 @@ withManager :: Options -> PeerInfo -> (Manager -> IO a) -> IO a
114withManager opts info = bracket (newManager opts info) closeManager 117withManager opts info = bracket (newManager opts info) closeManager
115 118
116{----------------------------------------------------------------------- 119{-----------------------------------------------------------------------
120-- Exceptions
121-----------------------------------------------------------------------}
122-- TODO Catch IO exceptions on rpc calls (?)
123
124data RpcException
125 = UdpException UDP.RpcException -- ^
126 | HttpException HTTP.RpcException -- ^
127 | UnknownProtocol String -- ^ unknown tracker protocol scheme
128 deriving (Show, Typeable)
129
130instance Exception RpcException
131
132packException :: Exception e => (e -> RpcException) -> IO a -> IO a
133packException f m = try m >>= either (throwIO . f) return
134{-# INLINE packException #-}
135
136{-----------------------------------------------------------------------
117-- RPC calls 137-- RPC calls
118-----------------------------------------------------------------------} 138-----------------------------------------------------------------------}
119-- TODO Catch IO exceptions on rpc calls.
120 139
121dispatch :: URI -> IO a -> IO a -> IO a 140dispatch :: URI -> IO a -> IO a -> IO a
122dispatch URI {..} http udp 141dispatch URI {..} http udp
123 | uriScheme == "http:" = http 142 | uriScheme == "http:" = packException HttpException http
124 | uriScheme == "udp:" = udp 143 | uriScheme == "udp:" = packException UdpException udp
125 | otherwise = throwIO $ userError msg 144 | otherwise = throwIO $ UnknownProtocol uriScheme
126 where
127 msg = "unknown tracker protocol scheme: " ++ show uriScheme
128 145
129announce :: Manager -> URI -> SAnnounceQuery -> IO AnnounceInfo 146announce :: Manager -> URI -> SAnnounceQuery -> IO AnnounceInfo
130announce Manager {..} uri simpleQuery 147announce Manager {..} uri simpleQuery