summaryrefslogtreecommitdiff
path: root/Connection
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-09-09 02:32:20 -0400
committerJoe Crayne <joe@jerkface.net>2018-10-03 06:49:55 -0400
commitfbf9890a6bcd4e6212b5947f908bc34f233b279d (patch)
tree1ceee1e2dcc2a1bb53c6ca03d0d4986099381630 /Connection
parent037508fe7ed09e3b4f4c00b7778f6c0dc4a3d5f9 (diff)
Moved resolving duty to Connection manager.
Diffstat (limited to 'Connection')
-rw-r--r--Connection/Tcp.hs33
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
54import Control.Monad.IO.Class (MonadIO (liftIO)) 54import Control.Monad.IO.Class (MonadIO (liftIO))
55import Data.Maybe
55import System.IO.Error (isDoesNotExistError) 56import System.IO.Error (isDoesNotExistError)
56import System.IO 57import 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
75import qualified Data.Text as Text
76 ;import Data.Text (Text)
77import DNSCache
74import InterruptibleDelay 78import InterruptibleDelay
75import PingMachine 79import PingMachine
76import Network.StreamServer 80import Network.StreamServer
77import Network.SocketLike hiding (sClose) 81import Network.SocketLike hiding (sClose)
78import qualified Connection as G 82import qualified Connection as G
79 ;import Connection (Manager (..), Policy(..)) 83 ;import Connection (Manager (..), PeerAddress (..), Policy (..))
80import DPut 84import DPut
81 85
82 86
@@ -763,15 +767,22 @@ debugNoise str = return ()
763 767
764data TCPStatus = Resolving | AwaitingRead | AwaitingWrite 768data TCPStatus = Resolving | AwaitingRead | AwaitingWrite
765 769
766tcpManager :: ( 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)) 773tcpManager :: (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))
772tcpManager grokKey s2k resolvKey sv = do 776 -> Server PeerAddress u releaseKey x
777 -> IO (Manager TCPStatus Text)
778tcpManager 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
806exportConnection :: Ord conkey => Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus 819exportConnection :: Ord conkey => Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus