From f0e98158444fe74b7ff88989429c50f96b9c6d00 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sat, 5 Mar 2022 16:01:22 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20possibilit=C3=A9=20d'=C3=A9crire=20un?= =?UTF-8?q?=20fichier=20sur=20le=20r=C3=A9seau?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 10 ++++------ .../domains/client/ClientHandler.java | 8 ++++---- .../lightcontainer/protocol/ProtocolWriter.java | 14 ++++++++++++-- .../writer/{SignError.java => SignErrorRule.java} | 4 ++-- .../rules/writer/{SignOk.java => SignOkRule.java} | 4 ++-- 5 files changed, 24 insertions(+), 16 deletions(-) rename app/src/main/java/lightcontainer/protocol/rules/writer/{SignError.java => SignErrorRule.java} (75%) rename app/src/main/java/lightcontainer/protocol/rules/writer/{SignOk.java => SignOkRule.java} (76%) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 07b9ed6..8de81fd 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -5,15 +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.FilelistRule; import lightcontainer.protocol.rules.reader.HelloRule; import lightcontainer.protocol.rules.reader.SavefileRule; import lightcontainer.protocol.rules.reader.SigninRule; -import lightcontainer.protocol.rules.writer.SignError; -import lightcontainer.protocol.rules.writer.SignOk; +import lightcontainer.protocol.rules.writer.SignErrorRule; +import lightcontainer.protocol.rules.writer.SignOkRule; import lightcontainer.repository.ClientHandlerRepository; import lightcontainer.repository.FileFrontEnd; import lightcontainer.repository.ProtocolRepositoryImpl; @@ -38,8 +36,8 @@ public class App { protocolRep.addReader(new FilelistRule()); protocolRep.addReader(new SavefileRule()); - protocolRep.addWriter(new SignOk()); - protocolRep.addWriter(new SignError()); + protocolRep.addWriter(new SignOkRule()); + protocolRep.addWriter(new SignErrorRule()); new UnicastServerListener(clientRep, protocolRep, UNICAST_PORT); new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index fa26a85..0b32715 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -4,8 +4,8 @@ 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 lightcontainer.protocol.rules.writer.SignErrorRule; +import lightcontainer.protocol.rules.writer.SignOkRule; import java.io.*; import java.net.Socket; @@ -114,13 +114,13 @@ public class ClientHandler implements Runnable, AutoCloseable { SigninRule.Result signinResult = (SigninRule.Result) ruleResult; if (signinResult.checkCredentials()) { this.login = signinResult.getLogin(); - writer.write(protocolRep.executeWriter(SignOk.NAME)); + writer.write(protocolRep.executeWriter(SignOkRule.NAME)); writer.flush(); return; } } catch (ClassCastException ignored) {} - writer.write(protocolRep.executeWriter(SignError.NAME)); // Envoie SignError car echec de la connection + writer.write(protocolRep.executeWriter(SignErrorRule.NAME)); // Envoie SignError car echec de la connection writer.flush(); this.close(); // Fermeture de la connection } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index ba857bd..80d6154 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -1,5 +1,7 @@ package lightcontainer.protocol; +import java.io.InputStream; +import java.io.OutputStream; import java.util.StringJoiner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -19,16 +21,17 @@ public abstract class ProtocolWriter { * Permet de récupérer le nom de la commande. * @return Nom de la commande. */ - public String getCmdName() { + public final String getCmdName() { return cmdName; } + /** * Permet de contruire une commande selon une règle établie. * @param data Les données à ajouter dans la commande; L'ordre défini leur position dans la commande * @return La commande construite */ - public String execute(String... data) { + public final String execute(String... data) { // Concatatène le nom de la commande avec les données (trim), avec un espace entre chaque String command; StringJoiner builder = new StringJoiner(" ", this.cmdName, "\r\n"); @@ -42,4 +45,11 @@ public abstract class ProtocolWriter { Matcher ruleMatcher = this.rulePattern.matcher(command); return ruleMatcher.matches() ? command : null; } + + /** + * Permet d'écrire un fichier sur le réseau. Cad envoyer le contenu d'un fichier sur le réseau. + * Redéfinissez cette méthode pour l'utiliser. + * @param writer Buffer à remplir qui sera envoyer via le réseau + */ + public void write(OutputStream writer) {} } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java similarity index 75% rename from app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java rename to app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java index 0daa349..0c35635 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java @@ -2,13 +2,13 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; -public class SignError extends ProtocolWriter { +public class SignErrorRule extends ProtocolWriter { private static final String PATTERN = "^SIGN_ERROR\r\n$"; public static final String NAME = "SIGN_ERROR"; - public SignError() { + public SignErrorRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java similarity index 76% rename from app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java rename to app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java index 848561c..44b3f3c 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java @@ -2,13 +2,13 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; -public class SignOk extends ProtocolWriter { +public class SignOkRule extends ProtocolWriter { private static final String PATTERN = "^SIGN_OK\r\n$"; public static final String NAME = "SIGN_OK"; - public SignOk() { + public SignOkRule() { super(NAME, PATTERN); } }