diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/KRPC/Manager.hs | 40 |
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 | ||
33 | import Control.Applicative | 33 | import Control.Applicative |
34 | import Control.Arrow | ||
35 | import Control.Concurrent | 34 | import Control.Concurrent |
36 | import Control.Concurrent.Lifted (fork) | 35 | import Control.Concurrent.Lifted (fork) |
37 | import Control.Exception hiding (Handler) | 36 | import Control.Exception hiding (Handler) |
@@ -146,6 +145,17 @@ withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a | |||
146 | withManager addr hs = bracket (newManager addr hs) closeManager | 145 | withManager addr hs = bracket (newManager addr hs) closeManager |
147 | 146 | ||
148 | {----------------------------------------------------------------------- | 147 | {----------------------------------------------------------------------- |
148 | -- Logging | ||
149 | -----------------------------------------------------------------------} | ||
150 | |||
151 | querySignature :: MethodName -> TransactionId -> SockAddr -> Text | ||
152 | querySignature 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 | ||
236 | runHandler :: MonadKRPC h m => HandlerBody h -> SockAddr -> KQuery -> m KResult | 244 | runHandler :: MonadKRPC h m |
245 | => HandlerBody h -> SockAddr -> KQuery -> m KResult | ||
237 | runHandler h addr KQuery {..} = wrapper `Lifted.catch` failback | 246 | runHandler 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 | ||
243 | dispatchHandler :: MonadKRPC h m => KQuery -> SockAddr -> m KResult | 267 | dispatchHandler :: MonadKRPC h m => KQuery -> SockAddr -> m KResult |
244 | dispatchHandler q @ KQuery {..} addr = do | 268 | dispatchHandler q @ KQuery {..} addr = do |