summaryrefslogtreecommitdiff
path: root/src/Network/KRPC
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-06 21:05:23 -0400
committerjoe <joe@jerkface.net>2017-06-06 21:05:23 -0400
commitcb1a1fb883527c1c6075c97d7262e41729a9b924 (patch)
tree7d673d59c2fc68057b2c2d87c1a407e1938efb4d /src/Network/KRPC
parent24df9a12a9240aaed8741d60e4b0b9cbf59a9fd9 (diff)
WIP: Adapting DHT to Tox network (part 3).
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r--src/Network/KRPC/Manager.hs20
-rw-r--r--src/Network/KRPC/Message.hs7
2 files changed, 26 insertions, 1 deletions
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs
index b1e93101..58ac7674 100644
--- a/src/Network/KRPC/Manager.hs
+++ b/src/Network/KRPC/Manager.hs
@@ -151,7 +151,11 @@ data Manager h = Manager
151 , listenerThread :: !(MVar ThreadId) 151 , listenerThread :: !(MVar ThreadId)
152 , transactionCounter :: {-# UNPACK #-} !TransactionCounter 152 , transactionCounter :: {-# UNPACK #-} !TransactionCounter
153 , pendingCalls :: {-# UNPACK #-} !PendingCalls 153 , pendingCalls :: {-# UNPACK #-} !PendingCalls
154#ifdef VERSION_bencoding
154 , handlers :: [Handler h KMessageOf BValue] 155 , handlers :: [Handler h KMessageOf BValue]
156#else
157 , handlers :: [Handler h KMessageOf BC.ByteString]
158#endif
155 } 159 }
156 160
157-- | A monad which can perform or handle queries. 161-- | A monad which can perform or handle queries.
@@ -185,7 +189,11 @@ sockAddrFamily (SockAddrCan _ ) = AF_CAN
185-- run 'listen'. 189-- run 'listen'.
186newManager :: Options -- ^ various protocol options; 190newManager :: Options -- ^ various protocol options;
187 -> SockAddr -- ^ address to listen on; 191 -> SockAddr -- ^ address to listen on;
192#ifdef VERSION_bencoding
188 -> [Handler h KMessageOf BValue] -- ^ handlers to run on incoming queries. 193 -> [Handler h KMessageOf BValue] -- ^ handlers to run on incoming queries.
194#else
195 -> [Handler h KMessageOf BC.ByteString] -- ^ handlers to run on incoming queries.
196#endif
189 -> IO (Manager h) -- ^ new rpc manager. 197 -> IO (Manager h) -- ^ new rpc manager.
190newManager opts @ Options {..} servAddr handlers = do 198newManager opts @ Options {..} servAddr handlers = do
191 validateOptions opts 199 validateOptions opts
@@ -218,7 +226,11 @@ isActive Manager {..} = liftIO $ isBound sock
218 226
219-- | Normally you should use Control.Monad.Trans.Resource.allocate 227-- | Normally you should use Control.Monad.Trans.Resource.allocate
220-- function. 228-- function.
229#ifdef VERSION_bencoding
221withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue] 230withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue]
231#else
232withManager :: Options -> SockAddr -> [Handler h KMessageOf BC.ByteString]
233#endif
222 -> (Manager h -> IO a) -> IO a 234 -> (Manager h -> IO a) -> IO a
223withManager opts addr hs = bracket (newManager opts addr hs) closeManager 235withManager opts addr hs = bracket (newManager opts addr hs) closeManager
224 236
@@ -420,14 +432,22 @@ handler msging body = (name, wrapper)
420 Right a -> Right . seal <$> body addr a 432 Right a -> Right . seal <$> body addr a
421 433
422runHandler :: MonadKRPC h m 434runHandler :: MonadKRPC h m
435#ifdef VERSION_bencoding
423 => HandlerBody h KMessageOf BValue -> SockAddr -> KQuery -> m KResult 436 => HandlerBody h KMessageOf BValue -> SockAddr -> KQuery -> m KResult
437#else
438 => HandlerBody h KMessageOf BC.ByteString -> SockAddr -> KQuery -> m KResult
439#endif
424runHandler h addr m = Lifted.catches wrapper failbacks 440runHandler h addr m = Lifted.catches wrapper failbacks
425 where 441 where
426 signature = querySignature (queryMethod m) (queryId m) addr 442 signature = querySignature (queryMethod m) (queryId m) addr
427 443
428 wrapper = do 444 wrapper = do
429 $(logDebugS) "handler.quered" signature 445 $(logDebugS) "handler.quered" signature
446#ifdef VERSION_bencoding
430 result <- liftHandler (h addr (Q m)) 447 result <- liftHandler (h addr (Q m))
448#else
449 result <- liftHandler (h addr m)
450#endif
431 451
432 case result of 452 case result of
433 Left msg -> do 453 Left msg -> do
diff --git a/src/Network/KRPC/Message.hs b/src/Network/KRPC/Message.hs
index 19f9fc9e..2f5f6729 100644
--- a/src/Network/KRPC/Message.hs
+++ b/src/Network/KRPC/Message.hs
@@ -34,7 +34,9 @@ module Network.KRPC.Message
34 , unknownMessage 34 , unknownMessage
35 35
36 -- * Query 36 -- * Query
37#ifdef VERSION_bencoding
37 , KQueryOf(..) 38 , KQueryOf(..)
39#endif
38 , KQuery 40 , KQuery
39#ifndef VERSION_bencoding 41#ifndef VERSION_bencoding
40 , queryArgs 42 , queryArgs
@@ -44,7 +46,9 @@ module Network.KRPC.Message
44 , MethodName 46 , MethodName
45 47
46 -- * Response 48 -- * Response
49#ifdef VERSION_bencoding
47 , KResponseOf(..) 50 , KResponseOf(..)
51#endif
48 , KResponse 52 , KResponse
49 , ReflectedIP(..) 53 , ReflectedIP(..)
50 54
@@ -357,5 +361,6 @@ instance BEncode KMessage where
357 <|> E <$> fromBEncode b 361 <|> E <$> fromBEncode b
358 <|> decodingError "KMessage: unknown message or message tag" 362 <|> decodingError "KMessage: unknown message or message tag"
359#else 363#else
360type KMessage = Tox.Message 364type KMessageOf = Tox.Message
365type KMessage = KMessageOf B.ByteString
361#endif 366#endif