Add: Main, ClientApp, and ServerApp classes for application entry point with... #159
+4
-1
@@ -102,4 +102,7 @@ $RECYCLE.BIN/
|
|||||||
*.lnk
|
*.lnk
|
||||||
|
|
||||||
## Jenv
|
## Jenv
|
||||||
.java-version
|
.java-version
|
||||||
|
|
||||||
|
## VSCode
|
||||||
|
.vscode
|
||||||
+1
-1
@@ -14,7 +14,7 @@ java {
|
|||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
mainClass = 'ch.unibas.dmi.dbis.cs108.example.HelloWorld'
|
mainClass = 'ch.unibas.dmi.dbis.cs108.casono.Main'
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
rootProject.name = 'example-project'
|
rootProject.name = 'casono'
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.client.ClientApp;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.ServerApp;
|
||||||
|
|
||||||
|
public final class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (!isValid(args)) {
|
||||||
|
printUsage();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (args[0]) {
|
||||||
|
case "server" -> ServerApp.start(args[1]);
|
||||||
|
case "client" -> ClientApp.start(args[1]);
|
||||||
|
default -> {
|
||||||
|
printUsage();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isValid(String[] args) {
|
||||||
|
if (args.length != 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return switch (args[0]) {
|
||||||
|
case "server", "client" -> true;
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printUsage() {
|
||||||
|
System.err.println("""
|
||||||
|
Usage:
|
||||||
|
java -jar xyz.jar server <listenPort>
|
||||||
|
java -jar xyz.jar client <serverIp>:<serverPort>
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.client;
|
||||||
|
|
||||||
|
public class ClientApp {
|
||||||
|
public static void start(String arg) {
|
||||||
|
String[] parts = arg.split(":", 2);
|
||||||
|
if (parts.length != 2) {
|
||||||
|
throw new IllegalArgumentException("Address must be in format <ip>:<port>.");
|
||||||
|
}
|
||||||
|
String host = parts[0];
|
||||||
|
int port = Integer.parseInt(parts[1]);
|
||||||
|
|
||||||
|
System.out.println("You've selected the client. It will connect port " + port + " at host " + host);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server;
|
||||||
|
|
||||||
|
public class ServerApp {
|
||||||
|
public static void start(String arg) {
|
||||||
|
int port = Integer.parseInt(arg);
|
||||||
|
System.out.println("You've selected the server. It will accept connections at port " + port);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user