Possibilité de se connecter (factice), tout en recevant un message de confirmation ou d'erreur.

This commit is contained in:
Benjamin Lejeune 2022-02-28 19:57:30 +01:00
parent 59ce081d8c
commit f0461537f6
7 changed files with 144 additions and 8 deletions

View File

@ -5,8 +5,13 @@ package lightcontainer;
import lightcontainer.domains.server.MulticastServerListener;
import lightcontainer.domains.server.UnicastServerListener;
import lightcontainer.interfaces.MulticastSPR;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.reader.HelloRule;
import lightcontainer.protocol.rules.reader.SigninRule;
import lightcontainer.protocol.rules.writer.SignError;
import lightcontainer.protocol.rules.writer.SignOk;
import lightcontainer.repository.ClientHandlerRepository;
import lightcontainer.repository.FileFrontEnd;
import lightcontainer.repository.ProtocolRepositoryImpl;
@ -29,6 +34,10 @@ public class App {
protocolRep.addReader(new HelloRule());
protocolRep.addReader(new SigninRule());
protocolRep.addWriter(new SignOk());
protocolRep.addWriter(new SignError());
new UnicastServerListener(clientRep, protocolRep, UNICAST_PORT);
new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT);
FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep);

View File

@ -3,11 +3,13 @@ package lightcontainer.domains.client;
import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.reader.SigninRule;
import lightcontainer.protocol.rules.writer.SignError;
import lightcontainer.protocol.rules.writer.SignOk;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
* ClientHandler
@ -30,6 +32,12 @@ public class ClientHandler implements Runnable, AutoCloseable {
private ProtocolRepository protocolRep;
private boolean client_run;
/**
* Login of client.
* State : NULL if not connected and Login string if connected
*/
private String login;
private BufferedReader reader;
private PrintWriter writer;
@ -82,15 +90,47 @@ public class ClientHandler implements Runnable, AutoCloseable {
ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(command + "\r\n");
if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) {
fileFrontEnd.newCommand(command, ruleResult.getResultCommand()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd
if (isConnected()) {
if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) {
fileFrontEnd.newCommand(command, ruleResult.getResultCommand()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd
} else {
writer.write(ruleResult.getResultCommand()); // Renvoye au client
}
} else {
writer.write(ruleResult.getResultCommand()); // Renvoye au client
authentificate(ruleResult);
}
} catch (IOException ignore) { }
}
}
private void authentificate(ProtocolReader.ProtocolResult ruleResult) {
try {
SigninRule.Result signinResult = (SigninRule.Result) ruleResult;
if (signinResult.checkCredentials()) {
this.login = signinResult.getLogin();
writer.write(protocolRep.executeWriter(SignOk.NAME));
writer.flush();
return;
}
} catch (ClassCastException castException) {}
writer.write(protocolRep.executeWriter(SignError.NAME)); // Envoie SignError car echec de la connection
writer.flush();
this.close(); // Fermeture de la connection
}
/**
* Permet de savoir si l'utilisateur s'est connecté (Avec login et mdp)
* @return
*/
private boolean isConnected() {
return login != null;
}
/**
* AutoClosable Function
* Close the Client thread and resources.

View File

@ -7,7 +7,7 @@ public interface ProtocolRepository {
<T extends ProtocolReader.ProtocolResult> T executeReader(String data);
String executeWriter(String... datas);
String executeWriter(String cmdName, String... datas);
void addReader(ProtocolReader reader);

View File

@ -0,0 +1,59 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.protocol.ProtocolReader;
public class SigninRule extends ProtocolReader {
private static final String PATTERN = "^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$";
// Index du domain dans le tableau de donnée
private static final int LOGIN = 0;
//Index du port dans le tableau de donnée
private static final int PASSWORD = 1;
public SigninRule() {
super(PATTERN);
}
public class Result extends ProtocolResult {
private final String login;
private final String password;
public Result(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
public boolean checkCredentials() {
return getLogin().equals("aa") && getPassword().equals("aaaaa");
}
}
@Override
protected SigninRule.Result onExecuted(String... data) {
SigninRule.Result result = new SigninRule.Result(data[LOGIN], data[PASSWORD]);
// TODO : Création d'une règle d'écriture SIGN_OK et SIGN_ERROR proprement
if (result.checkCredentials()) {
result.setResultCommand("SIGN_OK\r\n", ResultCmdReceiver.CLIENT);
} else {
result.setResultCommand("SIGN_ERROR\r\n", ResultCmdReceiver.CLIENT);
}
return result;
}
}

View File

@ -0,0 +1,14 @@
package lightcontainer.protocol.rules.writer;
import lightcontainer.protocol.ProtocolWriter;
public class SignError extends ProtocolWriter {
private static final String PATTERN = "^SIGN_ERROR\r\n$";
public static final String NAME = "SIGN_ERROR";
public SignError() {
super(NAME, PATTERN);
}
}

View File

@ -0,0 +1,14 @@
package lightcontainer.protocol.rules.writer;
import lightcontainer.protocol.ProtocolWriter;
public class SignOk extends ProtocolWriter {
private static final String PATTERN = "^SIGN_OK\r\n$";
public static final String NAME = "SIGN_OK";
public SignOk() {
super(NAME, PATTERN);
}
}

View File

@ -23,10 +23,10 @@ public class ProtocolRepositoryImpl implements ProtocolRepository {
}
@Override
public String executeWriter(String... data) {
public String executeWriter(String cmdName, String... data) {
for (ProtocolWriter writer : writers) {
String command = writer.execute(data);
if (command != null) {
String command;
if (cmdName.equals(writer.getCmdName()) && (command = writer.execute(data)) != null) {
return command;
}
}