diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/DHT.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 27 |
2 files changed, 23 insertions, 13 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs index 41a76886..5cf7468e 100644 --- a/src/Network/BitTorrent/DHT.hs +++ b/src/Network/BitTorrent/DHT.hs | |||
@@ -35,7 +35,6 @@ import Data.List as L | |||
35 | import Data.Monoid | 35 | import Data.Monoid |
36 | import Data.Text as T | 36 | import Data.Text as T |
37 | import Network.Socket (PortNumber) | 37 | import Network.Socket (PortNumber) |
38 | import System.Timeout.Lifted | ||
39 | import Text.PrettyPrint as PP hiding ((<>)) | 38 | import Text.PrettyPrint as PP hiding ((<>)) |
40 | import Text.PrettyPrint.Class | 39 | import Text.PrettyPrint.Class |
41 | 40 | ||
@@ -103,16 +102,12 @@ bootstrap startNodes = do | |||
103 | where | 102 | where |
104 | insertClosest addr = do | 103 | insertClosest addr = do |
105 | nid <- getNodeId | 104 | nid <- getNodeId |
106 | result <- try $ timeout 1000000 $ FindNode nid <@> addr | 105 | result <- try $ FindNode nid <@> addr |
107 | case result of | 106 | case result of |
108 | Left e -> do | 107 | Left e -> do |
109 | $(logWarnS) "bootstrap" $ T.pack $ show (e :: IOError) | 108 | $(logWarnS) "bootstrap" $ T.pack $ show (e :: IOError) |
110 | 109 | ||
111 | Right Nothing -> do | 110 | Right (NodeFound closest) -> do |
112 | $(logWarnS) "bootstrap" $ "not responding @ " | ||
113 | <> T.pack (show (pretty addr)) | ||
114 | |||
115 | Right (Just (NodeFound closest)) -> do | ||
116 | $(logDebug) ("Get a list of closest nodes: " <> | 111 | $(logDebug) ("Get a list of closest nodes: " <> |
117 | T.pack (PP.render (pretty closest))) | 112 | T.pack (PP.render (pretty closest))) |
118 | forM_ (L.take 2 closest) $ \ info @ NodeInfo {..} -> do | 113 | forM_ (L.take 2 closest) $ \ info @ NodeInfo {..} -> do |
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs index ac285bdc..a76880d7 100644 --- a/src/Network/BitTorrent/DHT/Session.hs +++ b/src/Network/BitTorrent/DHT/Session.hs | |||
@@ -48,6 +48,7 @@ import Control.Monad.Reader | |||
48 | import Control.Monad.Trans.Control | 48 | import Control.Monad.Trans.Control |
49 | import Control.Monad.Trans.Resource | 49 | import Control.Monad.Trans.Resource |
50 | import Data.Default | 50 | import Data.Default |
51 | import Data.Fixed | ||
51 | import Data.Hashable | 52 | import Data.Hashable |
52 | import Data.List as L | 53 | import Data.List as L |
53 | import Data.Monoid | 54 | import Data.Monoid |
@@ -57,6 +58,7 @@ import Data.Time | |||
57 | import Data.Time.Clock.POSIX | 58 | import Data.Time.Clock.POSIX |
58 | import System.Log.FastLogger | 59 | import System.Log.FastLogger |
59 | import System.Random (randomIO) | 60 | import System.Random (randomIO) |
61 | import System.Timeout.Lifted | ||
60 | import Text.PrettyPrint as PP hiding ((<>)) | 62 | import Text.PrettyPrint as PP hiding ((<>)) |
61 | import Text.PrettyPrint.Class | 63 | import Text.PrettyPrint.Class |
62 | 64 | ||
@@ -101,6 +103,11 @@ instance Default Options where | |||
101 | , optTimeout = 5 -- seconds | 103 | , optTimeout = 5 -- seconds |
102 | } | 104 | } |
103 | 105 | ||
106 | milliseconds :: NominalDiffTime -> Int | ||
107 | milliseconds dt = fromEnum millis | ||
108 | where | ||
109 | millis = realToFrac dt :: Milli | ||
110 | |||
104 | {----------------------------------------------------------------------- | 111 | {----------------------------------------------------------------------- |
105 | -- Tokens policy | 112 | -- Tokens policy |
106 | -----------------------------------------------------------------------} | 113 | -----------------------------------------------------------------------} |
@@ -185,11 +192,9 @@ runDHT handlers opts naddr action = runResourceT $ do | |||
185 | -- Routing | 192 | -- Routing |
186 | -----------------------------------------------------------------------} | 193 | -----------------------------------------------------------------------} |
187 | 194 | ||
188 | -- TODO fork? | ||
189 | routing :: Address ip => Routing ip a -> DHT ip (Maybe a) | 195 | routing :: Address ip => Routing ip a -> DHT ip (Maybe a) |
190 | routing = runRouting ping refreshNodes getTimestamp | 196 | routing = runRouting ping refreshNodes getTimestamp |
191 | 197 | ||
192 | -- TODO add timeout | ||
193 | ping :: Address ip => NodeAddr ip -> DHT ip Bool | 198 | ping :: Address ip => NodeAddr ip -> DHT ip Bool |
194 | ping addr = do | 199 | ping addr = do |
195 | $(logDebugS) "routing.questionable_node" (T.pack (render (pretty addr))) | 200 | $(logDebugS) "routing.questionable_node" (T.pack (render (pretty addr))) |
@@ -250,6 +255,7 @@ getTable = do | |||
250 | var <- asks routingTable | 255 | var <- asks routingTable |
251 | liftIO (readMVar var) | 256 | liftIO (readMVar var) |
252 | 257 | ||
258 | -- FIXME no blocking | ||
253 | getNodeId :: DHT ip NodeId | 259 | getNodeId :: DHT ip NodeId |
254 | getNodeId = thisId <$> getTable | 260 | getNodeId = thisId <$> getTable |
255 | 261 | ||
@@ -305,6 +311,7 @@ getPeerList ih = do | |||
305 | -- Messaging | 311 | -- Messaging |
306 | -----------------------------------------------------------------------} | 312 | -----------------------------------------------------------------------} |
307 | 313 | ||
314 | -- | Throws exception if node is not responding. | ||
308 | (<@>) :: forall a b ip. Address ip => KRPC (Query a) (Response b) | 315 | (<@>) :: forall a b ip. Address ip => KRPC (Query a) (Response b) |
309 | => a -> NodeAddr ip -> DHT ip b | 316 | => a -> NodeAddr ip -> DHT ip b |
310 | q <@> addr = do | 317 | q <@> addr = do |
@@ -313,11 +320,19 @@ q <@> addr = do | |||
313 | let Method name = method :: Method (Query a) (Response b) | 320 | let Method name = method :: Method (Query a) (Response b) |
314 | let signature = T.decodeUtf8 name <> " @ " <> T.pack (render (pretty addr)) | 321 | let signature = T.decodeUtf8 name <> " @ " <> T.pack (render (pretty addr)) |
315 | $(logDebugS) "queryNode" $ "Query sent | " <> signature | 322 | $(logDebugS) "queryNode" $ "Query sent | " <> signature |
316 | Response remoteId r <- query (toSockAddr addr) (Query nid q) | ||
317 | $(logDebugS) "queryNode" $ "Query recv | " <> signature | ||
318 | 323 | ||
319 | insertNode (NodeInfo remoteId addr) | 324 | interval <- asks (milliseconds . optTimeout . options) |
320 | return r | 325 | result <- timeout interval $ query (toSockAddr addr) (Query nid q) |
326 | case result of | ||
327 | Nothing -> do | ||
328 | $(logWarnS) "queryNode" $ "not responding @ " | ||
329 | <> T.pack (show (pretty addr)) | ||
330 | throwIO $ KError GenericError "timeout expired" "" | ||
331 | |||
332 | Just (Response remoteId r) -> do | ||
333 | $(logDebugS) "queryNode" $ "Query recv | " <> signature | ||
334 | insertNode (NodeInfo remoteId addr) | ||
335 | return r | ||
321 | 336 | ||
322 | type NodeHandler ip = Handler (DHT ip) | 337 | type NodeHandler ip = Handler (DHT ip) |
323 | 338 | ||