diff options
author | joe <joe@jerkface.net> | 2017-06-07 05:57:20 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-07 05:57:20 -0400 |
commit | 05345c643d0bcebe17f9474d9561da6e90fff34e (patch) | |
tree | c3ad0c1dd86a376b8c177fda57d5ef835e4efdf5 /src/Network/KRPC | |
parent | a4fe28f0cf95da88f5c2db4e3397c227625aa6ac (diff) |
WIP: Adapting DHT to Tox network (part 4).
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r-- | src/Network/KRPC/Manager.hs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs index 58ac7674..f31a3cd6 100644 --- a/src/Network/KRPC/Manager.hs +++ b/src/Network/KRPC/Manager.hs | |||
@@ -78,11 +78,13 @@ import Data.Tuple | |||
78 | import Data.Typeable | 78 | import Data.Typeable |
79 | import Network.RPC | 79 | import Network.RPC |
80 | import Network.KRPC.Message | 80 | import Network.KRPC.Message |
81 | import Network.KRPC.Method | 81 | import Network.KRPC.Method hiding (Envelope) |
82 | import qualified Network.KRPC.Method as KRPC (Envelope) | ||
82 | import Network.Socket hiding (listen) | 83 | import Network.Socket hiding (listen) |
83 | import Network.Socket.ByteString as BS | 84 | import Network.Socket.ByteString as BS |
84 | import System.IO.Error | 85 | import System.IO.Error |
85 | import System.Timeout | 86 | import System.Timeout |
87 | import Network.DHT.Mainline | ||
86 | 88 | ||
87 | 89 | ||
88 | {----------------------------------------------------------------------- | 90 | {----------------------------------------------------------------------- |
@@ -130,14 +132,14 @@ validateOptions Options {..} | |||
130 | -- Options | 132 | -- Options |
131 | -----------------------------------------------------------------------} | 133 | -----------------------------------------------------------------------} |
132 | 134 | ||
133 | type KResult = Either KError KResponse | 135 | type KResult = Either KError KMessage -- Response |
134 | 136 | ||
135 | type TransactionCounter = IORef Int | 137 | type TransactionCounter = IORef Int |
136 | type CallId = (TransactionId, SockAddr) | 138 | type CallId = (TransactionId, SockAddr) |
137 | type CallRes = MVar (KQueryArgs, KResult) -- (raw response, decoded response) | 139 | type CallRes = MVar (KQueryArgs, KResult) -- (raw response, decoded response) |
138 | type PendingCalls = IORef (Map CallId CallRes) | 140 | type PendingCalls = IORef (Map CallId CallRes) |
139 | 141 | ||
140 | type HandlerBody h msg v = SockAddr -> msg v -> h (Either String v) | 142 | type HandlerBody h msg v = SockAddr -> msg v -> h (Either String (msg v)) |
141 | 143 | ||
142 | -- | Handler is a function which will be invoked then some /remote/ | 144 | -- | Handler is a function which will be invoked then some /remote/ |
143 | -- node querying /this/ node. | 145 | -- node querying /this/ node. |
@@ -362,7 +364,7 @@ queryK addr params kont = do | |||
362 | case res of | 364 | case res of |
363 | #ifdef VERSION_bencoding | 365 | #ifdef VERSION_bencoding |
364 | Left (KError c m _) -> throwIO $ QueryFailed c (T.decodeUtf8 m) | 366 | Left (KError c m _) -> throwIO $ QueryFailed c (T.decodeUtf8 m) |
365 | Right (KResponse {..}) -> | 367 | Right (R (KResponse {..})) -> |
366 | case fromBEncode respVals of | 368 | case fromBEncode respVals of |
367 | Right r -> pure $ kont raw r respIP | 369 | Right r -> pure $ kont raw r respIP |
368 | #else | 370 | #else |
@@ -421,15 +423,16 @@ prettyQF e = T.encodeUtf8 $ "handler fail while performing query: " | |||
421 | -- If the handler make some 'query' normally it /should/ handle | 423 | -- If the handler make some 'query' normally it /should/ handle |
422 | -- corresponding 'QueryFailure's. | 424 | -- corresponding 'QueryFailure's. |
423 | -- | 425 | -- |
424 | handler :: forall h a b msg. (KRPC a b, Applicative h, Functor msg) | 426 | handler :: forall h a b msg raw. (KRPC a b, Applicative h, Functor msg, WireFormat raw msg, SerializableTo raw a, SerializableTo raw b) |
425 | => Messaging msg TransactionId (Envelope a b) -> (SockAddr -> a -> h b) -> Handler h msg (Envelope a b) | 427 | => (SockAddr -> a -> h b) -> Handler h msg raw |
426 | handler msging body = (name, wrapper) | 428 | handler body = (name, wrapper) |
427 | where | 429 | where |
428 | Method name = method :: Method a b | 430 | Method name = method :: Method a b |
431 | wrapper :: SockAddr -> msg raw -> h (Either String (msg raw)) | ||
429 | wrapper addr args = | 432 | wrapper addr args = |
430 | case unseal $ messagePayload msging args of | 433 | case decodePayload args of |
431 | Left e -> pure $ Left e | 434 | Left e -> pure $ Left e |
432 | Right a -> Right . seal <$> body addr a | 435 | Right a -> Right . encodePayload . buildReply (error "self node-id") addr args <$> body addr (envelopePayload a) |
433 | 436 | ||
434 | runHandler :: MonadKRPC h m | 437 | runHandler :: MonadKRPC h m |
435 | #ifdef VERSION_bencoding | 438 | #ifdef VERSION_bencoding |
@@ -461,7 +464,7 @@ runHandler h addr m = Lifted.catches wrapper failbacks | |||
461 | Right a -> do -- KQueryArgs | 464 | Right a -> do -- KQueryArgs |
462 | $(logDebugS) "handler.success" signature | 465 | $(logDebugS) "handler.success" signature |
463 | #ifdef VERSION_bencoding | 466 | #ifdef VERSION_bencoding |
464 | return $ Right $ KResponse a (queryId m) (Just $ ReflectedIP addr) | 467 | return $ Right a |
465 | #else | 468 | #else |
466 | let cli = error "TODO TOX client node id" | 469 | let cli = error "TODO TOX client node id" |
467 | messageid = error "TODO TOX message response id" | 470 | messageid = error "TODO TOX message response id" |
@@ -542,7 +545,7 @@ handleResponse raw result addr = do | |||
542 | Manager {..} <- getManager | 545 | Manager {..} <- getManager |
543 | liftIO $ do | 546 | liftIO $ do |
544 | #ifdef VERSION_bencoding | 547 | #ifdef VERSION_bencoding |
545 | let resultId = either errorId respId result | 548 | let resultId = either errorId envelopeTransaction result |
546 | #else | 549 | #else |
547 | let resultId = either Tox.msgNonce Tox.msgNonce result | 550 | let resultId = either Tox.msgNonce Tox.msgNonce result |
548 | #endif | 551 | #endif |
@@ -554,7 +557,7 @@ handleResponse raw result addr = do | |||
554 | #ifdef VERSION_bencoding | 557 | #ifdef VERSION_bencoding |
555 | handleMessage :: MonadKRPC h m => KQueryArgs -> KMessage -> SockAddr -> m () | 558 | handleMessage :: MonadKRPC h m => KQueryArgs -> KMessage -> SockAddr -> m () |
556 | handleMessage raw (Q q) = handleQuery raw q | 559 | handleMessage raw (Q q) = handleQuery raw q |
557 | handleMessage raw (R r) = handleResponse raw (Right r) | 560 | handleMessage raw (R r) = handleResponse raw (Right (R r)) |
558 | handleMessage raw (E e) = handleResponse raw (Left e) | 561 | handleMessage raw (E e) = handleResponse raw (Left e) |
559 | #else | 562 | #else |
560 | handleMessage :: MonadKRPC h m => KQueryArgs -> Tox.Message BC.ByteString -> SockAddr -> m () | 563 | handleMessage :: MonadKRPC h m => KQueryArgs -> Tox.Message BC.ByteString -> SockAddr -> m () |