summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/KRPC/Manager.hs40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs
index cc2e383e..ee336a4d 100644
--- a/src/Network/KRPC/Manager.hs
+++ b/src/Network/KRPC/Manager.hs
@@ -31,7 +31,6 @@ module Network.KRPC.Manager
31 ) where 31 ) where
32 32
33import Control.Applicative 33import Control.Applicative
34import Control.Arrow
35import Control.Concurrent 34import Control.Concurrent
36import Control.Concurrent.Lifted (fork) 35import Control.Concurrent.Lifted (fork)
37import Control.Exception hiding (Handler) 36import Control.Exception hiding (Handler)
@@ -146,6 +145,17 @@ withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a
146withManager addr hs = bracket (newManager addr hs) closeManager 145withManager addr hs = bracket (newManager addr hs) closeManager
147 146
148{----------------------------------------------------------------------- 147{-----------------------------------------------------------------------
148-- Logging
149-----------------------------------------------------------------------}
150
151querySignature :: MethodName -> TransactionId -> SockAddr -> Text
152querySignature name transaction addr = T.concat
153 [ "&", T.decodeUtf8 name
154 , " #", T.decodeUtf8 transaction
155 , " @", T.pack (show addr)
156 ]
157
158{-----------------------------------------------------------------------
149-- Client 159-- Client
150-----------------------------------------------------------------------} 160-----------------------------------------------------------------------}
151 161
@@ -189,9 +199,7 @@ query addr params = do
189 Manager {..} <- getManager 199 Manager {..} <- getManager
190 tid <- liftIO $ genTransactionId transactionCounter 200 tid <- liftIO $ genTransactionId transactionCounter
191 let queryMethod = method :: Method a b 201 let queryMethod = method :: Method a b
192 let signature = T.pack (show queryMethod) 202 let signature = querySignature (methodName queryMethod) tid addr
193 <> " @" <> T.pack (show addr)
194 <> " #" <> T.decodeUtf8 tid
195 $(logDebugS) "query.sending" signature 203 $(logDebugS) "query.sending" signature
196 204
197 mres <- liftIO $ do 205 mres <- liftIO $ do
@@ -233,12 +241,28 @@ handler body = (name, wrapper)
233 r <- body addr a 241 r <- body addr a
234 return $ Right $ toBEncode r 242 return $ Right $ toBEncode r
235 243
236runHandler :: MonadKRPC h m => HandlerBody h -> SockAddr -> KQuery -> m KResult 244runHandler :: MonadKRPC h m
245 => HandlerBody h -> SockAddr -> KQuery -> m KResult
237runHandler h addr KQuery {..} = wrapper `Lifted.catch` failback 246runHandler h addr KQuery {..} = wrapper `Lifted.catch` failback
238 where 247 where
239 wrapper = ((`decodeError` queryId) +++ (`KResponse` queryId)) 248 signature = querySignature queryMethod queryId addr
240 <$> liftHandler (h addr queryArgs) 249
241 failback e = return $ Left $ serverError e queryId 250 wrapper = do
251 $(logDebugS) "handler.quered" signature
252 result <- liftHandler (h addr queryArgs)
253
254 case result of
255 Left msg -> do
256 $(logDebugS) "handler.failed" $ signature <> " !" <> T.pack msg
257 return $ Left $ decodeError msg queryId
258
259 Right a -> do
260 $(logDebugS) "handler.success" signature
261 return $ Right $ a `KResponse` queryId
262
263 failback e = do
264 $(logDebugS) "handler.errored" signature
265 return $ Left $ serverError e queryId
242 266
243dispatchHandler :: MonadKRPC h m => KQuery -> SockAddr -> m KResult 267dispatchHandler :: MonadKRPC h m => KQuery -> SockAddr -> m KResult
244dispatchHandler q @ KQuery {..} addr = do 268dispatchHandler q @ KQuery {..} addr = do