diff options
author | Joe Crayne <joe@jerkface.net> | 2018-08-22 22:57:14 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-09-07 13:18:56 -0400 |
commit | b94122bfb91a37bb141fdff05573cc02fd93c942 (patch) | |
tree | b92fa188b3d7bc333d3d3bb532d33e2c4861d642 | |
parent | 4aeaf247a25fbe80598ce54e4142a707ec5b9951 (diff) |
More flexible Connection interface.
-rw-r--r-- | Connection.hs | 24 | ||||
-rw-r--r-- | Connection/Tcp.hs | 23 | ||||
-rw-r--r-- | examples/dhtd.hs | 25 |
3 files changed, 41 insertions, 31 deletions
diff --git a/Connection.hs b/Connection.hs index fc4025eb..a7e5d4cc 100644 --- a/Connection.hs +++ b/Connection.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE DeriveFunctor #-} | 1 | {-# LANGUAGE DeriveFunctor #-} |
2 | {-# LANGUAGE LambdaCase #-} | ||
2 | module Connection where | 3 | module Connection where |
3 | 4 | ||
4 | import Control.Applicative | 5 | import Control.Applicative |
@@ -25,12 +26,11 @@ data Policy | |||
25 | | TryingToConnect -- ^ We desire to be connected. | 26 | | TryingToConnect -- ^ We desire to be connected. |
26 | deriving (Eq,Ord,Show) | 27 | deriving (Eq,Ord,Show) |
27 | 28 | ||
28 | -- | Read-only information obtained via the 'connections' interface to | 29 | -- | Information obtained via the 'connectionStatus' interface to |
29 | -- 'Manager'. | 30 | -- 'Manager'. |
30 | data Connection status = Connection | 31 | data Connection status = Connection |
31 | { connStatus :: STM (Status status) | 32 | { connStatus :: Status status |
32 | , connPolicy :: STM Policy | 33 | , connPolicy :: Policy |
33 | , connPingLogic :: PingMachine | ||
34 | } | 34 | } |
35 | deriving Functor | 35 | deriving Functor |
36 | 36 | ||
@@ -48,9 +48,10 @@ data Connection status = Connection | |||
48 | data Manager status k = Manager | 48 | data Manager status k = Manager |
49 | { -- | Connect or disconnect a connection. | 49 | { -- | Connect or disconnect a connection. |
50 | setPolicy :: k -> Policy -> IO () | 50 | setPolicy :: k -> Policy -> IO () |
51 | -- | Obtain a list (in Map form) of all possible connections, whether | 51 | -- | Lookup a connection status. |
52 | -- connected or not. | 52 | , status :: k -> STM (Connection status) |
53 | , connections :: STM (Map k (Connection status)) | 53 | -- | Obtain a list of all known connections. |
54 | , connections :: STM [k] | ||
54 | -- | Parse a connection key out of a string. Inverse of 'showKey'. | 55 | -- | Parse a connection key out of a string. Inverse of 'showKey'. |
55 | , stringToKey :: String -> Maybe k | 56 | , stringToKey :: String -> Maybe k |
56 | -- | Convert a progress value to a string. | 57 | -- | Convert a progress value to a string. |
@@ -74,10 +75,13 @@ addManagers :: (Ord kA, Ord kB) => | |||
74 | -> Manager (Either statusA statusB) (Either kA kB) | 75 | -> Manager (Either statusA statusB) (Either kA kB) |
75 | addManagers mgrA mgrB = Manager | 76 | addManagers mgrA mgrB = Manager |
76 | { setPolicy = either (setPolicy mgrA) (setPolicy mgrB) | 77 | { setPolicy = either (setPolicy mgrA) (setPolicy mgrB) |
78 | , status = \case | ||
79 | Left k -> fmap Left <$> status mgrA k | ||
80 | Right k -> fmap Right <$> status mgrB k | ||
77 | , connections = do | 81 | , connections = do |
78 | as <- Map.toList <$> connections mgrA | 82 | as <- connections mgrA |
79 | bs <- Map.toList <$> connections mgrB | 83 | bs <- connections mgrB |
80 | return $ Map.fromList $ map (Left *** fmap Left) as ++ map (Right *** fmap Right) bs | 84 | return $ map Left as ++ map Right bs |
81 | , stringToKey = \str -> Left <$> stringToKey mgrA str | 85 | , stringToKey = \str -> Left <$> stringToKey mgrA str |
82 | <|> Right <$> stringToKey mgrB str | 86 | <|> Right <$> stringToKey mgrB str |
83 | , showProgress = either (showProgress mgrA) (showProgress mgrB) | 87 | , showProgress = either (showProgress mgrA) (showProgress mgrB) |
diff --git a/Connection/Tcp.hs b/Connection/Tcp.hs index e6be15b7..c6f3a8ce 100644 --- a/Connection/Tcp.hs +++ b/Connection/Tcp.hs | |||
@@ -790,9 +790,11 @@ tcpManager grokKey s2k resolvKey sv = do | |||
790 | (saddr,params,ms) -> ConnectWithEndlessRetry saddr params ms | 790 | (saddr,params,ms) -> ConnectWithEndlessRetry saddr params ms |
791 | OpenToConnect -> dput XMisc "TODO: TCP OpenToConnect" | 791 | OpenToConnect -> dput XMisc "TODO: TCP OpenToConnect" |
792 | RefusingToConnect -> dput XMisc "TODO: TCP RefusingToConnect" | 792 | RefusingToConnect -> dput XMisc "TODO: TCP RefusingToConnect" |
793 | , connections = do | 793 | , status = \k -> do |
794 | c <- readTVar $ conmap sv | 794 | c <- readTVar (conmap sv) |
795 | fmap (exportConnection nullping c) <$> readTVar rmap | 795 | ck <- Map.lookup k <$> readTVar rmap |
796 | return $ exportConnection c (join ck) | ||
797 | , connections = Map.keys <$> readTVar rmap | ||
796 | , stringToKey = s2k | 798 | , stringToKey = s2k |
797 | , showProgress = \case | 799 | , showProgress = \case |
798 | Resolving -> "resolving" | 800 | Resolving -> "resolving" |
@@ -801,19 +803,16 @@ tcpManager grokKey s2k resolvKey sv = do | |||
801 | , showKey = show | 803 | , showKey = show |
802 | } | 804 | } |
803 | 805 | ||
804 | exportConnection :: Ord conkey => PingMachine -> Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus | 806 | exportConnection :: Ord conkey => Map conkey (ConnectionRecord u) -> Maybe conkey -> G.Connection TCPStatus |
805 | exportConnection nullping conmap mkey = G.Connection | 807 | exportConnection conmap mkey = G.Connection |
806 | { G.connStatus = case mkey of | 808 | { G.connStatus = case mkey of |
807 | Nothing -> return $ G.Dormant | 809 | Nothing -> G.Dormant |
808 | Just conkey -> case Map.lookup conkey conmap of | 810 | Just conkey -> case Map.lookup conkey conmap of |
809 | Nothing -> return $ G.InProgress Resolving | 811 | Nothing -> G.InProgress Resolving |
810 | Just (ConnectionRecord ckont cstate cdata) -> return $ case cstate of | 812 | Just (ConnectionRecord ckont cstate cdata) -> case cstate of |
811 | SaneConnection {} -> G.Established | 813 | SaneConnection {} -> G.Established |
812 | ConnectionPair {} -> G.Established | 814 | ConnectionPair {} -> G.Established |
813 | ReadOnlyConnection {} -> G.InProgress AwaitingWrite | 815 | ReadOnlyConnection {} -> G.InProgress AwaitingWrite |
814 | WriteOnlyConnection {} -> G.InProgress AwaitingRead | 816 | WriteOnlyConnection {} -> G.InProgress AwaitingRead |
815 | , G.connPolicy = return TryingToConnect | 817 | , G.connPolicy = TryingToConnect |
816 | , G.connPingLogic = case mkey >>= flip Map.lookup conmap of | ||
817 | Nothing -> nullping | ||
818 | Just (ConnectionRecord _ cstate _) -> connPingTimer cstate | ||
819 | } | 818 | } |
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index d6049c13..45a2a682 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -351,6 +351,10 @@ clientSession0 s sock cnum h = do | |||
351 | parseDebugTag :: String -> Maybe DebugTag | 351 | parseDebugTag :: String -> Maybe DebugTag |
352 | parseDebugTag s' = readMaybe ('X':map toUpper (take 1 s') ++ drop 1 s') | 352 | parseDebugTag s' = readMaybe ('X':map toUpper (take 1 s') ++ drop 1 s') |
353 | 353 | ||
354 | showPolicy TryingToConnect = "*" | ||
355 | showPolicy OpenToConnect = "o" | ||
356 | showPolicy RefusingToConnect = "x" | ||
357 | |||
354 | clientSession :: Session -> t1 -> t -> ClientHandle -> IO () | 358 | clientSession :: Session -> t1 -> t -> ClientHandle -> IO () |
355 | clientSession s@Session{..} sock cnum h = do | 359 | clientSession s@Session{..} sock cnum h = do |
356 | line <- dropWhile isSpace <$> hGetClientLine h | 360 | line <- dropWhile isSpace <$> hGetClientLine h |
@@ -640,10 +644,7 @@ clientSession s@Session{..} sock cnum h = do | |||
640 | ca <- readTVar $ contactLastSeenAddr c | 644 | ca <- readTVar $ contactLastSeenAddr c |
641 | cf <- readTVar $ contactFriendRequest c | 645 | cf <- readTVar $ contactFriendRequest c |
642 | cp <- readTVar $ contactPolicy c | 646 | cp <- readTVar $ contactPolicy c |
643 | let showPolicy TryingToConnect = "*" | 647 | let summarizeNodeId | nosummary = id |
644 | showPolicy OpenToConnect = "o" | ||
645 | showPolicy RefusingToConnect = "x" | ||
646 | summarizeNodeId | nosummary = id | ||
647 | | otherwise = take 6 | 648 | | otherwise = take 6 |
648 | summarizeAddr | nosummary = id | 649 | summarizeAddr | nosummary = id |
649 | | otherwise = reverse . take 20 . reverse | 650 | | otherwise = reverse . take 20 . reverse |
@@ -1275,12 +1276,18 @@ clientSession s@Session{..} sock cnum h = do | |||
1275 | ("c", s) | Just (ConnectionManager mgr) <- connectionManager | 1276 | ("c", s) | Just (ConnectionManager mgr) <- connectionManager |
1276 | , "" <- strp s | 1277 | , "" <- strp s |
1277 | -> cmd0 $ join $ atomically $ do | 1278 | -> cmd0 $ join $ atomically $ do |
1278 | cmap <- connections mgr | 1279 | cs <- do |
1279 | cs <- Map.toList <$> mapM connStatus cmap | 1280 | ks <- connections mgr |
1280 | let mkrow = Connection.showKey mgr *** Connection.showStatus mgr | 1281 | forM ks $ \k -> do |
1281 | rs = map mkrow cs | 1282 | stat <- Connection.status mgr k |
1283 | return (k,stat) | ||
1284 | let mkrow (k,st) = [ Connection.showKey mgr k | ||
1285 | , Connection.showStatus mgr (connStatus st) | ||
1286 | , showPolicy (connPolicy st) | ||
1287 | ] | ||
1288 | rs = map mkrow cs | ||
1282 | return $ do | 1289 | return $ do |
1283 | hPutClient h $ showReport rs | 1290 | hPutClient h $ showColumns rs |
1284 | 1291 | ||
1285 | ("help", s) | Just DHT{..} <- Map.lookup netname dhts | 1292 | ("help", s) | Just DHT{..} <- Map.lookup netname dhts |
1286 | -> cmd0 $ do | 1293 | -> cmd0 $ do |