Merge pull request 'Add cli arguments, nick support and add data.db to .gitignore.' (#1) from bedohswe/crab:master into master

Reviewed-on: #1
This commit is contained in:
pixtaded 2025-01-10 15:42:36 +00:00
commit 44d11f18d6
4 changed files with 67 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
data.db
.gradle .gradle
build/ build/
!gradle/wrapper/gradle-wrapper.jar !gradle/wrapper/gradle-wrapper.jar

View File

@ -7,9 +7,43 @@ import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 0) {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
chooseMode(scanner); chooseMode(scanner);
scanner.close(); scanner.close();
return;
}
switch (args[0]) {
case "help" -> {
System.out.println("crab help - print this message.");
System.out.println("crab client <ip> <port> [nick] - connect to a server.");
System.out.println("crab server <port> - 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) { private static void chooseMode(Scanner scanner) {

View File

@ -17,14 +17,25 @@ public class CrabClient implements Crab {
private PrintWriter out; private PrintWriter out;
private BufferedReader in; private BufferedReader in;
private int lastBufferLength = 0; private int lastBufferLength = 0;
private String nickname;
public CrabClient() { 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 @Override
public void run() { public void run() {
try { try {
if (this.serverAddress == null)
setup(); setup();
connect(); connect();
communicate(); communicate();
@ -54,6 +65,11 @@ public class CrabClient implements Crab {
System.out.println("Enter a correct port number: "); 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 { private void connect() throws IOException {
@ -77,7 +93,7 @@ public class CrabClient implements Crab {
break; break;
} }
if (!message.isEmpty()) sendPacket(MESSAGE, message); if (!message.isEmpty()) sendPacket(MESSAGE, this.nickname + message);
} }
} }

View File

@ -18,9 +18,15 @@ public class CrabServer implements Crab {
this.db = new Database("data.db"); this.db = new Database("data.db");
} }
public CrabServer(int port) {
this.db = new Database("data.db");
this.port = port;
}
@Override @Override
public void run() { public void run() {
try { try {
if (this.port == 0)
setup(); setup();
listen(); listen();
} catch (IOException e) { } catch (IOException e) {