diff options
Diffstat (limited to 'dht/src/Network/Tox/DHT/Handlers.hs')
-rw-r--r-- | dht/src/Network/Tox/DHT/Handlers.hs | 69 |
1 files changed, 14 insertions, 55 deletions
diff --git a/dht/src/Network/Tox/DHT/Handlers.hs b/dht/src/Network/Tox/DHT/Handlers.hs index e97cab96..e93f565b 100644 --- a/dht/src/Network/Tox/DHT/Handlers.hs +++ b/dht/src/Network/Tox/DHT/Handlers.hs | |||
@@ -400,61 +400,20 @@ 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 | |||
446 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) | 403 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) |
447 | getNodes client cbvar nid addr = | 404 | getNodes client cbvar nid addr = do |
448 | sendQ (getNodesSendable cbvar addr) client nid addr | 405 | -- dput XMisc $ show addr ++ " <-- getnodes " ++ show nid |
449 | 406 | reply <- QR.sendQuery client (serializer GetNodesType DHTGetNodes unsendNodes) (GetNodes nid) addr | |
450 | asyncGetNodes :: QR.Client err PacketKind TransactionId NodeInfo Message | 407 | -- dput XMisc $ show addr ++ " -sendnodes-> " ++ show reply |
451 | -> TVar (HashMap NodeId [NodeInfoCallback]) | 408 | forM_ (join reply) $ \(SendNodes ns) -> |
452 | -> NodeId | 409 | forM_ ns $ \n -> do |
453 | -> NodeInfo | 410 | now <- getPOSIXTime |
454 | -> (Maybe ([NodeInfo], [NodeInfo], Maybe ()) -> IO ()) | 411 | atomically $ do |
455 | -> IO () | 412 | mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar |
456 | asyncGetNodes client cbvar nid addr go = | 413 | forM_ mcbs $ \cbs -> do |
457 | asyncQ (getNodesSendable cbvar addr) client nid addr go | 414 | forM_ cbs $ \cb -> do |
415 | rumoredAddress cb now (nodeAddr addr) n | ||
416 | return $ fmap unwrapNodes $ join reply | ||
458 | 417 | ||
459 | updateRouting :: Client -> Routing | 418 | updateRouting :: Client -> Routing |
460 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) | 419 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) |
@@ -566,7 +525,7 @@ nodeSearch :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> Search NodeI | |||
566 | nodeSearch client cbvar = Search | 525 | nodeSearch client cbvar = Search |
567 | { searchSpace = toxSpace | 526 | { searchSpace = toxSpace |
568 | , searchNodeAddress = nodeIP &&& nodePort | 527 | , searchNodeAddress = nodeIP &&& nodePort |
569 | , searchQuery = Right $ asyncGetNodes client cbvar | 528 | , searchQuery = Left $ getNodes client cbvar |
570 | , searchAlpha = 8 | 529 | , searchAlpha = 8 |
571 | , searchK = 16 | 530 | , searchK = 16 |
572 | 531 | ||