diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT/Routing.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Routing.hs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs index 5c9788dc..cf4a4de3 100644 --- a/src/Network/BitTorrent/DHT/Routing.hs +++ b/src/Network/BitTorrent/DHT/Routing.hs | |||
@@ -442,16 +442,16 @@ size = L.sum . shape | |||
442 | depth :: Table ip -> BucketCount | 442 | depth :: Table ip -> BucketCount |
443 | depth = L.length . shape | 443 | depth = L.length . shape |
444 | 444 | ||
445 | lookupBucket :: NodeId -> Table ip -> Maybe (Bucket ip) | 445 | lookupBucket :: NodeId -> Table ip -> [Bucket ip] |
446 | lookupBucket nid = go 0 | 446 | lookupBucket nid = go 0 [] |
447 | where | 447 | where |
448 | go i (Zero table bucket) | 448 | go i bs (Zero table bucket) |
449 | | testIdBit nid i = pure bucket | 449 | | testIdBit nid i = bucket : toBucketList table ++ bs |
450 | | otherwise = go (succ i) table | 450 | | otherwise = go (succ i) (bucket:bs) table |
451 | go i (One bucket table) | 451 | go i bs (One bucket table) |
452 | | testIdBit nid i = go (succ i) table | 452 | | testIdBit nid i = go (succ i) (bucket:bs) table |
453 | | otherwise = pure bucket | 453 | | otherwise = bucket : toBucketList table ++ bs |
454 | go _ (Tip _ _ bucket) = pure bucket | 454 | go _ bs (Tip _ _ bucket) = bucket : bs |
455 | 455 | ||
456 | compatibleNodeId :: Table ip -> IO NodeId | 456 | compatibleNodeId :: Table ip -> IO NodeId |
457 | compatibleNodeId tbl = genBucketSample prefix br | 457 | compatibleNodeId tbl = genBucketSample prefix br |
@@ -504,11 +504,14 @@ instance TableKey InfoHash where | |||
504 | -- | Get a list of /K/ closest nodes using XOR metric. Used in | 504 | -- | Get a list of /K/ closest nodes using XOR metric. Used in |
505 | -- 'find_node' and 'get_peers' queries. | 505 | -- 'find_node' and 'get_peers' queries. |
506 | kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip] | 506 | kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip] |
507 | kclosest k (toNodeId -> nid) | 507 | kclosest k (toNodeId -> nid) tbl = take k $ rank nodeId nid (L.concat bucket) |
508 | = L.take k . rank nodeId nid | 508 | ++ rank nodeId nid (L.concat everyone) |
509 | . L.map PSQ.key . PSQ.toList . fromMaybe PSQ.empty | 509 | where |
510 | . fmap bktNodes | 510 | (bucket,everyone) = |
511 | . lookupBucket nid | 511 | L.splitAt 1 |
512 | . L.map (L.map PSQ.key . PSQ.toList . bktNodes) | ||
513 | . lookupBucket nid | ||
514 | $ tbl | ||
512 | 515 | ||
513 | {----------------------------------------------------------------------- | 516 | {----------------------------------------------------------------------- |
514 | -- Routing | 517 | -- Routing |