-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.hs
64 lines (59 loc) · 3.18 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent
import Network
import System.Environment
import System.Exit
import System.IO
import Data.ConfigFile
import Data.Either.Utils
import qualified Data.UUID.V1 as U1
import Gatekeeper.CRDT
import Gatekeeper.Heartbeat
import Gatekeeper.LDAP
import Gatekeeper.Network
import Gatekeeper.NetUtils
main :: IO ()
main = do
progName <- getProgName
args <- getArgs
if length args == 1
then start progName args
else do putStrLn $ "Usage: " ++ progName ++ " <config>"
exitFailure
getInitData :: IO (MVar State)
getInitData = do -- May eventually read from a config file or some shit
m <- newEmptyMVar
putMVar m (State (Set [] []) (Cluster [] []) [] (NetState (Host "" "" (HostClock "" 0)) "0" (LdapInfo "" "" "")))
return m
start :: String -> [String] -> IO ()
start progName args = do cfg <- forceEither `fmap` readfile emptyCP (head args)
d <- getInitData
let hosttext = forceEither $ get cfg "DEFAULT" "myhost"
let port = forceEither $ get cfg "DEFAULT" "port"
let ldapurl = forceEither $ get cfg "DEFAULT" "ldapurl"
let ldapusername = forceEither $ get cfg "DEFAULT" "ldapusername"
let ldappassword = forceEither $ get cfg "DEFAULT" "ldappassword"
let updateupper = forceEither $ get cfg "DEFAULT" "updateupper"
let updatelower = forceEither $ get cfg "DEFAULT" "updatelower"
let hbu = forceEither $ get cfg "DEFAULT" "heartbeatupper"
let hbl = forceEither $ get cfg "DEFAULT" "heartbeatlower"
let knownnode = case get cfg "DEFAULT" "knownnode" of
Left _ -> Nothing
Right a -> Just a
let portNum = PortNumber $ fromIntegral (read port :: Int)
host <- lookupHost hosttext
(Just uid) <- U1.nextUUID
modifyMVar_ d (\(State s (Cluster a r) v _)
-> let u = show uid
newhost = Host host u (HostClock u 0)
vclock = HostClock u 0
ld = LdapInfo ldapurl ldapusername ldappassword
in return $ State s (Cluster (newhost:a) r) (vclock:v) (NetState newhost port ld))
putStrLn $ progName ++ " started on port '" ++ port ++ "'. I am host '" ++ host ++ "'."
forkIO $ netloop d =<< listenOn portNum
forkIO $ ldaploop d updateupper updatelower
case knownnode of
Just h -> forkIO $ addSelf d h port
Nothing -> myThreadId
sendHeartbeats d hbu hbl
return ()