diff options
Diffstat (limited to 'Connection.hs')
-rw-r--r-- | Connection.hs | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/Connection.hs b/Connection.hs index 58b4f4e5..fc4025eb 100644 --- a/Connection.hs +++ b/Connection.hs | |||
@@ -9,18 +9,24 @@ import qualified Data.Map as Map | |||
9 | 9 | ||
10 | import PingMachine | 10 | import PingMachine |
11 | 11 | ||
12 | -- | This type indicates the current status of a connection. The type | ||
13 | -- parameter indicates protocol-specific status information. To present | ||
14 | -- information as a user-comprehensible string, use 'showStatus'. | ||
12 | data Status status | 15 | data Status status |
13 | = Dormant | 16 | = Dormant |
14 | | InProgress status | 17 | | InProgress status |
15 | | Established | 18 | | Established |
16 | deriving (Show,Eq,Ord,Functor) | 19 | deriving (Show,Eq,Ord,Functor) |
17 | 20 | ||
21 | -- | A policy indicates a desired connection status. | ||
18 | data Policy | 22 | data Policy |
19 | = RefusingToConnect | 23 | = RefusingToConnect -- ^ We desire no connection. |
20 | | OpenToConnect | 24 | | OpenToConnect -- ^ We will cooperate if a remote side initiates. |
21 | | TryingToConnect | 25 | | TryingToConnect -- ^ We desire to be connected. |
22 | deriving (Eq,Ord,Show) | 26 | deriving (Eq,Ord,Show) |
23 | 27 | ||
28 | -- | Read-only information obtained via the 'connections' interface to | ||
29 | -- 'Manager'. | ||
24 | data Connection status = Connection | 30 | data Connection status = Connection |
25 | { connStatus :: STM (Status status) | 31 | { connStatus :: STM (Status status) |
26 | , connPolicy :: STM Policy | 32 | , connPolicy :: STM Policy |
@@ -28,20 +34,40 @@ data Connection status = Connection | |||
28 | } | 34 | } |
29 | deriving Functor | 35 | deriving Functor |
30 | 36 | ||
37 | -- | This is an interface to make or query status information about connections | ||
38 | -- of a specific kind. | ||
39 | -- | ||
40 | -- Type parameters: | ||
41 | -- | ||
42 | -- /k/ names a connection. It should implement Ord, and can be parsed and | ||
43 | -- displayed using 'stringToKey' and 'showKey'. | ||
44 | -- | ||
45 | -- /status/ indicates the progress of a connection. It is intended as a | ||
46 | -- parameter to the 'InProgress' constructor of 'Status'. | ||
47 | -- | ||
31 | data Manager status k = Manager | 48 | data Manager status k = Manager |
32 | { setPolicy :: k -> Policy -> IO () | 49 | { -- | Connect or disconnect a connection. |
50 | setPolicy :: k -> Policy -> IO () | ||
51 | -- | Obtain a list (in Map form) of all possible connections, whether | ||
52 | -- connected or not. | ||
33 | , connections :: STM (Map k (Connection status)) | 53 | , connections :: STM (Map k (Connection status)) |
54 | -- | Parse a connection key out of a string. Inverse of 'showKey'. | ||
34 | , stringToKey :: String -> Maybe k | 55 | , stringToKey :: String -> Maybe k |
56 | -- | Convert a progress value to a string. | ||
35 | , showProgress :: status -> String | 57 | , showProgress :: status -> String |
58 | -- | Show a connection key as a string. | ||
36 | , showKey :: k -> String | 59 | , showKey :: k -> String |
37 | } | 60 | } |
38 | 61 | ||
62 | -- | Present status information (visible in a UI) for a connection. | ||
39 | showStatus :: Manager status k -> Status status -> String | 63 | showStatus :: Manager status k -> Status status -> String |
40 | showStatus mgr Dormant = "dormant" | 64 | showStatus mgr Dormant = "dormant" |
41 | showStatus mgr Established = "established" | 65 | showStatus mgr Established = "established" |
42 | showStatus mgr (InProgress s) = "in progress ("++showProgress mgr s++")" | 66 | showStatus mgr (InProgress s) = "in progress ("++showProgress mgr s++")" |
43 | 67 | ||
44 | 68 | ||
69 | -- | Combine two different species of 'Manager' into a single interface using | ||
70 | -- 'Either' to combine key and status types. | ||
45 | addManagers :: (Ord kA, Ord kB) => | 71 | addManagers :: (Ord kA, Ord kB) => |
46 | Manager statusA kA | 72 | Manager statusA kA |
47 | -> Manager statusB kB | 73 | -> Manager statusB kB |