summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-22 01:37:04 -0400
committerjoe <joe@jerkface.net>2017-06-22 01:37:04 -0400
commite0ada56c116255a2feddcf7b09cec808f79afefc (patch)
treecf68a3015821039f707591684a237f8d7cf1c7cb /src/Network
parent012d138b1061d967ef3a05dfb7dc819d199b3902 (diff)
Propagated MonadKRPC deletion to Network.BitTorrent.DHT.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/DHT.hs11
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs9
-rw-r--r--src/Network/DatagramServer.hs24
3 files changed, 25 insertions, 19 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index 51d92127..d9328cea 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -70,8 +70,11 @@ import Network.BitTorrent.DHT.Query
70import Network.BitTorrent.DHT.Session 70import Network.BitTorrent.DHT.Session
71import Network.DHT.Routing as T hiding (null) 71import Network.DHT.Routing as T hiding (null)
72import qualified Data.Text as Text 72import qualified Data.Text as Text
73import Data.Typeable
73import Data.Monoid 74import Data.Monoid
74import Network.DatagramServer.Mainline (KMessageOf) 75import Network.DatagramServer.Mainline (KMessageOf)
76import qualified Network.DatagramServer as KRPC (listen, Protocol(..))
77
75 78
76 79
77{----------------------------------------------------------------------- 80{-----------------------------------------------------------------------
@@ -97,8 +100,12 @@ dht :: (Ord ip, Address ip)
97 -> IO a -- ^ result. 100 -> IO a -- ^ result.
98dht opts addr logfilter action = do 101dht opts addr logfilter action = do
99 runStderrLoggingT $ filterLogger logfilter $ LoggingT $ \ logger -> do 102 runStderrLoggingT $ filterLogger logfilter $ LoggingT $ \ logger -> do
100 bracket (newNode defaultHandlers opts addr logger Nothing) closeNode $ 103 bracket (newNode opts addr logger Nothing) closeNode $
101 \ node -> runDHT node action 104 \ node -> runDHT node $ do
105 hs <- defaultHandlers logger
106 m <- asks manager
107 liftIO $ KRPC.listen m hs (KRPC.Protocol Proxy Proxy)
108 action
102{-# INLINE dht #-} 109{-# INLINE dht #-}
103 110
104{----------------------------------------------------------------------- 111{-----------------------------------------------------------------------
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index d8665773..d4794038 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -377,8 +377,8 @@ locFromCS cs = case getCallStack cs of
377-- 'closeNode' function, otherwise socket or other scarce resources may 377-- 'closeNode' function, otherwise socket or other scarce resources may
378-- leak. 378-- leak.
379newNode :: Address ip 379newNode :: Address ip
380 => [NodeHandler] -- ^ handlers to run on accepted queries; 380 => -- [NodeHandler] -- ^ handlers to run on accepted queries;
381 -> Options -- ^ various dht options; 381 Options -- ^ various dht options;
382 -> NodeAddr ip -- ^ node address to bind; 382 -> NodeAddr ip -- ^ node address to bind;
383 -> LogFun -- ^ invoked on log messages; 383 -> LogFun -- ^ invoked on log messages;
384#ifdef VERSION_bencoding 384#ifdef VERSION_bencoding
@@ -387,7 +387,7 @@ newNode :: Address ip
387 -> Maybe (NodeId Tox.Message) -- ^ use this NodeId, if not given a new one is generated. 387 -> Maybe (NodeId Tox.Message) -- ^ use this NodeId, if not given a new one is generated.
388#endif 388#endif
389 -> IO (Node ip) -- ^ a new DHT node running at given address. 389 -> IO (Node ip) -- ^ a new DHT node running at given address.
390newNode hs opts naddr logger mbid = do 390newNode opts naddr logger mbid = do
391 s <- createInternalState 391 s <- createInternalState
392 runInternalState initNode s 392 runInternalState initNode s
393 `onException` closeInternalState s 393 `onException` closeInternalState s
@@ -396,7 +396,7 @@ newNode hs opts naddr logger mbid = do
396 nodeAddr = toSockAddr naddr 396 nodeAddr = toSockAddr naddr
397 initNode = do 397 initNode = do
398 s <- getInternalState 398 s <- getInternalState
399 (_, m) <- allocate (newManager rpcOpts (logt logger) nodeAddr hs) closeManager 399 (_, m) <- allocate (newManager rpcOpts (logt logger) nodeAddr []) closeManager
400 liftIO $ do 400 liftIO $ do
401 myId <- maybe genNodeId return mbid 401 myId <- maybe genNodeId return mbid
402 node <- Node opts myId s m 402 node <- Node opts myId s m
@@ -405,7 +405,6 @@ newNode hs opts naddr logger mbid = do
405 <*> newTVarIO S.empty 405 <*> newTVarIO S.empty
406 <*> (newTVarIO =<< nullSessionTokens) 406 <*> (newTVarIO =<< nullSessionTokens)
407 <*> pure logger 407 <*> pure logger
408 KRPC.listen m (KRPC.Protocol Proxy Proxy)
409 return node 408 return node
410 409
411-- | Some resources like listener thread may live for 410-- | Some resources like listener thread may live for
diff --git a/src/Network/DatagramServer.hs b/src/Network/DatagramServer.hs
index f212ffdf..2140e2cd 100644
--- a/src/Network/DatagramServer.hs
+++ b/src/Network/DatagramServer.hs
@@ -196,7 +196,7 @@ data Manager h raw msg = Manager
196 , listenerThread :: !(MVar ThreadId) 196 , listenerThread :: !(MVar ThreadId)
197 , transactionCounter :: {-# UNPACK #-} !TransactionCounter 197 , transactionCounter :: {-# UNPACK #-} !TransactionCounter
198 , pendingCalls :: {-# UNPACK #-} !(PendingCalls msg raw) 198 , pendingCalls :: {-# UNPACK #-} !(PendingCalls msg raw)
199 , handlers :: [Handler h msg raw] 199 , handlers :: [Handler h msg raw] -- TODO delete this, it's not used
200 , logMsg :: Char -> String -> T.Text -> IO () 200 , logMsg :: Char -> String -> T.Text -> IO ()
201 } 201 }
202 202
@@ -462,8 +462,8 @@ dispatchHandler :: ( Eq (QueryMethod msg)
462 , Show (QueryMethod msg) 462 , Show (QueryMethod msg)
463 , Serialize (TransactionID msg) 463 , Serialize (TransactionID msg)
464 , Envelope msg 464 , Envelope msg
465 ) => Manager IO raw msg -> QueryMethod msg -> msg raw -> SockAddr -> IO (KResult msg raw) 465 ) => Manager IO raw msg -> [Handler IO msg raw] -> QueryMethod msg -> msg raw -> SockAddr -> IO (KResult msg raw)
466dispatchHandler mgr@Manager{..} meth q addr = do 466dispatchHandler mgr handlers meth q addr = do
467 case L.lookup meth handlers of 467 case L.lookup meth handlers of
468 Nothing -> return $ Left $ KError MethodUnknown ("Unknown method " <> BC.pack (show meth)) (envelopeTransaction q) 468 Nothing -> return $ Left $ KError MethodUnknown ("Unknown method " <> BC.pack (show meth)) (envelopeTransaction q)
469 Just h -> runHandler mgr meth h addr q 469 Just h -> runHandler mgr meth h addr q
@@ -483,10 +483,10 @@ handleQuery :: ( WireFormat raw msg
483 , Eq (QueryMethod msg) 483 , Eq (QueryMethod msg)
484 , Show (QueryMethod msg) 484 , Show (QueryMethod msg)
485 , Serialize (TransactionID msg) 485 , Serialize (TransactionID msg)
486 ) => Manager IO raw msg -> QueryMethod msg -> raw -> msg raw -> SockAddr -> IO () 486 ) => Manager IO raw msg -> [Handler IO msg raw] -> QueryMethod msg -> raw -> msg raw -> SockAddr -> IO ()
487handleQuery mgr@Manager{..} meth raw q addr = void $ fork $ do 487handleQuery mgr@Manager{..} hs meth raw q addr = void $ fork $ do
488 myThreadId >>= liftIO . flip labelThread "KRPC.handleQuery" 488 myThreadId >>= liftIO . flip labelThread "KRPC.handleQuery"
489 res <- dispatchHandler mgr meth q addr 489 res <- dispatchHandler mgr hs meth q addr
490 let res' = either buildError Just res 490 let res' = either buildError Just res
491 ctx = error "TODO TOX ToxCipherContext 2 or () for Mainline" 491 ctx = error "TODO TOX ToxCipherContext 2 or () for Mainline"
492 resbs = fmap (encodeHeaders ctx) res' :: Maybe BS.ByteString 492 resbs = fmap (encodeHeaders ctx) res' :: Maybe BS.ByteString
@@ -520,8 +520,8 @@ listener :: forall raw msg.
520 , Eq (QueryMethod msg) 520 , Eq (QueryMethod msg)
521 , Show (QueryMethod msg) 521 , Show (QueryMethod msg)
522 , Serialize (TransactionID msg) 522 , Serialize (TransactionID msg)
523 ) => Manager IO raw msg -> Protocol raw msg -> IO () 523 ) => Manager IO raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO ()
524listener mgr@Manager{..} p = do 524listener mgr@Manager{..} hs p = do
525 fix $ \again -> do 525 fix $ \again -> do
526 let ctx = error "TODO TOX ToxCipherContext or () for Mainline" 526 let ctx = error "TODO TOX ToxCipherContext or () for Mainline"
527 (bs, addr) <- liftIO $ do 527 (bs, addr) <- liftIO $ do
@@ -532,7 +532,7 @@ listener mgr@Manager{..} p = do
532 return () -- Without transaction id, error message isn't very useful. 532 return () -- Without transaction id, error message isn't very useful.
533 Right (raw,m) -> 533 Right (raw,m) ->
534 case envelopeClass m of 534 case envelopeClass m of
535 Query meth -> handleQuery mgr meth (raw::raw) m addr 535 Query meth -> handleQuery mgr hs meth (raw::raw) m addr
536 Response _ -> handleResponse mgr raw (Right m) addr 536 Response _ -> handleResponse mgr raw (Right m) addr
537 Error e -> handleResponse mgr raw (Left e) addr 537 Error e -> handleResponse mgr raw (Left e) addr
538 538
@@ -551,10 +551,10 @@ listen :: ( WireFormat raw msg
551 , Eq (QueryMethod msg) 551 , Eq (QueryMethod msg)
552 , Show (QueryMethod msg) 552 , Show (QueryMethod msg)
553 , Serialize (TransactionID msg) 553 , Serialize (TransactionID msg)
554 ) => Manager IO raw msg -> Protocol raw msg -> IO () 554 ) => Manager IO raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO ()
555listen mgr@Manager{..} p = do 555listen mgr@Manager{..} hs p = do
556 tid <- fork $ do 556 tid <- fork $ do
557 myThreadId >>= liftIO . flip labelThread "KRPC.listen" 557 myThreadId >>= liftIO . flip labelThread "KRPC.listen"
558 listener mgr p `Lifted.finally` 558 listener mgr hs p `Lifted.finally`
559 liftIO (takeMVar listenerThread) 559 liftIO (takeMVar listenerThread)
560 liftIO $ putMVar listenerThread tid 560 liftIO $ putMVar listenerThread tid