summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Session.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-04 00:54:49 -0500
committerjoe <joe@jerkface.net>2017-01-04 00:54:49 -0500
commit2fd473635dba00f7af37401058522a29460392fc (patch)
tree25e90513b252b5361201c524175851b63e12ca06 /src/Network/BitTorrent/DHT/Session.hs
parent19aa76afa7349cc3c91111b38ab3012f63380433 (diff)
Made node refresh into full iterative search.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs64
1 files changed, 1 insertions, 63 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index ffce47de..2bb3ce85 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -30,6 +30,7 @@ module Network.BitTorrent.DHT.Session
30 , Node 30 , Node
31 , options 31 , options
32 , thisNodeId 32 , thisNodeId
33 , routingTable
33 34
34 -- ** Initialization 35 -- ** Initialization
35 , LogFun 36 , LogFun
@@ -50,7 +51,6 @@ module Network.BitTorrent.DHT.Session
50 -- ** Routing table 51 -- ** Routing table
51 , getTable 52 , getTable
52 , getClosest 53 , getClosest
53 , insertNode
54 54
55 -- ** Peer storage 55 -- ** Peer storage
56 , insertPeer 56 , insertPeer
@@ -59,9 +59,7 @@ module Network.BitTorrent.DHT.Session
59 , deleteTopic 59 , deleteTopic
60 60
61 -- ** Messaging 61 -- ** Messaging
62 , queryNode
63 , queryParallel 62 , queryParallel
64 , (<@>)
65 ) where 63 ) where
66 64
67import Prelude hiding (ioError) 65import Prelude hiding (ioError)
@@ -347,35 +345,9 @@ runDHT node action = runReaderT (unDHT action) node
347-- Routing 345-- Routing
348-----------------------------------------------------------------------} 346-----------------------------------------------------------------------}
349 347
350routing :: Address ip => Routing ip a -> DHT ip (Maybe a)
351routing = runRouting probeNode refreshNodes getTimestamp
352
353probeNode :: Address ip => NodeAddr ip -> DHT ip Bool
354probeNode addr = do
355 $(logDebugS) "routing.questionable_node" (T.pack (render (pPrint addr)))
356 result <- try $ Ping <@> addr
357 let _ = result :: Either SomeException Ping
358 return $ either (const False) (const True) result
359
360-- /pick a random ID/ in the range of the bucket and perform a 348-- /pick a random ID/ in the range of the bucket and perform a
361-- find_nodes search on it. 349-- find_nodes search on it.
362 350
363-- FIXME do not use getClosest sinse we should /refresh/ them
364refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip]
365refreshNodes nid = do
366 $(logDebugS) "routing.refresh_bucket" (T.pack (render (pPrint nid)))
367 nodes <- getClosest nid
368 nss <- forM (nodeAddr <$> nodes) $ \ addr -> do
369 NodeFound ns <- FindNode nid <@> addr
370 return ns
371 return $ L.concat nss
372
373getTimestamp :: DHT ip Timestamp
374getTimestamp = do
375 utcTime <- liftIO $ getCurrentTime
376 $(logDebugS) "routing.make_timestamp" (T.pack (render (pPrint utcTime)))
377 return $ utcTimeToPOSIXSeconds utcTime
378
379{----------------------------------------------------------------------- 351{-----------------------------------------------------------------------
380-- Tokens 352-- Tokens
381-----------------------------------------------------------------------} 353-----------------------------------------------------------------------}
@@ -421,24 +393,6 @@ getClosest node = do
421 k <- asks (optK . options) 393 k <- asks (optK . options)
422 kclosest k node <$> getTable 394 kclosest k node <$> getTable
423 395
424-- | This operation do not block but acquire exclusive access to
425-- routing table.
426insertNode :: Address ip => NodeInfo ip -> DHT ip ThreadId
427insertNode info = fork $ do
428 var <- asks routingTable
429 modifyMVar_ var $ \ t -> do
430 result <- routing (R.insert info t)
431 case result of
432 Nothing -> do
433 $(logDebugS) "insertNode" $ "Routing table is full: "
434 <> T.pack (show (pPrint t))
435 return t
436 Just t' -> do
437 let logMsg = "Routing table updated: "
438 <> pPrint t <> " -> " <> pPrint t'
439 $(logDebugS) "insertNode" (T.pack (render logMsg))
440 return t'
441
442{----------------------------------------------------------------------- 396{-----------------------------------------------------------------------
443-- Peer storage 397-- Peer storage
444-----------------------------------------------------------------------} 398-----------------------------------------------------------------------}
@@ -487,22 +441,6 @@ deleteTopic ih p = do
487-- Messaging 441-- Messaging
488-----------------------------------------------------------------------} 442-----------------------------------------------------------------------}
489 443
490-- | Throws exception if node is not responding.
491queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b)
492 => NodeAddr ip -> a -> DHT ip (NodeId, b)
493queryNode addr q = do
494 nid <- asks thisNodeId
495 let read_only = False -- TODO: check for NAT issues. (BEP 43)
496 Response remoteId r <- query (toSockAddr addr) (Query nid read_only q)
497 insertNode (NodeInfo remoteId addr)
498 return (remoteId, r)
499
500-- | Infix version of 'queryNode' function.
501(<@>) :: Address ip => KRPC (Query a) (Response b)
502 => a -> NodeAddr ip -> DHT ip b
503q <@> addr = snd <$> queryNode addr q
504{-# INLINE (<@>) #-}
505
506-- TODO: use alpha 444-- TODO: use alpha
507-- | Failed queries are ignored. 445-- | Failed queries are ignored.
508queryParallel :: [DHT ip a] -> DHT ip [a] 446queryParallel :: [DHT ip a] -> DHT ip [a]