summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs37
1 files changed, 19 insertions, 18 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 9db5947a..4ac1bee9 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -33,6 +33,7 @@ module Network.BitTorrent.DHT.Session
33 33
34import Control.Applicative 34import Control.Applicative
35import Control.Concurrent.STM 35import Control.Concurrent.STM
36import Control.Concurrent.Lifted
36import Control.Exception.Lifted hiding (Handler) 37import Control.Exception.Lifted hiding (Handler)
37import Control.Monad.Base 38import Control.Monad.Base
38import Control.Monad.Logger 39import Control.Monad.Logger
@@ -93,7 +94,7 @@ invalidateTokens curTime ts @ SessionTokens {..}
93 94
94data Node ip = Node 95data Node ip = Node
95 { manager :: !(Manager (DHT ip)) 96 { manager :: !(Manager (DHT ip))
96 , routingTable :: !(TVar (Table ip)) 97 , routingTable :: !(MVar (Table ip))
97 , contactInfo :: !(TVar (PeerStore ip)) 98 , contactInfo :: !(TVar (PeerStore ip))
98 , sessionTokens :: !(TVar SessionTokens) 99 , sessionTokens :: !(TVar SessionTokens)
99 , loggerFun :: !(Loc -> LogSource -> LogLevel -> LogStr -> IO ()) 100 , loggerFun :: !(Loc -> LogSource -> LogLevel -> LogStr -> IO ())
@@ -134,7 +135,7 @@ runDHT naddr handlers action = runResourceT $ do
134 (_, m) <- allocate (newManager (toSockAddr naddr) handlers) closeManager 135 (_, m) <- allocate (newManager (toSockAddr naddr) handlers) closeManager
135 myId <- liftIO genNodeId 136 myId <- liftIO genNodeId
136 node <- liftIO $ Node m 137 node <- liftIO $ Node m
137 <$> newTVarIO (nullTable myId) 138 <$> newMVar (nullTable myId)
138 <*> newTVarIO def 139 <*> newTVarIO def
139 <*> (newTVarIO =<< nullSessionTokens) 140 <*> (newTVarIO =<< nullSessionTokens)
140 <*> pure logger 141 <*> pure logger
@@ -204,12 +205,7 @@ checkToken addr questionableToken = do
204getTable :: DHT ip (Table ip) 205getTable :: DHT ip (Table ip)
205getTable = do 206getTable = do
206 var <- asks routingTable 207 var <- asks routingTable
207 liftIO (readTVarIO var) 208 liftIO (readMVar var)
208
209putTable :: Table ip -> DHT ip ()
210putTable table = do
211 var <- asks routingTable
212 liftIO (atomically (writeTVar var table))
213 209
214getNodeId :: DHT ip NodeId 210getNodeId :: DHT ip NodeId
215getNodeId = thisId <$> getTable 211getNodeId = thisId <$> getTable
@@ -220,16 +216,21 @@ getClosest nid = kclosest 8 nid <$> getTable
220getClosestHash :: Eq ip => InfoHash -> DHT ip [NodeInfo ip] 216getClosestHash :: Eq ip => InfoHash -> DHT ip [NodeInfo ip]
221getClosestHash ih = kclosestHash 8 ih <$> getTable 217getClosestHash ih = kclosestHash 8 ih <$> getTable
222 218
223insertNode :: Address ip => NodeInfo ip -> DHT ip () 219-- FIXME some nodes can be ommited
224insertNode info = do 220insertNode :: Address ip => NodeInfo ip -> DHT ip ThreadId
225 t <- getTable 221insertNode info = fork $ do
226 mt <- routing (R.insert info t) 222 var <- asks routingTable
227 case mt of 223 modifyMVar_ var $ \ t -> do
228 Nothing -> $(logDebugS) "insertNode" "Routing table is full" 224 result <- routing (R.insert info t)
229 Just t' -> do 225 case result of
230 putTable t' 226 Nothing -> do
231 let logMsg = "Routing table updated: " <> pretty t <> " -> " <> pretty t' 227 $(logDebugS) "insertNode" $ "Routing table is full: "
232 $(logDebugS) "insertNode" (T.pack (render logMsg)) 228 <> T.pack (show (pretty t))
229 return t
230 Just t' -> do
231 let logMsg = "Routing table updated: " <> pretty t <> " -> " <> pretty t'
232 $(logDebugS) "insertNode" (T.pack (render logMsg))
233 return t'
233 234
234{----------------------------------------------------------------------- 235{-----------------------------------------------------------------------
235-- Peer storage 236-- Peer storage