diff options
-rw-r--r-- | src/Network/KRPC.hs | 3 | ||||
-rw-r--r-- | src/Network/KRPC/Manager.hs | 30 |
2 files changed, 26 insertions, 7 deletions
diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs index 7c02702c..96971803 100644 --- a/src/Network/KRPC.hs +++ b/src/Network/KRPC.hs | |||
@@ -58,6 +58,9 @@ module Network.KRPC | |||
58 | -- * RPC | 58 | -- * RPC |
59 | , Handler | 59 | , Handler |
60 | , handler | 60 | , handler |
61 | |||
62 | -- ** Query | ||
63 | , QueryFailure (..) | ||
61 | , query | 64 | , query |
62 | 65 | ||
63 | -- * Manager | 66 | -- * Manager |
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs index bf142738..6799277f 100644 --- a/src/Network/KRPC/Manager.hs +++ b/src/Network/KRPC/Manager.hs | |||
@@ -14,6 +14,7 @@ | |||
14 | {-# LANGUAGE DefaultSignatures #-} | 14 | {-# LANGUAGE DefaultSignatures #-} |
15 | {-# LANGUAGE MultiParamTypeClasses #-} | 15 | {-# LANGUAGE MultiParamTypeClasses #-} |
16 | {-# LANGUAGE FunctionalDependencies #-} | 16 | {-# LANGUAGE FunctionalDependencies #-} |
17 | {-# LANGUAGE DeriveDataTypeable #-} | ||
17 | {-# LANGUAGE TemplateHaskell #-} | 18 | {-# LANGUAGE TemplateHaskell #-} |
18 | module Network.KRPC.Manager | 19 | module Network.KRPC.Manager |
19 | ( -- * Manager | 20 | ( -- * Manager |
@@ -26,7 +27,10 @@ module Network.KRPC.Manager | |||
26 | , listen | 27 | , listen |
27 | 28 | ||
28 | -- * Queries | 29 | -- * Queries |
30 | , QueryFailure (..) | ||
29 | , query | 31 | , query |
32 | |||
33 | -- * Handlers | ||
30 | , Handler | 34 | , Handler |
31 | , handler | 35 | , handler |
32 | ) where | 36 | ) where |
@@ -52,6 +56,7 @@ import Data.Monoid | |||
52 | import Data.Text as T | 56 | import Data.Text as T |
53 | import Data.Text.Encoding as T | 57 | import Data.Text.Encoding as T |
54 | import Data.Tuple | 58 | import Data.Tuple |
59 | import Data.Typeable | ||
55 | import Network.KRPC.Message | 60 | import Network.KRPC.Message |
56 | import Network.KRPC.Method | 61 | import Network.KRPC.Method |
57 | import Network.Socket hiding (listen) | 62 | import Network.Socket hiding (listen) |
@@ -204,6 +209,15 @@ querySignature name transaction addr = T.concat | |||
204 | {----------------------------------------------------------------------- | 209 | {----------------------------------------------------------------------- |
205 | -- Client | 210 | -- Client |
206 | -----------------------------------------------------------------------} | 211 | -----------------------------------------------------------------------} |
212 | -- we don't need to know about TransactionId while performing query, | ||
213 | -- so we introduce QueryFailure exceptions | ||
214 | |||
215 | data QueryFailure | ||
216 | = QueryFailed ErrorCode Text | ||
217 | | TimeoutExpired | ||
218 | deriving (Show, Eq, Typeable) | ||
219 | |||
220 | instance Exception QueryFailure | ||
207 | 221 | ||
208 | sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () | 222 | sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () |
209 | sendMessage sock addr a = do | 223 | sendMessage sock addr a = do |
@@ -230,15 +244,17 @@ unregisterQuery cid ref = do | |||
230 | queryResponse :: BEncode a => CallRes -> IO a | 244 | queryResponse :: BEncode a => CallRes -> IO a |
231 | queryResponse ares = do | 245 | queryResponse ares = do |
232 | res <- readMVar ares | 246 | res <- readMVar ares |
233 | KResponse {..} <- either throwIO pure res | 247 | case res of |
234 | case fromBEncode respVals of | 248 | Left (KError c m _) -> throwIO $ QueryFailed c (T.decodeUtf8 m) |
235 | Right r -> pure r | 249 | Right (KResponse {..}) -> |
236 | Left e -> throwIO $ decodeError e respId | 250 | case fromBEncode respVals of |
251 | Right r -> pure r | ||
252 | Left e -> throwIO $ QueryFailed ProtocolError (T.pack e) | ||
237 | 253 | ||
238 | -- | Enqueue query to the given node. | 254 | -- | Enqueue query to the given node. |
239 | -- | 255 | -- |
240 | -- This function will throw exception if quered node respond with | 256 | -- This function should throw 'QueryFailure' exception if quered node |
241 | -- @error@ message or timeout expires. | 257 | -- respond with @error@ message or the query timeout expires. |
242 | -- | 258 | -- |
243 | query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b | 259 | query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b |
244 | query addr params = do | 260 | query addr params = do |
@@ -267,7 +283,7 @@ query addr params = do | |||
267 | _ <- liftIO $ unregisterQuery (tid, addr) pendingCalls | 283 | _ <- liftIO $ unregisterQuery (tid, addr) pendingCalls |
268 | $(logWarnS) "query.not_responding" $ signature <> " for " <> | 284 | $(logWarnS) "query.not_responding" $ signature <> " for " <> |
269 | T.pack (show (optQueryTimeout options)) <> " seconds" | 285 | T.pack (show (optQueryTimeout options)) <> " seconds" |
270 | throw $ timeoutExpired tid | 286 | throw $ TimeoutExpired |
271 | 287 | ||
272 | {----------------------------------------------------------------------- | 288 | {----------------------------------------------------------------------- |
273 | -- Handlers | 289 | -- Handlers |