summaryrefslogtreecommitdiff
path: root/dht/src/Network/QueryResponse.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network/QueryResponse.hs')
-rw-r--r--dht/src/Network/QueryResponse.hs33
1 files changed, 18 insertions, 15 deletions
diff --git a/dht/src/Network/QueryResponse.hs b/dht/src/Network/QueryResponse.hs
index 9c33b911..5fcd1989 100644
--- a/dht/src/Network/QueryResponse.hs
+++ b/dht/src/Network/QueryResponse.hs
@@ -217,11 +217,11 @@ asyncQuery_ (Client net d err pending whoami _) meth q addr0 withResponse = do
217 now <- getPOSIXTime 217 now <- getPOSIXTime
218 (tid,addr,expiry) <- atomically $ do 218 (tid,addr,expiry) <- atomically $ do
219 tbl <- readTVar pending 219 tbl <- readTVar pending
220 ((tid,addr,expiry), tbl') <- dispatchRegister (tableMethods d) 220 (addr,expiry) <- methodTimeout meth addr0
221 (methodTimeout meth) 221 (tid, tbl') <- dispatchRegister (tableMethods d)
222 now 222 (now + microsecondsDiff expiry)
223 (withResponse . fmap (unwrapResponse meth)) 223 (withResponse . fmap (unwrapResponse meth))
224 addr0 224 addr -- XXX: Should be addr0 or addr?
225 tbl 225 tbl
226 -- (addr,expiry) <- methodTimeout meth tid addr0 226 -- (addr,expiry) <- methodTimeout meth tid addr0
227 writeTVar pending tbl' 227 writeTVar pending tbl'
@@ -365,7 +365,7 @@ data MethodSerializer tid addr x meth a b = MethodSerializer
365 { -- | Returns the microseconds to wait for a response to this query being 365 { -- | Returns the microseconds to wait for a response to this query being
366 -- sent to the given address. The /addr/ may also be modified to add 366 -- sent to the given address. The /addr/ may also be modified to add
367 -- routing information. 367 -- routing information.
368 methodTimeout :: tid -> addr -> STM (addr,Int) 368 methodTimeout :: addr -> STM (addr,Int)
369 -- | A method identifier used for error reporting. This needn't be the 369 -- | A method identifier used for error reporting. This needn't be the
370 -- same as the /meth/ argument to 'MethodHandler', but it is suggested. 370 -- same as the /meth/ argument to 'MethodHandler', but it is suggested.
371 , method :: meth 371 , method :: meth
@@ -385,21 +385,25 @@ data MethodSerializer tid addr x meth a b = MethodSerializer
385-- 385--
386-- The type variable /d/ is used to represent the current state of the 386-- The type variable /d/ is used to represent the current state of the
387-- transaction generator and the table of pending transactions. 387-- transaction generator and the table of pending transactions.
388data TransactionMethods d tid addr x = TransactionMethods 388data TransactionMethods d qid addr x = TransactionMethods
389 { 389 {
390 -- | Before a query is sent, this function stores an 'MVar' to which the 390 -- | Before a query is sent, this function stores an 'MVar' to which the
391 -- response will be written too. The returned /tid/ is a transaction id 391 -- response will be written too. The returned /qid/ is a transaction id
392 -- that can be used to forget the 'MVar' if the remote peer is not 392 -- that can be used to forget the 'MVar' if the remote peer is not
393 -- responding. 393 -- responding.
394 dispatchRegister :: (tid -> addr -> STM (addr,Int)) -> POSIXTime -> (Maybe x -> IO ()) -> addr -> d -> STM ((tid,addr,Int), d) 394 dispatchRegister :: POSIXTime -- time of expiry
395 -> (Maybe x -> IO ()) -- callback upon response (or timeout)
396 -> addr
397 -> d
398 -> STM (qid, d)
395 -- | This method is invoked when an incoming packet /x/ indicates it is 399 -- | This method is invoked when an incoming packet /x/ indicates it is
396 -- a response to the transaction with id /tid/. The returned IO action 400 -- a response to the transaction with id /qid/. The returned IO action
397 -- will write the packet to the correct 'MVar' thus completing the 401 -- will write the packet to the correct 'MVar' thus completing the
398 -- dispatch. 402 -- dispatch.
399 , dispatchResponse :: tid -> x -> d -> STM (d, IO ()) 403 , dispatchResponse :: qid -> x -> d -> STM (d, IO ())
400 -- | When a timeout interval elapses, this method is called to remove the 404 -- | When a timeout interval elapses, this method is called to remove the
401 -- transaction from the table. 405 -- transaction from the table.
402 , dispatchCancel :: tid -> d -> STM d 406 , dispatchCancel :: qid -> d -> STM d
403 } 407 }
404 408
405-- | Construct 'TransactionMethods' methods out of 3 lookup table primitives and a 409-- | Construct 'TransactionMethods' methods out of 3 lookup table primitives and a
@@ -424,11 +428,10 @@ transactionMethods' ::
424 -> TransactionMethods (g,t a) tid addr x 428 -> TransactionMethods (g,t a) tid addr x
425transactionMethods' store load (TableMethods insert delete lookup) generate = TransactionMethods 429transactionMethods' store load (TableMethods insert delete lookup) generate = TransactionMethods
426 { dispatchCancel = \tid (g,t) -> return (g, delete tid t) 430 { dispatchCancel = \tid (g,t) -> return (g, delete tid t)
427 , dispatchRegister = \getTimeout now v a0 (g,t) -> do 431 , dispatchRegister = \nowPlusExpiry v a (g,t) -> do
428 let (tid,g') = generate g 432 let (tid,g') = generate g
429 (a,expiry) <- getTimeout tid a0 433 let t' = insert tid (store v) nowPlusExpiry t -- (now + microsecondsDiff expiry) t
430 let t' = insert tid (store v) (now + microsecondsDiff expiry) t 434 return ( tid, (g',t') )
431 return ( (tid,a,expiry), (g',t') )
432 , dispatchResponse = \tid x (g,t) -> 435 , dispatchResponse = \tid x (g,t) ->
433 case lookup tid t of 436 case lookup tid t of
434 Just v -> let t' = delete tid t 437 Just v -> let t' = delete tid t