diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 046be00..29d9fac 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -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 channels = new ArrayList<>(); + private final List 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 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 { diff --git a/src/fr/univ/lyon1/client/ClientSend.java b/src/fr/univ/lyon1/client/ClientSend.java index d3d82fe..8d36f80 100644 --- a/src/fr/univ/lyon1/client/ClientSend.java +++ b/src/fr/univ/lyon1/client/ClientSend.java @@ -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(); + } } } } diff --git a/src/fr/univ/lyon1/common/command/Command.java b/src/fr/univ/lyon1/common/command/Command.java index d3e5ea6..c3e1ac6 100644 --- a/src/fr/univ/lyon1/common/command/Command.java +++ b/src/fr/univ/lyon1/common/command/Command.java @@ -8,9 +8,9 @@ public class Command implements Serializable { private final CommandType type; private final List args; - public Command(CommandType type, List args) { + public Command(CommandType type, List args) { this.type = type; - this.args = args; + this.args = (List) args; } public CommandType getType() { diff --git a/src/fr/univ/lyon1/common/exception/NotInChannel.java b/src/fr/univ/lyon1/common/exception/NotInChannel.java index 5a2473f..eeb1baa 100644 --- a/src/fr/univ/lyon1/common/exception/NotInChannel.java +++ b/src/fr/univ/lyon1/common/exception/NotInChannel.java @@ -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); + } } diff --git a/src/fr/univ/lyon1/common/exception/UnknownCommand.java b/src/fr/univ/lyon1/common/exception/UnknownCommand.java index 32a7c29..029de44 100644 --- a/src/fr/univ/lyon1/common/exception/UnknownCommand.java +++ b/src/fr/univ/lyon1/common/exception/UnknownCommand.java @@ -6,4 +6,8 @@ public class UnknownCommand extends ChatException { public UnknownCommand() { super("Command unknown"); } + + public UnknownCommand(String commandName) { + super("Command " + commandName + " unknown"); + } }