summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-08 08:01:17 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-08 08:01:17 +0400
commita813326a2de915676f5d494b0cd9b4aa6247a3d2 (patch)
tree07296a2e7a46bce91369ce882d2b7e81c172d2df /src
parent64f26fba49e7ae933a0ccdcd7cdbb56c5c6a7df7 (diff)
Pass query_timeout options to rpc manager
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs25
1 files changed, 9 insertions, 16 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index d7c6a7f7..263b88ac 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -120,10 +120,8 @@ instance Default Options where
120 , optTimeout = 5 -- seconds 120 , optTimeout = 5 -- seconds
121 } 121 }
122 122
123microseconds :: NominalDiffTime -> Int 123seconds :: NominalDiffTime -> Int
124microseconds dt = fromEnum millis 124seconds dt = fromEnum (realToFrac dt :: Uni)
125 where
126 millis = realToFrac dt :: Micro
127 125
128{----------------------------------------------------------------------- 126{-----------------------------------------------------------------------
129-- Tokens policy 127-- Tokens policy
@@ -196,8 +194,9 @@ runDHT :: forall ip a. Address ip
196 -> IO a -- ^ result. 194 -> IO a -- ^ result.
197runDHT handlers opts naddr action = runResourceT $ do 195runDHT handlers opts naddr action = runResourceT $ do
198 runStderrLoggingT $ LoggingT $ \ logger -> do 196 runStderrLoggingT $ LoggingT $ \ logger -> do
199 let kopts = KRPC.def 197 let rpcOpts = KRPC.def { optQueryTimeout = seconds (optTimeout opts) }
200 (_, m) <- allocate (newManager kopts (toSockAddr naddr) handlers) closeManager 198 let nodeAddr = toSockAddr naddr
199 (_, m) <- allocate (newManager rpcOpts nodeAddr handlers) closeManager
201 myId <- liftIO genNodeId 200 myId <- liftIO genNodeId
202 node <- liftIO $ Node opts m 201 node <- liftIO $ Node opts m
203 <$> newMVar (nullTable myId (optBucketCount opts)) 202 <$> newMVar (nullTable myId (optBucketCount opts))
@@ -262,7 +261,7 @@ checkToken addr questionableToken = do
262 tryUpdateSecret 261 tryUpdateSecret
263 toks <- asks sessionTokens >>= liftIO . readTVarIO 262 toks <- asks sessionTokens >>= liftIO . readTVarIO
264 unless (member addr questionableToken (tokenMap toks)) $ 263 unless (member addr questionableToken (tokenMap toks)) $
265 throw $ InvalidParameter "token" 264 throwIO $ InvalidParameter "token"
266 265
267{----------------------------------------------------------------------- 266{-----------------------------------------------------------------------
268-- Routing table 267-- Routing table
@@ -334,15 +333,9 @@ queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b)
334 => NodeAddr ip -> a -> DHT ip b 333 => NodeAddr ip -> a -> DHT ip b
335queryNode addr q = do 334queryNode addr q = do
336 nid <- getNodeId 335 nid <- getNodeId
337 -- TODO remove timeout: KRPC already keep track timeouts 336 Response remoteId r <- query (toSockAddr addr) (Query nid q)
338 interval <- asks (optTimeout . options) 337 insertNode (NodeInfo remoteId addr)
339 result <- timeout (microseconds interval) $ do 338 return r
340 query (toSockAddr addr) (Query nid q)
341 case result of
342 Nothing -> ioError $ userError "timeout expired"
343 Just (Response remoteId r) -> do
344 insertNode (NodeInfo remoteId addr)
345 return r
346 339
347-- | Infix version of 'queryNode' function. 340-- | Infix version of 'queryNode' function.
348(<@>) :: Address ip => KRPC (Query a) (Response b) 341(<@>) :: Address ip => KRPC (Query a) (Response b)