Browse Source

Allow using cli for server

Arnaud Vergnet 3 years ago
parent
commit
4d21742261

+ 5
- 0
server/build.gradle View File

@@ -1,5 +1,6 @@
1 1
 plugins {
2 2
     id 'application'
3
+    id 'com.github.johnrengelman.shadow' version '6.1.0'
3 4
 }
4 5
 
5 6
 group 'fr.insa.clavardator.server'
@@ -16,6 +17,10 @@ dependencies {
16 17
 
17 18
 mainClassName = 'fr.insa.clavardator.server.Main'
18 19
 
20
+shadowJar {
21
+    mergeServiceFiles()
22
+}
23
+
19 24
 run{
20 25
     standardInput = System.in
21 26
 }

+ 68
- 4
server/src/main/java/fr/insa/clavardator/server/Main.java View File

@@ -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();

+ 2
- 4
server/src/main/java/fr/insa/clavardator/server/Presence.java View File

@@ -12,19 +12,17 @@ import java.net.Socket;
12 12
 import java.util.ArrayList;
13 13
 import java.util.HashMap;
14 14
 import java.util.Map;
15
-import java.util.stream.Collectors;
16 15
 
17 16
 public class Presence {
18
-	private static final int PRESENCE_PORT = 35650;
19 17
 
20 18
 	private final Map<String, TcpConnection> subscribers = new HashMap<>();
21 19
 	private final ArrayList<UserInformation> connectedUsers = new ArrayList<>();
22 20
 	private final TcpListener presenceListener;
23 21
 
24
-	public Presence() {
22
+	public Presence(int port) {
25 23
 		presenceListener = new TcpListener();
26 24
 		presenceListener.acceptConnection(
27
-				PRESENCE_PORT,
25
+				port,
28 26
 				this::subscribe,
29 27
 				e -> Log.e(getClass().getSimpleName(), "Error while registering a user", e));
30 28
 	}

+ 2
- 3
server/src/main/java/fr/insa/clavardator/server/Proxy.java View File

@@ -12,14 +12,13 @@ import java.util.HashMap;
12 12
 import java.util.Map;
13 13
 
14 14
 public class Proxy {
15
-	private static final int PROXY_PORT = 35750;
16 15
 	private final HashMap<String, TcpConnection> users = new HashMap<>();
17 16
 	private final TcpListener proxyListener;
18 17
 
19
-	public Proxy() {
18
+	public Proxy(int port) {
20 19
 		proxyListener = new TcpListener();
21 20
 		proxyListener.acceptConnection(
22
-				PROXY_PORT,
21
+				port,
23 22
 				clientSocket -> {
24 23
 
25 24
 					Log.v(getClass().getSimpleName(), "Accepting a new user");

Loading…
Cancel
Save