Allow using cli for server
This commit is contained in:
förälder
2fc3a00264
incheckning
4d21742261
4 ändrade filer med 77 tillägg och 11 borttagningar
|
@ -1,5 +1,6 @@
|
|||
plugins {
|
||||
id 'application'
|
||||
id 'com.github.johnrengelman.shadow' version '6.1.0'
|
||||
}
|
||||
|
||||
group 'fr.insa.clavardator.server'
|
||||
|
@ -16,6 +17,10 @@ dependencies {
|
|||
|
||||
mainClassName = 'fr.insa.clavardator.server.Main'
|
||||
|
||||
shadowJar {
|
||||
mergeServiceFiles()
|
||||
}
|
||||
|
||||
run{
|
||||
standardInput = System.in
|
||||
}
|
|
@ -2,21 +2,85 @@ package fr.insa.clavardator.server;
|
|||
|
||||
import fr.insa.clavardator.lib.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
private static final int PROXY_PORT = 35750;
|
||||
private static final int PRESENCE_PORT = 35650;
|
||||
|
||||
private static final List<String> HELP_ARGS = List.of("-h", "--help");
|
||||
private static final List<String> VERSION_ARGS = List.of("-v", "--version");
|
||||
private static final List<String> PROXY_ARGS = List.of("-x", "--proxy");
|
||||
private static final List<String> PRESENCE_ARGS = List.of("-p", "--presence");
|
||||
|
||||
private static int proxyPort = PROXY_PORT;
|
||||
private static int presencePort = PRESENCE_PORT;
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.println("\nClavardator INSA presence server v1.0.0\n");
|
||||
System.out.println("usage: <command> [options]\n");
|
||||
System.out.println("Options:");
|
||||
System.out.println(" -h, --help Display help message.");
|
||||
System.out.println(" -v, --version Display version information.");
|
||||
System.out.println(" -X <port>, --proxy <port> Set the proxy port.");
|
||||
System.out.println(" -p <port>, --presence <port> Set the presence server port.");
|
||||
}
|
||||
|
||||
private static void printVersion() {
|
||||
System.out.println("\nClavardator INSA presence server v1.0.0\n");
|
||||
}
|
||||
|
||||
private static int getPort(String value, int defaultValue) {
|
||||
int port = defaultValue;
|
||||
try {
|
||||
port = Integer.parseInt(value.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Could not read port " + value + ". Using default.");
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
private static void readArgs(String[] args) {
|
||||
if (args.length > 0) {
|
||||
if (HELP_ARGS.contains(args[0])) {
|
||||
printHelp();
|
||||
System.exit(0);
|
||||
}
|
||||
if (VERSION_ARGS.contains(args[0])) {
|
||||
printVersion();
|
||||
System.exit(0);
|
||||
}
|
||||
for (int i = 0; i < args.length -1; i++) {
|
||||
if (PROXY_ARGS.contains(args[i])) {
|
||||
proxyPort = getPort(args[i + 1], PROXY_PORT);
|
||||
}
|
||||
if (PRESENCE_ARGS.contains(args[i])) {
|
||||
presencePort = getPort(args[i + 1], PRESENCE_PORT);
|
||||
}
|
||||
}
|
||||
if (presencePort == proxyPort) {
|
||||
System.out.println("Proxy and presence ports must be different");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Log.v(Main.class.getSimpleName(), "Server started! You can stop it by typing stop");
|
||||
|
||||
Proxy proxy = new Proxy();
|
||||
Presence presence = new Presence();
|
||||
readArgs(args);
|
||||
|
||||
System.out.println("\nType q then press enter to exit\n");
|
||||
Log.v(Main.class.getSimpleName(), "Starting proxy on port " + proxyPort);
|
||||
Proxy proxy = new Proxy(proxyPort);
|
||||
Log.v(Main.class.getSimpleName(), "Starting presence server on port " + presencePort);
|
||||
Presence presence = new Presence(presencePort);
|
||||
|
||||
String line;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
do {
|
||||
line = scanner.nextLine();
|
||||
} while (!line.equals("stop"));
|
||||
} while (!line.equals("q"));
|
||||
|
||||
proxy.stop();
|
||||
presence.stop();
|
||||
|
|
|
@ -12,19 +12,17 @@ import java.net.Socket;
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Presence {
|
||||
private static final int PRESENCE_PORT = 35650;
|
||||
|
||||
private final Map<String, TcpConnection> subscribers = new HashMap<>();
|
||||
private final ArrayList<UserInformation> connectedUsers = new ArrayList<>();
|
||||
private final TcpListener presenceListener;
|
||||
|
||||
public Presence() {
|
||||
public Presence(int port) {
|
||||
presenceListener = new TcpListener();
|
||||
presenceListener.acceptConnection(
|
||||
PRESENCE_PORT,
|
||||
port,
|
||||
this::subscribe,
|
||||
e -> Log.e(getClass().getSimpleName(), "Error while registering a user", e));
|
||||
}
|
||||
|
|
|
@ -12,14 +12,13 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
public class Proxy {
|
||||
private static final int PROXY_PORT = 35750;
|
||||
private final HashMap<String, TcpConnection> users = new HashMap<>();
|
||||
private final TcpListener proxyListener;
|
||||
|
||||
public Proxy() {
|
||||
public Proxy(int port) {
|
||||
proxyListener = new TcpListener();
|
||||
proxyListener.acceptConnection(
|
||||
PROXY_PORT,
|
||||
port,
|
||||
clientSocket -> {
|
||||
|
||||
Log.v(getClass().getSimpleName(), "Accepting a new user");
|
||||
|
|
Laddar…
Referens i nytt ärende