feat: created main, and implemented log-level setting and config loading

This commit is contained in:
Yannik Bretschneider 2021-06-04 18:46:44 +02:00
parent d534897a5b
commit b37737f073

View File

@ -1,7 +1,146 @@
package uulm.teamname.marvelous.server; 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 class Server {
public static void main(String[] args) { 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 <b>BEFORE ANY LOGGING OPERATIONS</b> .
*/
private static void setLogLevel(int logLevel) {
// System.out.println("setting log level to " + logLevel);
Map<String, String> 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();
} }
} }