diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 16 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Routing.hs | 10 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 9 |
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 | ||
54 | import Control.Concurrent.Lifted.Instrument hiding (yield) | ||
55 | #else | ||
56 | import GHC.Conc (labelThread) | ||
52 | import Control.Concurrent.Lifted hiding (yield) | 57 | import Control.Concurrent.Lifted hiding (yield) |
58 | #endif | ||
53 | import Control.Exception.Lifted hiding (Handler) | 59 | import Control.Exception.Lifted hiding (Handler) |
54 | import Control.Monad.Reader | 60 | import Control.Monad.Reader |
55 | import Control.Monad.Logger | 61 | import 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. |
228 | insertNode :: forall ip. Address ip => NodeInfo ip -> Maybe ReflectedIP -> DHT ip ThreadId | 234 | insertNode :: forall ip. Address ip => NodeInfo ip -> Maybe ReflectedIP -> DHT ip () |
229 | insertNode info witnessed_ip0 = fork $ do | 235 | insertNode 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. |
531 | modifyBucket | 531 | modifyBucket |
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) |
534 | modifyBucket nodeId f = go (0 :: BitIx) | 534 | modifyBucket 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 |
565 | insert :: (Alternative m, Eq ip) => Timestamp -> Event ip -> Table ip -> m ([CheckPing ip], Table ip) | 565 | insert :: (Eq ip, Applicative m) => Timestamp -> Event ip -> Table ip -> m ([CheckPing ip], Table ip) |
566 | insert tm event = modifyBucket (eventId event) (insertBucket tm event) | 566 | insert 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 | |||
72 | import Prelude hiding (ioError) | 72 | import Prelude hiding (ioError) |
73 | 73 | ||
74 | import Control.Concurrent.STM | 74 | import Control.Concurrent.STM |
75 | #ifdef THREAD_DEBUG | ||
76 | import Control.Concurrent.Async.Lifted.Instrument | ||
77 | #else | ||
75 | import Control.Concurrent.Async.Lifted | 78 | import Control.Concurrent.Async.Lifted |
79 | #endif | ||
76 | import Control.Exception.Lifted hiding (Handler) | 80 | import Control.Exception.Lifted hiding (Handler) |
77 | import Control.Monad.Base | 81 | import Control.Monad.Base |
78 | import Control.Monad.Logger | 82 | import Control.Monad.Logger |
@@ -395,8 +399,9 @@ routableAddress = do | |||
395 | myNodeIdAccordingTo :: NodeAddr ip -> DHT ip NodeId | 399 | myNodeIdAccordingTo :: NodeAddr ip -> DHT ip NodeId |
396 | myNodeIdAccordingTo _ = do | 400 | myNodeIdAccordingTo _ = 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. |