diff options
author | joe <joe@jerkface.net> | 2017-01-04 01:26:51 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-01-04 01:26:51 -0500 |
commit | 990296703f511efe2bc2899d514dbe2a20247c88 (patch) | |
tree | a8b0032f5ae7436747af5ab41110da12834cd9df /src/Network/BitTorrent/DHT | |
parent | 2fd473635dba00f7af37401058522a29460392fc (diff) |
Routing table snapshot/restore.
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 8 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Routing.hs | 8 |
2 files changed, 16 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index c5fcccb4..7f20ad6d 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -41,6 +41,7 @@ module Network.BitTorrent.DHT.Query | |||
41 | , publish | 41 | , publish |
42 | 42 | ||
43 | -- ** Routing table | 43 | -- ** Routing table |
44 | , restoreTable | ||
44 | , insertNode | 45 | , insertNode |
45 | , refreshNodes | 46 | , refreshNodes |
46 | 47 | ||
@@ -266,3 +267,10 @@ queryNode addr q = do | |||
266 | q <@> addr = snd <$> queryNode addr q | 267 | q <@> addr = snd <$> queryNode addr q |
267 | {-# INLINE (<@>) #-} | 268 | {-# INLINE (<@>) #-} |
268 | 269 | ||
270 | restoreTable :: Address ip => Table ip -> DHT ip () | ||
271 | restoreTable tbl = do | ||
272 | tblvar <- asks routingTable | ||
273 | tbl0 <- liftIO $ takeMVar tblvar | ||
274 | mb <- routing $ merge tbl tbl0 | ||
275 | maybe (return ()) (liftIO . putMVar tblvar) mb | ||
276 | |||
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs index fee52380..68edef56 100644 --- a/src/Network/BitTorrent/DHT/Routing.hs +++ b/src/Network/BitTorrent/DHT/Routing.hs | |||
@@ -46,6 +46,7 @@ module Network.BitTorrent.DHT.Routing | |||
46 | -- * Construction | 46 | -- * Construction |
47 | , Network.BitTorrent.DHT.Routing.nullTable | 47 | , Network.BitTorrent.DHT.Routing.nullTable |
48 | , Network.BitTorrent.DHT.Routing.insert | 48 | , Network.BitTorrent.DHT.Routing.insert |
49 | , Network.BitTorrent.DHT.Routing.merge | ||
49 | 50 | ||
50 | -- * Conversion | 51 | -- * Conversion |
51 | , Network.BitTorrent.DHT.Routing.TableEntry | 52 | , Network.BitTorrent.DHT.Routing.TableEntry |
@@ -457,3 +458,10 @@ toBucketList (One b t) = b : toBucketList t | |||
457 | 458 | ||
458 | toList :: Eq ip => Table ip -> [[TableEntry ip]] | 459 | toList :: Eq ip => Table ip -> [[TableEntry ip]] |
459 | toList = L.map (L.map tableEntry . PSQ.toList) . toBucketList | 460 | toList = L.map (L.map tableEntry . PSQ.toList) . toBucketList |
461 | |||
462 | merge :: Eq ip => Table ip -> Table ip -> Routing ip (Table ip) | ||
463 | merge a b = do | ||
464 | let ns = concatMap PSQ.toList $ toBucketList a | ||
465 | -- TODO: merge timestamps as well and let refresh take care of ping. | ||
466 | as <- filterM (needPing . nodeAddr . PSQ.key) ns | ||
467 | foldM (flip $ Network.BitTorrent.DHT.Routing.insert) b $ map PSQ.key as | ||