Allow --port=PORT command-line parameter to specify the port to listen on
authorunc0rr
Fri, 10 Oct 2008 13:18:45 +0000
changeset 1341 86d7d5ab22a2
parent 1340 430d210d54ae
child 1342 ae6c4f10ace2
Allow --port=PORT command-line parameter to specify the port to listen on
netserver/Opts.hs
netserver/newhwserv.hs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netserver/Opts.hs	Fri Oct 10 13:18:45 2008 +0000
@@ -0,0 +1,30 @@
+module Opts where
+
+import System
+import System.Console.GetOpt
+import Network
+import Data.Maybe ( fromMaybe )
+import Miscutils
+
+data Flag = ListenPort PortNumber
+	deriving Show
+
+options :: [OptDescr Flag]
+options = [
+	Option ['p'] ["port"] (OptArg defaultPort "PORT") "listen on PORT"
+	]
+
+defaultPort :: Maybe String -> Flag
+defaultPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))
+
+opts :: IO [Flag]
+opts = do
+	args <- getArgs
+	case getOpt Permute options args of
+		(o, [], []) -> return o
+		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
+	where header = "Usage: newhwserv [OPTION...]"
+
+getPort :: [Flag] -> PortNumber
+getPort [] = 46631
+getPort (ListenPort a:flags) = a
--- a/netserver/newhwserv.hs	Thu Oct 09 21:00:42 2008 +0000
+++ b/netserver/newhwserv.hs	Fri Oct 10 13:18:45 2008 +0000
@@ -11,6 +11,7 @@
 import Data.List
 import Miscutils
 import HWProto
+import Opts
 
 acceptLoop :: Socket -> TChan ClientInfo -> IO ()
 acceptLoop servSock acceptChan = do
@@ -87,5 +88,7 @@
 
 
 main = withSocketsDo $ do
-	serverSocket <- listenOn $ Service "hedgewars"
+	flags <- opts
+	putStrLn $ "Listening on port " ++ show (getPort flags)
+	serverSocket <- listenOn $ PortNumber (getPort flags)
 	startServer serverSocket `finally` sClose serverSocket