netserver/Opts.hs
author unc0rr
Sun, 02 Nov 2008 20:48:10 +0000
changeset 1462 d3323637da1f
parent 1386 674429128152
child 1473 60e1fad78d58
permissions -rw-r--r--
Client sends PONG to server's PING
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     1
module Opts where
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     2
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     3
import System
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     4
import System.Console.GetOpt
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     5
import Network
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     6
import Data.Maybe ( fromMaybe )
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     7
import Miscutils
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
     8
import System.IO.Unsafe
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     9
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    10
data GlobalOptions =
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    11
	GlobalOptions
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    12
	{
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    13
		isDedicated :: Bool,
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    14
		serverMessage :: String,
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    15
		listenPort :: PortNumber
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    16
	}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    17
defaultMessage = "<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>"
1386
674429128152 Oops... start a dedicated server by default
unc0rr
parents: 1383
diff changeset
    18
defaultOptions = (GlobalOptions True defaultMessage 46631)
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    19
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    20
options :: [OptDescr (GlobalOptions -> GlobalOptions)]
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    21
options = [
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    22
	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    23
	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)"
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    24
	]
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    25
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    26
readListenPort, readDedicated :: String -> GlobalOptions -> GlobalOptions
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    27
readListenPort str opts = opts{listenPort = readPort}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    28
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    29
		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    30
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    31
readDedicated str opts = opts{isDedicated = readDedicated}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    32
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    33
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    34
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    35
opts :: IO GlobalOptions
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    36
opts = do
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    37
	args <- getArgs
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    38
	case getOpt Permute options args of
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    39
		(o, [], []) -> return $ foldr ($) defaultOptions o
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    40
		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    41
	where header = "Usage: newhwserv [OPTION...]"
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    42
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    43
{-# NOINLINE globalOptions #-}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    44
globalOptions = unsafePerformIO opts