diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-06 05:34:31 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-06 05:34:31 +0400 |
commit | 285a01b62a2cf42d3548a0af6235f7ae2e3d1603 (patch) | |
tree | 2f0fcaaf4e0481a3edbb81cb43d4ad1b071bc599 /src/Network/BitTorrent/Tracker/RPC.hs | |
parent | f138b7d6444b2de6f1ceab115b07011131b477d3 (diff) |
Add unified tracker RpcExceptions
Diffstat (limited to 'src/Network/BitTorrent/Tracker/RPC.hs')
-rw-r--r-- | src/Network/BitTorrent/Tracker/RPC.hs | 29 |
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 #-} | ||
10 | module Network.BitTorrent.Tracker.RPC | 11 | module 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 | ||
26 | import Control.Exception | 28 | import Control.Exception |
27 | import Data.Default | 29 | import Data.Default |
30 | import Data.Typeable | ||
28 | import Network | 31 | import Network |
29 | import Network.URI | 32 | import Network.URI |
30 | import Network.Socket (HostAddress) | 33 | import Network.Socket (HostAddress) |
@@ -114,17 +117,31 @@ withManager :: Options -> PeerInfo -> (Manager -> IO a) -> IO a | |||
114 | withManager opts info = bracket (newManager opts info) closeManager | 117 | withManager opts info = bracket (newManager opts info) closeManager |
115 | 118 | ||
116 | {----------------------------------------------------------------------- | 119 | {----------------------------------------------------------------------- |
120 | -- Exceptions | ||
121 | -----------------------------------------------------------------------} | ||
122 | -- TODO Catch IO exceptions on rpc calls (?) | ||
123 | |||
124 | data RpcException | ||
125 | = UdpException UDP.RpcException -- ^ | ||
126 | | HttpException HTTP.RpcException -- ^ | ||
127 | | UnknownProtocol String -- ^ unknown tracker protocol scheme | ||
128 | deriving (Show, Typeable) | ||
129 | |||
130 | instance Exception RpcException | ||
131 | |||
132 | packException :: Exception e => (e -> RpcException) -> IO a -> IO a | ||
133 | packException 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 | ||
121 | dispatch :: URI -> IO a -> IO a -> IO a | 140 | dispatch :: URI -> IO a -> IO a -> IO a |
122 | dispatch URI {..} http udp | 141 | dispatch 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 | ||
129 | announce :: Manager -> URI -> SAnnounceQuery -> IO AnnounceInfo | 146 | announce :: Manager -> URI -> SAnnounceQuery -> IO AnnounceInfo |
130 | announce Manager {..} uri simpleQuery | 147 | announce Manager {..} uri simpleQuery |