diff --git a/.gitignore b/.gitignore index b63da45..c55ac0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +data.db + .gradle build/ !gradle/wrapper/gradle-wrapper.jar @@ -39,4 +41,4 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store diff --git a/src/main/java/net/pixtaded/crab/Main.java b/src/main/java/net/pixtaded/crab/Main.java index e35894e..ac4629d 100644 --- a/src/main/java/net/pixtaded/crab/Main.java +++ b/src/main/java/net/pixtaded/crab/Main.java @@ -7,9 +7,43 @@ import java.util.Scanner; public class Main { public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - chooseMode(scanner); - scanner.close(); + if (args.length == 0) { + Scanner scanner = new Scanner(System.in); + chooseMode(scanner); + scanner.close(); + return; + } + switch (args[0]) { + case "help" -> { + System.out.println("crab help - print this message."); + System.out.println("crab client [nick] - connect to a server."); + System.out.println("crab server - start a server."); + } + case "client" -> { + CrabClient client; + try { + client = new CrabClient(args[1], Integer.parseInt(args[2]), args.length == 4 ? args[3] : null); + } catch (NumberFormatException e) { + System.err.println("Port is not a number."); + return; + } + client.run(); + } + case "server" -> { + CrabServer server; + try { + server = new CrabServer(Integer.parseInt(args[1])); + } catch (NumberFormatException e) { + System.err.println("Port is not a number."); + return; + } + server.run(); + } + default -> { + System.err.println("Unknown argument"); + return; + } + } } private static void chooseMode(Scanner scanner) { @@ -28,4 +62,4 @@ public class Main { } } -} \ No newline at end of file +} diff --git a/src/main/java/net/pixtaded/crab/client/CrabClient.java b/src/main/java/net/pixtaded/crab/client/CrabClient.java index 76ee11e..52ed5db 100644 --- a/src/main/java/net/pixtaded/crab/client/CrabClient.java +++ b/src/main/java/net/pixtaded/crab/client/CrabClient.java @@ -17,15 +17,26 @@ public class CrabClient implements Crab { private PrintWriter out; private BufferedReader in; private int lastBufferLength = 0; + private String nickname; public CrabClient() { + this.nickname = ""; + } + public CrabClient(String serverAddress, int port, String nickname) { + this.serverAddress = serverAddress; + this.port = port; + if (nickname != null) + this.nickname = "<" + nickname + "> "; + else + this.nickname = ""; } @Override public void run() { try { - setup(); + if (this.serverAddress == null) + setup(); connect(); communicate(); } catch (IOException e) { @@ -54,6 +65,11 @@ public class CrabClient implements Crab { System.out.println("Enter a correct port number: "); } } + + System.out.print("Enter your nickname (leave empty for no nickname): "); + nickname = scanner.nextLine(); + if (nickname != "") + nickname = "<" + nickname + "> "; } private void connect() throws IOException { @@ -77,7 +93,7 @@ public class CrabClient implements Crab { break; } - if (!message.isEmpty()) sendPacket(MESSAGE, message); + if (!message.isEmpty()) sendPacket(MESSAGE, this.nickname + message); } } @@ -120,4 +136,4 @@ public class CrabClient implements Crab { sendPacket(LOGS_SIZE, ""); sendPacket(LOGS, ""); } -} \ No newline at end of file +} diff --git a/src/main/java/net/pixtaded/crab/server/CrabServer.java b/src/main/java/net/pixtaded/crab/server/CrabServer.java index ea6eab1..d5705a4 100644 --- a/src/main/java/net/pixtaded/crab/server/CrabServer.java +++ b/src/main/java/net/pixtaded/crab/server/CrabServer.java @@ -18,10 +18,16 @@ public class CrabServer implements Crab { this.db = new Database("data.db"); } + public CrabServer(int port) { + this.db = new Database("data.db"); + this.port = port; + } + @Override public void run() { try { - setup(); + if (this.port == 0) + setup(); listen(); } catch (IOException e) { if (!isStopped) System.err.println("Error starting server: " + e.getMessage());