-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathClient.hs
More file actions
72 lines (64 loc) · 2.1 KB
/
Client.hs
File metadata and controls
72 lines (64 loc) · 2.1 KB
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
65
66
67
68
69
70
71
72
{-# LANGUAGE CPP #-}
module Client
( getServerStatus
, stopServer
, serverCommand
) where
import Control.Exception (tryJust)
import Control.Monad (guard)
import Network (connectTo)
#ifdef mingw32_HOST_OS
import Network (PortID(PortNumber))
#else
import Network (PortID(UnixSocket))
#endif
import System.Exit (exitFailure, exitWith)
import System.IO (Handle, hClose, hFlush, hGetLine, hPutStrLn, stderr)
import System.IO.Error (isDoesNotExistError)
import Daemonize (daemonize)
import Server (createListenSocket, startServer)
import Types (ClientDirective(..), Command(..), ServerDirective(..))
import Util (readMaybe, connect)
getServerStatus :: FilePath -> IO ()
getServerStatus sock = do
h <- connect sock
hPutStrLn h $ show SrvStatus
hFlush h
startClientReadLoop h
stopServer :: FilePath -> IO ()
stopServer sock = do
h <- connect sock
hPutStrLn h $ show SrvExit
hFlush h
startClientReadLoop h
serverCommand :: FilePath -> Command -> [String] -> IO ()
serverCommand sock cmd ghcOpts = do
r <- tryJust (guard . isDoesNotExistError) (connect sock)
case r of
Right h -> do
hPutStrLn h $ show (SrvCommand cmd ghcOpts)
hFlush h
startClientReadLoop h
Left _ -> do
daemonize False sock
serverCommand sock cmd ghcOpts
startClientReadLoop :: Handle -> IO ()
startClientReadLoop h = do
msg <- hGetLine h
let clientDirective = readMaybe msg
case clientDirective of
Just (ClientStdout out) -> putStrLn out >> startClientReadLoop h
Just (ClientStderr err) -> hPutStrLn stderr err >> startClientReadLoop h
Just (ClientExit exitCode) -> hClose h >> exitWith exitCode
Just (ClientUnexpectedError err) -> hClose h >> unexpectedError err
Nothing -> do
hClose h
unexpectedError $
"The server sent an invalid message to the client: " ++ show msg
unexpectedError :: String -> IO ()
unexpectedError err = do
hPutStrLn stderr banner
hPutStrLn stderr err
hPutStrLn stderr banner
exitFailure
where banner = replicate 78 '*'