diff options
Diffstat (limited to 'Connection.hs')
-rw-r--r-- | Connection.hs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Connection.hs b/Connection.hs new file mode 100644 index 00000000..3287bc1b --- /dev/null +++ b/Connection.hs | |||
@@ -0,0 +1,58 @@ | |||
1 | {-# LANGUAGE DeriveFunctor #-} | ||
2 | module Connection where | ||
3 | |||
4 | import Control.Applicative | ||
5 | import Control.Arrow | ||
6 | import Control.Concurrent.STM | ||
7 | import qualified Data.Map as Map | ||
8 | ;import Data.Map (Map) | ||
9 | |||
10 | import PingMachine | ||
11 | |||
12 | data Status status | ||
13 | = Dormant | ||
14 | | InProgress status | ||
15 | | Established | ||
16 | deriving Functor | ||
17 | |||
18 | data Policy | ||
19 | = RefusingToConnect | ||
20 | | OpenToConnect | ||
21 | | TryingToConnect | ||
22 | |||
23 | data Connection status = Connection | ||
24 | { connStatus :: STM (Status status) | ||
25 | , connPolicy :: STM Policy | ||
26 | , connPingLogic :: PingMachine | ||
27 | } | ||
28 | deriving Functor | ||
29 | |||
30 | data Manager status k = Manager | ||
31 | { setPolicy :: k -> Policy -> IO () | ||
32 | , connections :: STM (Map k (Connection status)) | ||
33 | , stringToKey :: String -> Maybe k | ||
34 | , showProgress :: status -> String | ||
35 | , showKey :: k -> String | ||
36 | } | ||
37 | |||
38 | showStatus :: Manager status k -> Status status -> String | ||
39 | showStatus mgr Dormant = "dormant" | ||
40 | showStatus mgr Established = "established" | ||
41 | showStatus mgr (InProgress s) = "in progress ("++showProgress mgr s++")" | ||
42 | |||
43 | |||
44 | addManagers :: (Ord kA, Ord kB) => | ||
45 | Manager statusA kA | ||
46 | -> Manager statusB kB | ||
47 | -> Manager (Either statusA statusB) (Either kA kB) | ||
48 | addManagers mgrA mgrB = Manager | ||
49 | { setPolicy = either (setPolicy mgrA) (setPolicy mgrB) | ||
50 | , connections = do | ||
51 | as <- Map.toList <$> connections mgrA | ||
52 | bs <- Map.toList <$> connections mgrB | ||
53 | return $ Map.fromList $ map (Left *** fmap Left) as ++ map (Right *** fmap Right) bs | ||
54 | , stringToKey = \str -> Left <$> stringToKey mgrA str | ||
55 | <|> Right <$> stringToKey mgrB str | ||
56 | , showProgress = either (showProgress mgrA) (showProgress mgrB) | ||
57 | , showKey = either (showKey mgrA) (showKey mgrB) | ||
58 | } | ||