summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r--src/Network/BitTorrent/DHT/Query.hs16
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs10
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs9
3 files changed, 24 insertions, 11 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs
index 73b3d492..533068c6 100644
--- a/src/Network/BitTorrent/DHT/Query.hs
+++ b/src/Network/BitTorrent/DHT/Query.hs
@@ -9,6 +9,7 @@
9-- Normally, you don't need to import this module, use 9-- Normally, you don't need to import this module, use
10-- "Network.BitTorrent.DHT" instead. 10-- "Network.BitTorrent.DHT" instead.
11-- 11--
12{-# LANGUAGE CPP #-}
12{-# LANGUAGE FlexibleContexts #-} 13{-# LANGUAGE FlexibleContexts #-}
13{-# LANGUAGE ScopedTypeVariables #-} 14{-# LANGUAGE ScopedTypeVariables #-}
14{-# LANGUAGE TemplateHaskell #-} 15{-# LANGUAGE TemplateHaskell #-}
@@ -49,7 +50,12 @@ module Network.BitTorrent.DHT.Query
49 , (<@>) 50 , (<@>)
50 ) where 51 ) where
51 52
53#ifdef THREAD_DEBUG
54import Control.Concurrent.Lifted.Instrument hiding (yield)
55#else
56import GHC.Conc (labelThread)
52import Control.Concurrent.Lifted hiding (yield) 57import Control.Concurrent.Lifted hiding (yield)
58#endif
53import Control.Exception.Lifted hiding (Handler) 59import Control.Exception.Lifted hiding (Handler)
54import Control.Monad.Reader 60import Control.Monad.Reader
55import Control.Monad.Logger 61import Control.Monad.Logger
@@ -225,8 +231,8 @@ refreshNodes nid = do
225 231
226-- | This operation do not block but acquire exclusive access to 232-- | This operation do not block but acquire exclusive access to
227-- routing table. 233-- routing table.
228insertNode :: forall ip. Address ip => NodeInfo ip -> Maybe ReflectedIP -> DHT ip ThreadId 234insertNode :: forall ip. Address ip => NodeInfo ip -> Maybe ReflectedIP -> DHT ip ()
229insertNode info witnessed_ip0 = fork $ do 235insertNode info witnessed_ip0 = do
230 var <- asks routingInfo 236 var <- asks routingInfo
231 tm <- getTimestamp 237 tm <- getTimestamp
232 let showTable = do 238 let showTable = do
@@ -286,8 +292,10 @@ insertNode info witnessed_ip0 = fork $ do
286 return ps 292 return ps
287 ps <- join $ liftIO $ atomically $ atomicInsert arrival0 witnessed_ip0 293 ps <- join $ liftIO $ atomically $ atomicInsert arrival0 witnessed_ip0
288 showTable 294 showTable
289 _ <- fork $ forM_ ps $ \(CheckPing ns)-> do 295 _ <- fork $ do
290 forM_ ns $ \n -> do 296 myThreadId >>= liftIO . flip labelThread "DHT.insertNode.pingResults"
297 forM_ ps $ \(CheckPing ns)-> do
298 forM_ ns $ \n -> do
291 (b,mip) <- probeNode (nodeAddr n) 299 (b,mip) <- probeNode (nodeAddr n)
292 let alive = PingResult n b 300 let alive = PingResult n b
293 $(logDebugS) "insertNode" $ T.pack ("PingResult "++show (nodeId n,b)) 301 $(logDebugS) "insertNode" $ T.pack ("PingResult "++show (nodeId n,b))
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs
index 8a6849a1..d64e415e 100644
--- a/src/Network/BitTorrent/DHT/Routing.hs
+++ b/src/Network/BitTorrent/DHT/Routing.hs
@@ -529,11 +529,11 @@ splitTip nid n i bucket
529-- k nodes in them. Which subtrees I mean is illustrated in Fig 1. of Kademlia 529-- k nodes in them. Which subtrees I mean is illustrated in Fig 1. of Kademlia
530-- paper. The rule requiring additional splits is in section 2.4. 530-- paper. The rule requiring additional splits is in section 2.4.
531modifyBucket 531modifyBucket
532 :: forall f ip xs. (Alternative f, Eq ip) => 532 :: forall ip xs. (Eq ip) =>
533 NodeId -> (Bucket ip -> f (xs, Bucket ip)) -> Table ip -> f (xs,Table ip) 533 NodeId -> (Bucket ip -> Maybe (xs, Bucket ip)) -> Table ip -> Maybe (xs,Table ip)
534modifyBucket nodeId f = go (0 :: BitIx) 534modifyBucket nodeId f = go (0 :: BitIx)
535 where 535 where
536 go :: BitIx -> Table ip -> f (xs, Table ip) 536 go :: BitIx -> Table ip -> Maybe (xs, Table ip)
537 go i (Zero table bucket) 537 go i (Zero table bucket)
538 | testIdBit nodeId i = second (Zero table) <$> f bucket 538 | testIdBit nodeId i = second (Zero table) <$> f bucket
539 | otherwise = second (`Zero` bucket) <$> go (succ i) table 539 | otherwise = second (`Zero` bucket) <$> go (succ i) table
@@ -562,8 +562,8 @@ data CheckPing ip = CheckPing [NodeInfo ip]
562 562
563 563
564-- | Atomic 'Table' update 564-- | Atomic 'Table' update
565insert :: (Alternative m, Eq ip) => Timestamp -> Event ip -> Table ip -> m ([CheckPing ip], Table ip) 565insert :: (Eq ip, Applicative m) => Timestamp -> Event ip -> Table ip -> m ([CheckPing ip], Table ip)
566insert tm event = modifyBucket (eventId event) (insertBucket tm event) 566insert tm event tbl = pure $ fromMaybe ([],tbl) $ modifyBucket (eventId event) (insertBucket tm event) tbl
567 567
568 568
569{----------------------------------------------------------------------- 569{-----------------------------------------------------------------------
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index c08021c7..bad783a5 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -72,7 +72,11 @@ module Network.BitTorrent.DHT.Session
72import Prelude hiding (ioError) 72import Prelude hiding (ioError)
73 73
74import Control.Concurrent.STM 74import Control.Concurrent.STM
75#ifdef THREAD_DEBUG
76import Control.Concurrent.Async.Lifted.Instrument
77#else
75import Control.Concurrent.Async.Lifted 78import Control.Concurrent.Async.Lifted
79#endif
76import Control.Exception.Lifted hiding (Handler) 80import Control.Exception.Lifted hiding (Handler)
77import Control.Monad.Base 81import Control.Monad.Base
78import Control.Monad.Logger 82import Control.Monad.Logger
@@ -395,8 +399,9 @@ routableAddress = do
395myNodeIdAccordingTo :: NodeAddr ip -> DHT ip NodeId 399myNodeIdAccordingTo :: NodeAddr ip -> DHT ip NodeId
396myNodeIdAccordingTo _ = do 400myNodeIdAccordingTo _ = do
397 info <- asks routingInfo >>= liftIO . atomically . readTVar 401 info <- asks routingInfo >>= liftIO . atomically . readTVar
398 fallback <- asks tentativeNodeId 402 maybe (asks tentativeNodeId)
399 return $ maybe fallback myNodeId info 403 (return . myNodeId)
404 info
400 405
401-- | Get current routing table. Normally you don't need to use this 406-- | Get current routing table. Normally you don't need to use this
402-- function, but it can be usefull for debugging and profiling purposes. 407-- function, but it can be usefull for debugging and profiling purposes.