diff --git a/.gitignore b/.gitignore
index fec019b..4d66140 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,3 @@
.idea/
/target/
-/connection.properties
+/server.properties
diff --git a/pom.xml b/pom.xml
index 59b3bd1..ff0ea2f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,6 +48,11 @@
annotations
22.0.0
+
+ org.mariadb.jdbc
+ mariadb-java-client
+ 2.7.1
+
diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java
index 19718cc..ec650a1 100644
--- a/src/fr/univ/lyon1/client/Client.java
+++ b/src/fr/univ/lyon1/client/Client.java
@@ -1,25 +1,57 @@
package fr.univ.lyon1.client;
+import fr.univ.lyon1.common.Channel;
+import fr.univ.lyon1.common.ChatSSL;
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.UnknownCommand;
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
public class Client {
private final int port;
private final String address;
+ private final String username;
+ private final String password;
protected final Socket socket;
protected final ObjectOutputStream out;
private ObjectInputStream in;
+ private List channels = new ArrayList<>();
protected boolean started = false;
- public Client(String address, int port) throws IOException, InterruptedException {
+
+ public Client(String address, int port, String username, String password) throws Exception {
this.address = address;
this.port = port;
- socket = new Socket(address, port);
+ this.username = username;
+ this.password = password;
+ socket = initSSL();
out = new ObjectOutputStream(socket.getOutputStream());
+ getIn();
+ }
+
+ private Socket initSSL() throws IOException {
+ SSLContext ctx = ChatSSL.getSSLContext();
+
+ SocketFactory factory = ctx.getSocketFactory();
+
+ Socket connection = factory.createSocket(address, port);
+ ((SSLSocket) connection).setEnabledProtocols(new String[] {ChatSSL.tlsVersion});
+ SSLParameters sslParams = new SSLParameters();
+ sslParams.setEndpointIdentificationAlgorithm("HTTPS");
+ ((SSLSocket) connection).setSSLParameters(sslParams);
+ return connection;
}
public void disconnectedServer() throws IOException {
@@ -32,10 +64,9 @@ public class Client {
}
public String sendMessage(String content) {
- Message msg = new Message(null, content);
try {
- out.writeObject(msg);
+ out.writeObject(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
out.flush();
} catch (IOException e) {
System.err.println("Fail to send message !");
@@ -45,15 +76,55 @@ public class Client {
return content;
}
- public Message messageReceived(Message msg) {
+ public void action(Object data) throws IOException {
+ if (data instanceof Command)
+ command((Command) data);
+ else if (data instanceof ChatException)
+ ((ChatException) data).printStackTrace();
+ else {
+ out.writeObject(new UnknownCommand());
+ out.flush();
+ }
+ }
+
+ private void command(Command cmd) throws IOException {
+ switch (cmd.getType()) {
+ case login -> commandLogin();
+ case message -> commandMessage(cmd);
+ case list -> commandList(cmd);
+ case join -> commandJoin(cmd);
+ }
+ }
+
+ private void commandLogin() throws IOException {
+ out.writeObject(new Command(CommandType.list, null));
+ out.flush();
+ out.writeObject(new Command(CommandType.join, List.of("general")));
+ out.flush();
+ }
+
+ protected void commandMessage(Command cmd) {
System.out.println();
- System.out.println(msg);
- return msg;
+ System.out.println(cmd.getArgs().get(0));
+ }
+
+ private void commandList(Command cmd) {
+ List