summaryrefslogtreecommitdiff
path: root/src/Network/KRPC
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-08 02:46:32 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-08 02:46:32 +0400
commitfe87b6cec9504114dafca26166b51f6c48250106 (patch)
tree0b769283810c34e11c42002ce69b122e183d79ab /src/Network/KRPC
parenta9a0be92f7db16e1d7afe3422e56b7d7d2a63ec9 (diff)
Introduce QueryFailure exceptions
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r--src/Network/KRPC/Manager.hs30
1 files changed, 23 insertions, 7 deletions
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 #-}
18module Network.KRPC.Manager 19module 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
52import Data.Text as T 56import Data.Text as T
53import Data.Text.Encoding as T 57import Data.Text.Encoding as T
54import Data.Tuple 58import Data.Tuple
59import Data.Typeable
55import Network.KRPC.Message 60import Network.KRPC.Message
56import Network.KRPC.Method 61import Network.KRPC.Method
57import Network.Socket hiding (listen) 62import 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
215data QueryFailure
216 = QueryFailed ErrorCode Text
217 | TimeoutExpired
218 deriving (Show, Eq, Typeable)
219
220instance Exception QueryFailure
207 221
208sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () 222sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m ()
209sendMessage sock addr a = do 223sendMessage sock addr a = do
@@ -230,15 +244,17 @@ unregisterQuery cid ref = do
230queryResponse :: BEncode a => CallRes -> IO a 244queryResponse :: BEncode a => CallRes -> IO a
231queryResponse ares = do 245queryResponse 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--
243query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b 259query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b
244query addr params = do 260query 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