diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs index 9243ef49..9db5947a 100644 --- a/src/Network/BitTorrent/DHT/Session.hs +++ b/src/Network/BitTorrent/DHT/Session.hs | |||
@@ -33,7 +33,7 @@ module Network.BitTorrent.DHT.Session | |||
33 | 33 | ||
34 | import Control.Applicative | 34 | import Control.Applicative |
35 | import Control.Concurrent.STM | 35 | import Control.Concurrent.STM |
36 | import Control.Exception hiding (Handler) | 36 | import Control.Exception.Lifted hiding (Handler) |
37 | import Control.Monad.Base | 37 | import Control.Monad.Base |
38 | import Control.Monad.Logger | 38 | import Control.Monad.Logger |
39 | import Control.Monad.Reader | 39 | import Control.Monad.Reader |
@@ -145,15 +145,16 @@ runDHT naddr handlers action = runResourceT $ do | |||
145 | -----------------------------------------------------------------------} | 145 | -----------------------------------------------------------------------} |
146 | 146 | ||
147 | -- TODO fork? | 147 | -- TODO fork? |
148 | routing :: Address ip => Routing ip a -> DHT ip a | 148 | routing :: Address ip => Routing ip a -> DHT ip (Maybe a) |
149 | routing = runRouting ping refreshNodes getTimestamp | 149 | routing = runRouting ping refreshNodes getTimestamp |
150 | 150 | ||
151 | -- TODO add timeout | 151 | -- TODO add timeout |
152 | ping :: Address ip => NodeAddr ip -> DHT ip Bool | 152 | ping :: Address ip => NodeAddr ip -> DHT ip Bool |
153 | ping addr = do | 153 | ping addr = do |
154 | $(logDebugS) "routing.questionable_node" (T.pack (render (pretty addr))) | 154 | $(logDebugS) "routing.questionable_node" (T.pack (render (pretty addr))) |
155 | Ping <- Ping <@> addr | 155 | result <- try $ Ping <@> addr |
156 | return True | 156 | let _ = result :: Either SomeException Ping |
157 | return $ either (const False) (const True) result | ||
157 | 158 | ||
158 | -- FIXME do not use getClosest sinse we should /refresh/ them | 159 | -- FIXME do not use getClosest sinse we should /refresh/ them |
159 | refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip] | 160 | refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip] |
@@ -167,9 +168,9 @@ refreshNodes nid = do | |||
167 | 168 | ||
168 | getTimestamp :: DHT ip Timestamp | 169 | getTimestamp :: DHT ip Timestamp |
169 | getTimestamp = do | 170 | getTimestamp = do |
170 | timestamp <- liftIO $ getCurrentTime | 171 | utcTime <- liftIO $ getCurrentTime |
171 | $(logDebugS) "routing.make_timestamp" (T.pack (render (pretty timestamp))) | 172 | $(logDebugS) "routing.make_timestamp" (T.pack (render (pretty utcTime))) |
172 | return $ utcTimeToPOSIXSeconds timestamp | 173 | return $ utcTimeToPOSIXSeconds utcTime |
173 | 174 | ||
174 | {----------------------------------------------------------------------- | 175 | {----------------------------------------------------------------------- |
175 | -- Tokens | 176 | -- Tokens |
@@ -222,11 +223,13 @@ getClosestHash ih = kclosestHash 8 ih <$> getTable | |||
222 | insertNode :: Address ip => NodeInfo ip -> DHT ip () | 223 | insertNode :: Address ip => NodeInfo ip -> DHT ip () |
223 | insertNode info = do | 224 | insertNode info = do |
224 | t <- getTable | 225 | t <- getTable |
225 | t' <- routing (R.insert info t) | 226 | mt <- routing (R.insert info t) |
226 | putTable t' | 227 | case mt of |
227 | 228 | Nothing -> $(logDebugS) "insertNode" "Routing table is full" | |
228 | let logMsg = "Routing table updated: " <> pretty t <> " -> " <> pretty t' | 229 | Just t' -> do |
229 | $(logDebugS) "insertNode" (T.pack (render logMsg)) | 230 | putTable t' |
231 | let logMsg = "Routing table updated: " <> pretty t <> " -> " <> pretty t' | ||
232 | $(logDebugS) "insertNode" (T.pack (render logMsg)) | ||
230 | 233 | ||
231 | {----------------------------------------------------------------------- | 234 | {----------------------------------------------------------------------- |
232 | -- Peer storage | 235 | -- Peer storage |