diff options
author | Joe Crayne <joe@jerkface.net> | 2018-09-09 02:32:20 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-10-03 06:49:55 -0400 |
commit | fbf9890a6bcd4e6212b5947f908bc34f233b279d (patch) | |
tree | 1ceee1e2dcc2a1bb53c6ca03d0d4986099381630 /Connection/Tcp.hs | |
parent | 037508fe7ed09e3b4f4c00b7778f6c0dc4a3d5f9 (diff) |
Moved resolving duty to Connection manager.
Diffstat (limited to 'Connection/Tcp.hs')
-rw-r--r-- | Connection/Tcp.hs | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/Connection/Tcp.hs b/Connection/Tcp.hs index c6f3a8ce..35083198 100644 --- a/Connection/Tcp.hs +++ b/Connection/Tcp.hs | |||
@@ -52,6 +52,7 @@ import Control.Monad.Fix | |||
52 | -- import Control.Monad.STM | 52 | -- import Control.Monad.STM |
53 | -- import Control.Monad.Trans.Resource | 53 | -- import Control.Monad.Trans.Resource |
54 | import Control.Monad.IO.Class (MonadIO (liftIO)) | 54 | import Control.Monad.IO.Class (MonadIO (liftIO)) |
55 | import Data.Maybe | ||
55 | import System.IO.Error (isDoesNotExistError) | 56 | import System.IO.Error (isDoesNotExistError) |
56 | import System.IO | 57 | import System.IO |
57 | ( IOMode(..) | 58 | ( IOMode(..) |
@@ -71,12 +72,15 @@ import Data.Time.Clock (getCurrentTime,diffUTCTime) | |||
71 | -- import SockAddr () | 72 | -- import SockAddr () |
72 | -- import System.Locale (defaultTimeLocale) | 73 | -- import System.Locale (defaultTimeLocale) |
73 | 74 | ||
75 | import qualified Data.Text as Text | ||
76 | ;import Data.Text (Text) | ||
77 | import DNSCache | ||
74 | import InterruptibleDelay | 78 | import InterruptibleDelay |
75 | import PingMachine | 79 | import PingMachine |
76 | import Network.StreamServer | 80 | import Network.StreamServer |
77 | import Network.SocketLike hiding (sClose) | 81 | import Network.SocketLike hiding (sClose) |
78 | import qualified Connection as G | 82 | import qualified Connection as G |
79 | ;import Connection (Manager (..), Policy(..)) | 83 | ;import Connection (Manager (..), PeerAddress (..), Policy (..)) |
80 | import DPut | 84 | import DPut |
81 | 85 | ||
82 | 86 | ||
@@ -763,15 +767,22 @@ debugNoise str = return () | |||
763 | 767 | ||
764 | data TCPStatus = Resolving | AwaitingRead | AwaitingWrite | 768 | data TCPStatus = Resolving | AwaitingRead | AwaitingWrite |
765 | 769 | ||
766 | tcpManager :: ( Show k, Ord k, Ord conkey ) => | 770 | -- SockAddr -> (SockAddr, ConnectionParameters SockAddr ConnectionData, Miliseconds) |
767 | (conkey -> (SockAddr, ConnectionParameters conkey u, Miliseconds)) | 771 | |
768 | -> (String -> Maybe k) | 772 | |
769 | -> (k -> IO (Maybe conkey)) | 773 | tcpManager :: (PeerAddress -> (SockAddr, ConnectionParameters PeerAddress u, Miliseconds)) |
770 | -> Server conkey u releaseKey x | 774 | -- -> (String -> Maybe Text) |
771 | -> IO (Manager TCPStatus k) | 775 | -- -> (Text -> IO (Maybe PeerAddress)) |
772 | tcpManager grokKey s2k resolvKey sv = do | 776 | -> Server PeerAddress u releaseKey x |
777 | -> IO (Manager TCPStatus Text) | ||
778 | tcpManager grokKey sv = do | ||
773 | rmap <- atomically $ newTVar Map.empty -- Map k (Maybe conkey) | 779 | rmap <- atomically $ newTVar Map.empty -- Map k (Maybe conkey) |
774 | nullping <- forkPingMachine "tcpManager" 0 0 | 780 | nullping <- forkPingMachine "tcpManager" 0 0 |
781 | (rslv,rev) <- do | ||
782 | dns <- newDNSCache | ||
783 | let rslv k = map PeerAddress <$> forwardResolve dns k | ||
784 | rev (PeerAddress addr) = reverseResolve dns addr | ||
785 | return (rslv,rev) | ||
775 | return Manager { | 786 | return Manager { |
776 | setPolicy = \k -> \case | 787 | setPolicy = \k -> \case |
777 | TryingToConnect -> join $ atomically $ do | 788 | TryingToConnect -> join $ atomically $ do |
@@ -782,7 +793,7 @@ tcpManager grokKey s2k resolvKey sv = do | |||
782 | modifyTVar' rmap $ Map.insert k Nothing | 793 | modifyTVar' rmap $ Map.insert k Nothing |
783 | return $ void $ forkIO $ do | 794 | return $ void $ forkIO $ do |
784 | myThreadId >>= flip labelThread ("resolve."++show k) | 795 | myThreadId >>= flip labelThread ("resolve."++show k) |
785 | mconkey <- resolvKey k | 796 | mconkey <- listToMaybe <$> rslv k |
786 | case mconkey of | 797 | case mconkey of |
787 | Nothing -> atomically $ modifyTVar' rmap $ Map.delete k | 798 | Nothing -> atomically $ modifyTVar' rmap $ Map.delete k |
788 | Just conkey -> do | 799 | Just conkey -> do |
@@ -795,12 +806,14 @@ tcpManager grokKey s2k resolvKey sv = do | |||
795 | ck <- Map.lookup k <$> readTVar rmap | 806 | ck <- Map.lookup k <$> readTVar rmap |
796 | return $ exportConnection c (join ck) | 807 | return $ exportConnection c (join ck) |
797 | , connections = Map.keys <$> readTVar rmap | 808 | , connections = Map.keys <$> readTVar rmap |
798 | , stringToKey = s2k | 809 | , stringToKey = Just . Text.pack |
799 | , showProgress = \case | 810 | , showProgress = \case |
800 | Resolving -> "resolving" | 811 | Resolving -> "resolving" |
801 | AwaitingRead -> "awaiting inbound" | 812 | AwaitingRead -> "awaiting inbound" |
802 | AwaitingWrite -> "awaiting outbound" | 813 | AwaitingWrite -> "awaiting outbound" |
803 | , showKey = show | 814 | , showKey = show |
815 | , resolvePeer = rslv | ||
816 | , reverseAddress = rev | ||
804 | } | 817 | } |
805 | 818 | ||
806 | exportConnection :: Ord conkey => Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus | 819 | exportConnection :: Ord conkey => Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus |