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:
commit
44d11f18d6
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
|
data.db
|
||||||
|
|
||||||
.gradle
|
.gradle
|
||||||
build/
|
build/
|
||||||
!gradle/wrapper/gradle-wrapper.jar
|
!gradle/wrapper/gradle-wrapper.jar
|
||||||
@ -39,4 +41,4 @@ bin/
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
### Mac OS ###
|
### Mac OS ###
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
@ -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) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
if (args.length == 0) {
|
||||||
chooseMode(scanner);
|
Scanner scanner = new Scanner(System.in);
|
||||||
scanner.close();
|
chooseMode(scanner);
|
||||||
|
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) {
|
||||||
@ -28,4 +62,4 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,15 +17,26 @@ 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 {
|
||||||
setup();
|
if (this.serverAddress == null)
|
||||||
|
setup();
|
||||||
connect();
|
connect();
|
||||||
communicate();
|
communicate();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,4 +136,4 @@ public class CrabClient implements Crab {
|
|||||||
sendPacket(LOGS_SIZE, "");
|
sendPacket(LOGS_SIZE, "");
|
||||||
sendPacket(LOGS, "");
|
sendPacket(LOGS, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,16 @@ 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 {
|
||||||
setup();
|
if (this.port == 0)
|
||||||
|
setup();
|
||||||
listen();
|
listen();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (!isStopped) System.err.println("Error starting server: " + e.getMessage());
|
if (!isStopped) System.err.println("Error starting server: " + e.getMessage());
|
||||||
|
Loading…
Reference in New Issue
Block a user