summaryrefslogtreecommitdiff
path: root/dht/src/Network/Tox/DHT/Handlers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network/Tox/DHT/Handlers.hs')
-rw-r--r--dht/src/Network/Tox/DHT/Handlers.hs69
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
400unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () ) 400unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () )
401unwrapNodes (SendNodes ns) = (ns,ns,Just ()) 401unwrapNodes (SendNodes ns) = (ns,ns,Just ())
402 402
403data 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
409sendQ :: SendableQuery x a b
410 -> QR.Client err PacketKind TransactionId NodeInfo Message
411 -> NodeId
412 -> NodeInfo
413 -> IO b
414sendQ s client nid addr = do
415 reply <- QR.sendQuery client (sendableSerializer s) (sendableQuery s nid) addr
416 sendableResult s reply
417
418asyncQ :: SendableQuery x a b
419 -> QR.Client err PacketKind TransactionId NodeInfo Message
420 -> NodeId
421 -> NodeInfo
422 -> (b -> IO ())
423 -> IO ()
424asyncQ s client nid addr go = do
425 QR.asyncQuery client (sendableSerializer s) (sendableQuery s nid) addr
426 $ sendableResult s >=> go
427
428getNodesSendable :: TVar (HashMap NodeId [NodeInfoCallback])
429 -> NodeInfo
430 -> SendableQuery SendNodes GetNodes (Maybe ([NodeInfo], [NodeInfo], Maybe ()))
431getNodesSendable 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
446getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) 403getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ()))
447getNodes client cbvar nid addr = 404getNodes 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
450asyncGetNodes :: 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
456asyncGetNodes 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
459updateRouting :: Client -> Routing 418updateRouting :: 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
566nodeSearch client cbvar = Search 525nodeSearch 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