Skip to content

Commit

Permalink
feat: gq server command line parsing, max client count setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xspanger3770 committed Nov 12, 2023
1 parent 1ceb12d commit f4ccaf8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions GlobalQuakeServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<version>0.10.0_pre1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.0</version>
</dependency>

</dependencies>

Expand Down
44 changes: 41 additions & 3 deletions GlobalQuakeServer/src/main/java/gqserver/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import gqserver.server.GlobalQuakeServer;
import gqserver.ui.server.DatabaseMonitorFrame;
import org.apache.commons.cli.*;
import org.tinylog.Logger;

import java.io.File;
Expand Down Expand Up @@ -43,13 +44,50 @@ private static void startDatabaseManager() throws FatalIOException {

public static void main(String[] args) {
initErrorHandler();
GlobalQuake.prepare(Main.MAIN_FOLDER, Main.getErrorHandler());

Options options = new Options();

Option headlessOption = new Option("h", "headless", false, "run in headless mode");
headlessOption.setRequired(false);
options.addOption(headlessOption);

Option maxClientsOption = new Option("c", "clients", true, "maximum number of clients");
maxClientsOption.setRequired(false);
options.addOption(maxClientsOption);

CommandLineParser parser = new org.apache.commons.cli.BasicParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.err.println(e.getMessage());
formatter.printHelp("gqserver", options);

if(args.length > 0 && (args[0].equals("--headless"))){
System.exit(1);
}

if(cmd.hasOption(headlessOption.getOpt())){
headless = true;
Logger.info("Running as headless");
}

GlobalQuake.prepare(Main.MAIN_FOLDER, Main.getErrorHandler());
if(cmd.hasOption(maxClientsOption.getOpt())) {
try {
int maxCli = Integer.parseInt(cmd.getOptionValue(maxClientsOption.getOpt()));
if(maxCli < 1){
throw new IllegalArgumentException("Maximum client count must be at least 1!");
}
Settings.maxClients = maxCli;
Logger.info("Maximum client count set to %d".formatted(Settings.maxClients));
} catch(IllegalArgumentException e){
Logger.error(e);
System.exit(1);
}
}

Logger.info("Headless = %s".formatted(headless));

try {
startDatabaseManager();
Expand Down

0 comments on commit f4ccaf8

Please sign in to comment.