summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Routing.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-02-01 03:21:52 -0500
committerjoe <joe@jerkface.net>2017-02-01 03:21:52 -0500
commitc51e64666b672637843a04c2f279d7d0c9eed01c (patch)
treed6f50018659ac3c5c3d72ee9bde3824514bd9f6a /src/Network/BitTorrent/DHT/Routing.hs
parent0d1de683de78a70ce9c054b444bb6f19c39d112c (diff)
New improved iterative search algorithm.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Routing.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs31
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
442depth :: Table ip -> BucketCount 442depth :: Table ip -> BucketCount
443depth = L.length . shape 443depth = L.length . shape
444 444
445lookupBucket :: NodeId -> Table ip -> Maybe (Bucket ip) 445lookupBucket :: NodeId -> Table ip -> [Bucket ip]
446lookupBucket nid = go 0 446lookupBucket 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
456compatibleNodeId :: Table ip -> IO NodeId 456compatibleNodeId :: Table ip -> IO NodeId
457compatibleNodeId tbl = genBucketSample prefix br 457compatibleNodeId 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.
506kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip] 506kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip]
507kclosest k (toNodeId -> nid) 507kclosest 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