summaryrefslogtreecommitdiff
path: root/Connection.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Connection.hs')
-rw-r--r--Connection.hs58
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 #-}
2module Connection where
3
4import Control.Applicative
5import Control.Arrow
6import Control.Concurrent.STM
7import qualified Data.Map as Map
8 ;import Data.Map (Map)
9
10import PingMachine
11
12data Status status
13 = Dormant
14 | InProgress status
15 | Established
16 deriving Functor
17
18data Policy
19 = RefusingToConnect
20 | OpenToConnect
21 | TryingToConnect
22
23data Connection status = Connection
24 { connStatus :: STM (Status status)
25 , connPolicy :: STM Policy
26 , connPingLogic :: PingMachine
27 }
28 deriving Functor
29
30data 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
38showStatus :: Manager status k -> Status status -> String
39showStatus mgr Dormant = "dormant"
40showStatus mgr Established = "established"
41showStatus mgr (InProgress s) = "in progress ("++showProgress mgr s++")"
42
43
44addManagers :: (Ord kA, Ord kB) =>
45 Manager statusA kA
46 -> Manager statusB kB
47 -> Manager (Either statusA statusB) (Either kA kB)
48addManagers 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 }