Archived
1
0
Fork 0

Support of send message to a specific channel from CLI and command interpretation

This commit is contained in:
Ethanell 2022-01-19 08:50:06 +01:00
parent 62727bcb3c
commit 6e05784eb7
5 changed files with 57 additions and 8 deletions

View file

@ -6,6 +6,7 @@ import fr.univ.lyon1.common.Message;
import fr.univ.lyon1.common.command.Command;
import fr.univ.lyon1.common.command.CommandType;
import fr.univ.lyon1.common.exception.ChatException;
import fr.univ.lyon1.common.exception.NotInChannel;
import fr.univ.lyon1.common.exception.UnknownCommand;
import javax.net.SocketFactory;
@ -17,6 +18,8 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -28,7 +31,7 @@ public class Client {
protected final Socket socket;
protected final ObjectOutputStream out;
private ObjectInputStream in;
private List<Channel> channels = new ArrayList<>();
private final List<Channel> channels = new ArrayList<>();
protected boolean started = false;
@ -64,17 +67,44 @@ public class Client {
System.exit(0);
}
public String sendMessage(String content) {
private void send(Command cmd) {
try {
out.writeObject(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
out.writeObject(cmd);
out.flush();
} catch (IOException e) {
System.err.println("Fail to send message !");
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 {

View file

@ -1,5 +1,7 @@
package fr.univ.lyon1.client;
import fr.univ.lyon1.common.exception.ChatException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
@ -25,7 +27,16 @@ public class ClientSend implements Runnable {
if (m.equals("exit"))
break;
client.sendMessage(m);
try {
if (m.startsWith("/"))
client.sendCommand(m);
else if (m.startsWith("#"))
client.sendMessageChannel(m);
else
client.sendMessage(m);
} catch (ChatException e) {
e.printStackTrace();
}
}
}
}

View file

@ -8,9 +8,9 @@ public class Command implements Serializable {
private final CommandType type;
private final List<Object> args;
public Command(CommandType type, List<Object> args) {
public Command(CommandType type, List<?> args) {
this.type = type;
this.args = args;
this.args = (List<Object>) args;
}
public CommandType getType() {

View file

@ -6,4 +6,8 @@ public class NotInChannel extends ChatException {
public NotInChannel(Channel channel) {
super("Your not in channel "+channel);
}
public NotInChannel(String channel) {
super("Your not in channel "+channel);
}
}

View file

@ -6,4 +6,8 @@ public class UnknownCommand extends ChatException {
public UnknownCommand() {
super("Command unknown");
}
public UnknownCommand(String commandName) {
super("Command " + commandName + " unknown");
}
}