summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Routing.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-07 20:50:33 -0500
committerjoe <joe@jerkface.net>2017-01-08 16:35:59 -0500
commita18fe8a84025b3f0beb357eba73f37d77244a44a (patch)
tree6cad0091df7d6aaceaa4f88be0a29fd320a8abba /src/Network/BitTorrent/DHT/Routing.hs
parentbcd860aa8816cf52a01c313aecfdcde21fcd2c16 (diff)
Use BEP 42 compatible node ids.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Routing.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs
index a4da8445..84e4d4ce 100644
--- a/src/Network/BitTorrent/DHT/Routing.hs
+++ b/src/Network/BitTorrent/DHT/Routing.hs
@@ -22,6 +22,7 @@
22module Network.BitTorrent.DHT.Routing 22module Network.BitTorrent.DHT.Routing
23 ( -- * Table 23 ( -- * Table
24 Table 24 Table
25 , Info(..)
25 26
26 -- * Attributes 27 -- * Attributes
27 , BucketCount 28 , BucketCount
@@ -343,6 +344,15 @@ type BucketCount = Int
343defaultBucketCount :: BucketCount 344defaultBucketCount :: BucketCount
344defaultBucketCount = 20 345defaultBucketCount = 20
345 346
347data Info ip = Info
348 { myBuckets :: Table ip
349 , myNodeId :: NodeId
350 , myAddress :: ip
351 }
352 deriving (Eq, Show, Generic)
353
354instance (Eq ip, Serialize ip) => Serialize (Info ip)
355
346-- | The routing table covers the entire 'NodeId' space from 0 to 2 ^ 356-- | The routing table covers the entire 'NodeId' space from 0 to 2 ^
347-- 160. The routing table is subdivided into 'Bucket's that each cover 357-- 160. The routing table is subdivided into 'Bucket's that each cover
348-- a portion of the space. An empty table has one bucket with an ID 358-- a portion of the space. An empty table has one bucket with an ID
@@ -497,7 +507,7 @@ instance TableKey InfoHash where
497-- 'find_node' and 'get_peers' queries. 507-- 'find_node' and 'get_peers' queries.
498kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip] 508kclosest :: Eq ip => TableKey a => K -> a -> Table ip -> [NodeInfo ip]
499kclosest k (toNodeId -> nid) 509kclosest k (toNodeId -> nid)
500 = L.take k . rank nid 510 = L.take k . rank nodeId nid
501 . L.map PSQ.key . PSQ.toList . fromMaybe PSQ.empty 511 . L.map PSQ.key . PSQ.toList . fromMaybe PSQ.empty
502 . fmap bktNodes 512 . fmap bktNodes
503 . lookupBucket nid 513 . lookupBucket nid
@@ -536,8 +546,10 @@ modifyBucket nodeId f = go (0 :: BitIx)
536 <|> go i (splitTip nid n i bucket) 546 <|> go i (splitTip nid n i bucket)
537 547
538-- | Triggering event for atomic table update 548-- | Triggering event for atomic table update
539data Event ip = TryInsert (NodeInfo ip) 549data Event ip = TryInsert { foreignNode :: NodeInfo ip }
540 | PingResult (NodeInfo ip) Bool 550 | PingResult { foreignNode :: NodeInfo ip
551 , ponged :: Bool
552 }
541 deriving (Eq,Ord,Show) 553 deriving (Eq,Ord,Show)
542 554
543eventId (TryInsert NodeInfo{..}) = nodeId 555eventId (TryInsert NodeInfo{..}) = nodeId