From 5d5c52d5a558b49fb8db814198315627d1c119e5 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 23 Jan 2014 02:44:25 +0400 Subject: Add convertion from routing table to list --- src/Network/BitTorrent/DHT/Routing.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/Network/BitTorrent/DHT/Routing.hs') diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs index 73fe358a..f62c9f56 100644 --- a/src/Network/BitTorrent/DHT/Routing.hs +++ b/src/Network/BitTorrent/DHT/Routing.hs @@ -47,6 +47,10 @@ module Network.BitTorrent.DHT.Routing , Network.BitTorrent.DHT.Routing.nullTable , Network.BitTorrent.DHT.Routing.insert + -- * Conversion + , Network.BitTorrent.DHT.Routing.TableEntry + , Network.BitTorrent.DHT.Routing.toList + -- * Routing , Timestamp , Routing @@ -428,3 +432,21 @@ insert info @ NodeInfo {..} = go (0 :: BitIx) | n == 0 = Tip nid n <$> insertNode info bucket | otherwise = Tip nid n <$> insertNode info bucket <|> go (succ i) (splitTip nid n i bucket) + +{----------------------------------------------------------------------- +-- Conversion +-----------------------------------------------------------------------} + +type TableEntry ip = (NodeInfo ip, Timestamp) + +tableEntry :: NodeEntry ip -> TableEntry ip +tableEntry (a :-> b) = (a, b) + +-- | Non-empty list of buckets. +toBucketList :: Table ip -> [Bucket ip] +toBucketList (Tip _ _ b) = [b] +toBucketList (Zero t b) = b : toBucketList t +toBucketList (One b t) = b : toBucketList t + +toList :: Eq ip => Table ip -> [[TableEntry ip]] +toList = L.map (L.map tableEntry . PSQ.toList) . toBucketList -- cgit v1.2.3