Support of send message to a specific channel from CLI and command interpretation
This commit is contained in:
parent
62727bcb3c
commit
6e05784eb7
5 changed files with 57 additions and 8 deletions
|
@ -6,6 +6,7 @@ import fr.univ.lyon1.common.Message;
|
||||||
import fr.univ.lyon1.common.command.Command;
|
import fr.univ.lyon1.common.command.Command;
|
||||||
import fr.univ.lyon1.common.command.CommandType;
|
import fr.univ.lyon1.common.command.CommandType;
|
||||||
import fr.univ.lyon1.common.exception.ChatException;
|
import fr.univ.lyon1.common.exception.ChatException;
|
||||||
|
import fr.univ.lyon1.common.exception.NotInChannel;
|
||||||
import fr.univ.lyon1.common.exception.UnknownCommand;
|
import fr.univ.lyon1.common.exception.UnknownCommand;
|
||||||
|
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
@ -17,6 +18,8 @@ import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -28,7 +31,7 @@ public class Client {
|
||||||
protected final Socket socket;
|
protected final Socket socket;
|
||||||
protected final ObjectOutputStream out;
|
protected final ObjectOutputStream out;
|
||||||
private ObjectInputStream in;
|
private ObjectInputStream in;
|
||||||
private List<Channel> channels = new ArrayList<>();
|
private final List<Channel> channels = new ArrayList<>();
|
||||||
protected boolean started = false;
|
protected boolean started = false;
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,17 +67,44 @@ public class Client {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String sendMessage(String content) {
|
private void send(Command cmd) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
out.writeObject(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
|
out.writeObject(cmd);
|
||||||
out.flush();
|
out.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Fail to send message !");
|
System.err.println("Fail to send message !");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return content;
|
public void sendMessage(String content) {
|
||||||
|
send(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendCommand(String content) throws UnknownCommand {
|
||||||
|
List<String> args = Arrays.asList(content.split(" "));
|
||||||
|
String commandName = args.get(0).replace("/", "");
|
||||||
|
CommandType commandType;
|
||||||
|
|
||||||
|
try {
|
||||||
|
commandType = CommandType.valueOf(commandName);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new UnknownCommand(commandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
send(new Command(commandType, new ArrayList<>(args.subList(1, args.size()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessageChannel(String content) throws NotInChannel {
|
||||||
|
String[] args = content.split(" ");
|
||||||
|
String channelName = args[0].replace("#", "");
|
||||||
|
content = String.join(" ", Arrays.stream(args).toList().subList(1, args.length));
|
||||||
|
Channel channel = channels.stream().filter(c -> c.getName().equals(channelName)).findFirst().orElse(null);
|
||||||
|
|
||||||
|
if (channel == null)
|
||||||
|
throw new NotInChannel(channelName);
|
||||||
|
|
||||||
|
send(new Command(CommandType.message, List.of(new Message(content, channel))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void action(Object data) throws IOException {
|
public void action(Object data) throws IOException {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package fr.univ.lyon1.client;
|
package fr.univ.lyon1.client;
|
||||||
|
|
||||||
|
import fr.univ.lyon1.common.exception.ChatException;
|
||||||
|
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
@ -25,7 +27,16 @@ public class ClientSend implements Runnable {
|
||||||
if (m.equals("exit"))
|
if (m.equals("exit"))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (m.startsWith("/"))
|
||||||
|
client.sendCommand(m);
|
||||||
|
else if (m.startsWith("#"))
|
||||||
|
client.sendMessageChannel(m);
|
||||||
|
else
|
||||||
client.sendMessage(m);
|
client.sendMessage(m);
|
||||||
|
} catch (ChatException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ public class Command implements Serializable {
|
||||||
private final CommandType type;
|
private final CommandType type;
|
||||||
private final List<Object> args;
|
private final List<Object> args;
|
||||||
|
|
||||||
public Command(CommandType type, List<Object> args) {
|
public Command(CommandType type, List<?> args) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.args = args;
|
this.args = (List<Object>) args;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandType getType() {
|
public CommandType getType() {
|
||||||
|
|
|
@ -6,4 +6,8 @@ public class NotInChannel extends ChatException {
|
||||||
public NotInChannel(Channel channel) {
|
public NotInChannel(Channel channel) {
|
||||||
super("Your not in channel "+channel);
|
super("Your not in channel "+channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NotInChannel(String channel) {
|
||||||
|
super("Your not in channel "+channel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,8 @@ public class UnknownCommand extends ChatException {
|
||||||
public UnknownCommand() {
|
public UnknownCommand() {
|
||||||
super("Command unknown");
|
super("Command unknown");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UnknownCommand(String commandName) {
|
||||||
|
super("Command " + commandName + " unknown");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue