diff options
author | Joe Crayne <joe@jerkface.net> | 2019-01-17 03:42:32 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-01-17 03:42:32 -0500 |
commit | 46b1ebb81838dc7ecf94533b25cd51e84bd0cf04 (patch) | |
tree | dd92015778558c13b4fba70c310c8ffd08e37820 /src/Network/Tox/DHT | |
parent | 6ebe91b686ca8bef893f9a3dd704e45c04124b8f (diff) |
Use async queries for all UDP kademlia searches.
Diffstat (limited to 'src/Network/Tox/DHT')
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs index abd607c3..1eec93b9 100644 --- a/src/Network/Tox/DHT/Handlers.hs +++ b/src/Network/Tox/DHT/Handlers.hs | |||
@@ -400,20 +400,61 @@ unsendNodes _ = Nothing | |||
400 | unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () ) | 400 | unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () ) |
401 | unwrapNodes (SendNodes ns) = (ns,ns,Just ()) | 401 | unwrapNodes (SendNodes ns) = (ns,ns,Just ()) |
402 | 402 | ||
403 | data SendableQuery x a b = SendableQuery | ||
404 | { sendableSerializer :: MethodSerializer TransactionId NodeInfo Message PacketKind a (Maybe x) | ||
405 | , sendableQuery :: NodeId -> a | ||
406 | , sendableResult :: Maybe (Maybe x) -> IO b | ||
407 | } | ||
408 | |||
409 | sendQ :: SendableQuery x a b | ||
410 | -> QR.Client err PacketKind TransactionId NodeInfo Message | ||
411 | -> NodeId | ||
412 | -> NodeInfo | ||
413 | -> IO b | ||
414 | sendQ s client nid addr = do | ||
415 | reply <- QR.sendQuery client (sendableSerializer s) (sendableQuery s nid) addr | ||
416 | sendableResult s reply | ||
417 | |||
418 | asyncQ :: SendableQuery x a b | ||
419 | -> QR.Client err PacketKind TransactionId NodeInfo Message | ||
420 | -> NodeId | ||
421 | -> NodeInfo | ||
422 | -> (b -> IO ()) | ||
423 | -> IO () | ||
424 | asyncQ s client nid addr go = do | ||
425 | QR.asyncQuery client (sendableSerializer s) (sendableQuery s nid) addr | ||
426 | $ sendableResult s >=> go | ||
427 | |||
428 | getNodesSendable :: TVar (HashMap NodeId [NodeInfoCallback]) | ||
429 | -> NodeInfo | ||
430 | -> SendableQuery SendNodes GetNodes (Maybe ([NodeInfo], [NodeInfo], Maybe ())) | ||
431 | getNodesSendable cbvar addr = SendableQuery (serializer GetNodesType DHTGetNodes unsendNodes) | ||
432 | GetNodes | ||
433 | go | ||
434 | where | ||
435 | go reply = do | ||
436 | forM_ (join reply) $ \(SendNodes ns) -> | ||
437 | forM_ ns $ \n -> do | ||
438 | now <- getPOSIXTime | ||
439 | atomically $ do | ||
440 | mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar | ||
441 | forM_ mcbs $ \cbs -> do | ||
442 | forM_ cbs $ \cb -> do | ||
443 | rumoredAddress cb now (nodeAddr addr) n | ||
444 | return $ fmap unwrapNodes $ join reply | ||
445 | |||
403 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) | 446 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) |
404 | getNodes client cbvar nid addr = do | 447 | getNodes client cbvar nid addr = |
405 | -- dput XMisc $ show addr ++ " <-- getnodes " ++ show nid | 448 | sendQ (getNodesSendable cbvar addr) client nid addr |
406 | reply <- QR.sendQuery client (serializer GetNodesType DHTGetNodes unsendNodes) (GetNodes nid) addr | 449 | |
407 | -- dput XMisc $ show addr ++ " -sendnodes-> " ++ show reply | 450 | asyncGetNodes :: QR.Client err PacketKind TransactionId NodeInfo Message |
408 | forM_ (join reply) $ \(SendNodes ns) -> | 451 | -> TVar (HashMap NodeId [NodeInfoCallback]) |
409 | forM_ ns $ \n -> do | 452 | -> NodeId |
410 | now <- getPOSIXTime | 453 | -> NodeInfo |
411 | atomically $ do | 454 | -> (Maybe ([NodeInfo], [NodeInfo], Maybe ()) -> IO ()) |
412 | mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar | 455 | -> IO () |
413 | forM_ mcbs $ \cbs -> do | 456 | asyncGetNodes client cbvar nid addr go = |
414 | forM_ cbs $ \cb -> do | 457 | asyncQ (getNodesSendable cbvar addr) client nid addr go |
415 | rumoredAddress cb now (nodeAddr addr) n | ||
416 | return $ fmap unwrapNodes $ join reply | ||
417 | 458 | ||
418 | updateRouting :: Client -> Routing | 459 | updateRouting :: Client -> Routing |
419 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) | 460 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) |
@@ -525,7 +566,7 @@ nodeSearch :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> Search NodeI | |||
525 | nodeSearch client cbvar = Search | 566 | nodeSearch client cbvar = Search |
526 | { searchSpace = toxSpace | 567 | { searchSpace = toxSpace |
527 | , searchNodeAddress = nodeIP &&& nodePort | 568 | , searchNodeAddress = nodeIP &&& nodePort |
528 | , searchQuery = Left $ getNodes client cbvar | 569 | , searchQuery = Right $ asyncGetNodes client cbvar |
529 | , searchAlpha = 8 | 570 | , searchAlpha = 8 |
530 | , searchK = 16 | 571 | , searchK = 16 |
531 | 572 | ||