summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Session.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-28 18:30:44 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-28 18:30:44 +0400
commit4a46766d5fb0882151e80f9137983a8c2dfb7869 (patch)
tree62d49c7c00858377275f321a590e2c36b7577062 /src/Network/BitTorrent/DHT/Session.hs
parent0e3ed097d12e5fb82b594265a1adb6096fe556b4 (diff)
Add instance Alternative Routing
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs27
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
34import Control.Applicative 34import Control.Applicative
35import Control.Concurrent.STM 35import Control.Concurrent.STM
36import Control.Exception hiding (Handler) 36import Control.Exception.Lifted hiding (Handler)
37import Control.Monad.Base 37import Control.Monad.Base
38import Control.Monad.Logger 38import Control.Monad.Logger
39import Control.Monad.Reader 39import Control.Monad.Reader
@@ -145,15 +145,16 @@ runDHT naddr handlers action = runResourceT $ do
145-----------------------------------------------------------------------} 145-----------------------------------------------------------------------}
146 146
147-- TODO fork? 147-- TODO fork?
148routing :: Address ip => Routing ip a -> DHT ip a 148routing :: Address ip => Routing ip a -> DHT ip (Maybe a)
149routing = runRouting ping refreshNodes getTimestamp 149routing = runRouting ping refreshNodes getTimestamp
150 150
151-- TODO add timeout 151-- TODO add timeout
152ping :: Address ip => NodeAddr ip -> DHT ip Bool 152ping :: Address ip => NodeAddr ip -> DHT ip Bool
153ping addr = do 153ping 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
159refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip] 160refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip]
@@ -167,9 +168,9 @@ refreshNodes nid = do
167 168
168getTimestamp :: DHT ip Timestamp 169getTimestamp :: DHT ip Timestamp
169getTimestamp = do 170getTimestamp = 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
222insertNode :: Address ip => NodeInfo ip -> DHT ip () 223insertNode :: Address ip => NodeInfo ip -> DHT ip ()
223insertNode info = do 224insertNode 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