|
@@ -2,21 +2,85 @@ package fr.insa.clavardator.server;
|
2
|
2
|
|
3
|
3
|
import fr.insa.clavardator.lib.util.Log;
|
4
|
4
|
|
|
5
|
+import java.util.List;
|
5
|
6
|
import java.util.Scanner;
|
6
|
7
|
|
7
|
8
|
public class Main {
|
|
9
|
+ private static final int PROXY_PORT = 35750;
|
|
10
|
+ private static final int PRESENCE_PORT = 35650;
|
|
11
|
+
|
|
12
|
+ private static final List<String> HELP_ARGS = List.of("-h", "--help");
|
|
13
|
+ private static final List<String> VERSION_ARGS = List.of("-v", "--version");
|
|
14
|
+ private static final List<String> PROXY_ARGS = List.of("-x", "--proxy");
|
|
15
|
+ private static final List<String> PRESENCE_ARGS = List.of("-p", "--presence");
|
|
16
|
+
|
|
17
|
+ private static int proxyPort = PROXY_PORT;
|
|
18
|
+ private static int presencePort = PRESENCE_PORT;
|
|
19
|
+
|
|
20
|
+ private static void printHelp() {
|
|
21
|
+ System.out.println("\nClavardator INSA presence server v1.0.0\n");
|
|
22
|
+ System.out.println("usage: <command> [options]\n");
|
|
23
|
+ System.out.println("Options:");
|
|
24
|
+ System.out.println(" -h, --help Display help message.");
|
|
25
|
+ System.out.println(" -v, --version Display version information.");
|
|
26
|
+ System.out.println(" -X <port>, --proxy <port> Set the proxy port.");
|
|
27
|
+ System.out.println(" -p <port>, --presence <port> Set the presence server port.");
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ private static void printVersion() {
|
|
31
|
+ System.out.println("\nClavardator INSA presence server v1.0.0\n");
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ private static int getPort(String value, int defaultValue) {
|
|
35
|
+ int port = defaultValue;
|
|
36
|
+ try {
|
|
37
|
+ port = Integer.parseInt(value.trim());
|
|
38
|
+ } catch (NumberFormatException e) {
|
|
39
|
+ System.out.println("Could not read port " + value + ". Using default.");
|
|
40
|
+ }
|
|
41
|
+ return port;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private static void readArgs(String[] args) {
|
|
45
|
+ if (args.length > 0) {
|
|
46
|
+ if (HELP_ARGS.contains(args[0])) {
|
|
47
|
+ printHelp();
|
|
48
|
+ System.exit(0);
|
|
49
|
+ }
|
|
50
|
+ if (VERSION_ARGS.contains(args[0])) {
|
|
51
|
+ printVersion();
|
|
52
|
+ System.exit(0);
|
|
53
|
+ }
|
|
54
|
+ for (int i = 0; i < args.length -1; i++) {
|
|
55
|
+ if (PROXY_ARGS.contains(args[i])) {
|
|
56
|
+ proxyPort = getPort(args[i + 1], PROXY_PORT);
|
|
57
|
+ }
|
|
58
|
+ if (PRESENCE_ARGS.contains(args[i])) {
|
|
59
|
+ presencePort = getPort(args[i + 1], PRESENCE_PORT);
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ if (presencePort == proxyPort) {
|
|
63
|
+ System.out.println("Proxy and presence ports must be different");
|
|
64
|
+ System.exit(1);
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ }
|
8
|
68
|
|
9
|
69
|
public static void main(String[] args) {
|
10
|
|
- Log.v(Main.class.getSimpleName(), "Server started! You can stop it by typing stop");
|
11
|
70
|
|
12
|
|
- Proxy proxy = new Proxy();
|
13
|
|
- Presence presence = new Presence();
|
|
71
|
+ readArgs(args);
|
|
72
|
+
|
|
73
|
+ System.out.println("\nType q then press enter to exit\n");
|
|
74
|
+ Log.v(Main.class.getSimpleName(), "Starting proxy on port " + proxyPort);
|
|
75
|
+ Proxy proxy = new Proxy(proxyPort);
|
|
76
|
+ Log.v(Main.class.getSimpleName(), "Starting presence server on port " + presencePort);
|
|
77
|
+ Presence presence = new Presence(presencePort);
|
14
|
78
|
|
15
|
79
|
String line;
|
16
|
80
|
Scanner scanner = new Scanner(System.in);
|
17
|
81
|
do {
|
18
|
82
|
line = scanner.nextLine();
|
19
|
|
- } while (!line.equals("stop"));
|
|
83
|
+ } while (!line.equals("q"));
|
20
|
84
|
|
21
|
85
|
proxy.stop();
|
22
|
86
|
presence.stop();
|