From b37737f073a7ba3d56ccde88e96cff3d1d4dac50 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Fri, 4 Jun 2021 18:46:44 +0200 Subject: [PATCH] feat: created main, and implemented log-level setting and config loading --- .../teamname/marvelous/server/Server.java | 141 +++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Server.java b/Server/src/main/java/uulm/teamname/marvelous/server/Server.java index a300724..7cd8256 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/Server.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Server.java @@ -1,7 +1,146 @@ package uulm.teamname.marvelous.server; +import com.beust.jcommander.JCommander; +import org.tinylog.Logger; +import org.tinylog.configuration.Configuration; +import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig; +import uulm.teamname.marvelous.gamelibrary.config.PartyConfig; +import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; +import uulm.teamname.marvelous.gamelibrary.json.JSON; +import uulm.teamname.marvelous.server.args.ServerArgs; +import uulm.teamname.marvelous.server.netconnector.MarvelousServer; + +import java.io.*; +import java.net.InetSocketAddress; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + public class Server { + public static void main(String[] args) { - System.out.println("There's nothing here yet. Sucks for you!"); + + ServerArgs serverArgs = new ServerArgs(); + + JCommander.newBuilder() + .addObject(serverArgs) + .build() + .parse(args); + + System.out.println(serverArgs); + + if (serverArgs.isVerbose()) { + setLogLevel(5); + } else { + setLogLevel(serverArgs.getLogLevel()); + } + + + ScenarioConfig scenarioConfig = readScenarioConfig(serverArgs.getScenarioConfigFile()); + CharacterConfig characterConfig = readCharacterConfig(serverArgs.getCharacterConfigFile()); + PartyConfig partyConfig = readPartyConfig(serverArgs.getMatchConfigFile()); + + InetSocketAddress address = new InetSocketAddress(serverArgs.getPort()); + Logger.trace("Inet address {} created", address); + + + + } + + /** Function that sets the log level for {@link Logger Tinylog}. + * It has to be executed BEFORE ANY LOGGING OPERATIONS . + */ + private static void setLogLevel(int logLevel) { + // System.out.println("setting log level to " + logLevel); + Map map = new HashMap<>(); + + Configuration.replace(map); + + String logLevelDescriptor = switch (logLevel) { + case 0 -> "off"; + case 1 -> "error"; + case 2 -> "warn"; + case 3 -> "info"; + case 4 -> "debug"; + case 5 -> "trace"; + default -> "info"; + }; + + // Add log writer 1, a console writer (which logs to the console) + map.put("writer1", "console"); + map.put("writer1.level", logLevelDescriptor); + + + // Add log writer 2, a file writer logging to the file server.log + map.put("writer2", "file"); + map.put("writer2.level", logLevelDescriptor); + map.put("writer2.file", "./logs/server.log"); + + Configuration.replace(map); + + Logger.info("Log level set to '" + logLevelDescriptor + "'"); + } + + private static ScenarioConfig readScenarioConfig(File source) { + + if (!source.exists()) { + Logger.error("Scenario Configuration file not found. Exiting..."); + System.exit(1); + } + var config = JSON.parseScenarioConfig(source); + if (config.isEmpty()) { + Logger.error("Scenario Configuration couldn't be parsed. Exiting..."); + System.exit(1); + } + + // TODO: Check whether Scenario Config is valid + + Logger.info("Scenario Config file loaded"); + + return config.get(); + } + + + private static CharacterConfig readCharacterConfig(File source) { + + if (!source.exists()) { + Logger.error("Character Configuration file not found. Exiting..."); + System.exit(1); + } + var config = JSON.parseCharacterConfig(source); + if (config.isEmpty()) { + Logger.error("Character Configuration couldn't be parsed. Exiting..."); + System.exit(1); + } + + + // TODO: Check whether Character Config is valid + + Logger.info("Character Config file loaded"); + + return config.get(); + } + + + private static PartyConfig readPartyConfig(File source) { + + if (!source.exists()) { + Logger.error("Party Configuration file not found. Exiting..."); + System.exit(1); + } + var config = JSON.parsePartyConfig(source); + if (config.isEmpty()) { + Logger.error("Party Configuration couldn't be parsed. Exiting..."); + System.exit(1); + } + + // TODO: Check whether Party Config is valid + + Logger.info("Party Config file loaded"); + + return config.get(); } }