From e9fafd3cd80d9b48c712cbb65d868ab6811d7f3c Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 8 Mar 2022 10:25:00 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=203r=C3=A8gles=20SendFileRule.go,?= =?UTF-8?q?=20SendOkRule.go,=20SendErrorRule.go=20et=20corrections=20de=20?= =?UTF-8?q?bugs=20mineurs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 30 ++++++++++-- pkg/network/ClientMulticast.go | 10 ++-- pkg/protocol/IProtocolWriter.go | 2 +- pkg/protocol/repository/ProtocolRepository.go | 6 +-- pkg/protocol/rules/readers/EraseFileRule.go | 6 +-- pkg/protocol/rules/readers/SendFileRule.go | 49 +++++++++++++++++++ pkg/protocol/rules/writers/HelloRule.go | 21 ++++---- pkg/protocol/rules/writers/SendErrorRule.go | 32 ++++++++++++ pkg/protocol/rules/writers/SendOkRule.go | 32 ++++++++++++ 9 files changed, 163 insertions(+), 25 deletions(-) create mode 100644 pkg/protocol/rules/readers/SendFileRule.go create mode 100644 pkg/protocol/rules/writers/SendErrorRule.go create mode 100644 pkg/protocol/rules/writers/SendOkRule.go diff --git a/cmd/main.go b/cmd/main.go index d312423..9735c98 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -29,19 +29,43 @@ func main() { protocolRepository := repository.CreateProtocolRepository() - // Création des Writers + /** + ===== Init all Write here ===== + */ + // Creation of the HelloRule helloRule := writers.CreateHelloRule("^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$") protocolRepository.AddWriter(&helloRule) - // Création des Readers + // Creation of the SendOkRule + sendOkRule := writers.CreateSendOkRule("^SEND_OK\n$") + protocolRepository.AddWriter(&sendOkRule) + + // Creation of the SendErrorRule + sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\n$") + protocolRepository.AddWriter(&sendErrorRule) + + /** + ===== Init all Reader here ===== + */ + // Creation of the EraseFileRule eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") protocolRepository.AddReader(&eraseFileRule) + // Creation of the SendFileRule + sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n$") + protocolRepository.AddReader(&sendFileRule) + + // Create a Multicast Client & run it multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository) go multicast.Run() requestManager := managers.RequestManager{Repository: protocolRepository} server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager} - server.Run() + server.Run() // TODO : -> pourquoi ne pas partir dans un thread ici. + + //reader := bufio.NewReader(os.Stdin) TODO ne pas oublier ici de mettre en place un point de sortie pour le programme. + //fmt.Print("Type Enter to quite: ") + //cmd, _ := reader.ReadString('\n') + //println(cmd) } diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 097f18f..2a39dd7 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -40,17 +40,17 @@ type ClientMulticast struct { // Run Cette méthode démarre une commmunication multicast func (client ClientMulticast) Run() { - addr, done := client.ResolveAddr() - if done { + addr, failedRA := client.ResolveAddr() + if failedRA { return } - con, done2 := client.DialUdp(addr) - if done2 { + con, failedDU := client.DialUdp(addr) + if failedDU { return } - cmd, correct := client.repository.ExecuteWriter(writers.HelloRuleName, client.domain, fmt.Sprintf("%d", client.port)) + cmd, correct := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port)) if !correct { println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")") return diff --git a/pkg/protocol/IProtocolWriter.go b/pkg/protocol/IProtocolWriter.go index 688f51d..d12d960 100644 --- a/pkg/protocol/IProtocolWriter.go +++ b/pkg/protocol/IProtocolWriter.go @@ -5,6 +5,6 @@ type IProtocolWriter interface { // GetCmd Permet de récupérer le nom de la commande GetCmd() string - // Execute Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol + // Execute Permet de créer une règle à envoyer Execute(argsData ...string) (string, bool) } diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 6470360..e47615a 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -17,7 +17,6 @@ type ProtocolRepository struct { // AddReader Permet d'ajouter une règle servant à lire une commande func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) { cmd := (*reader).GetCmd() - if cmd != "" && !repo.containsReader(cmd) { repo.protocolReaders[cmd] = reader } @@ -26,7 +25,6 @@ func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) { // AddWriter Permet d'ajouter une règle servant à construire une commande func (repo ProtocolRepository) AddWriter(writer *protocol.IProtocolWriter) { cmd := (*writer).GetCmd() - if cmd != "" && !repo.containsWriter(cmd) { repo.protocolWriters[cmd] = writer } @@ -49,10 +47,10 @@ func (repo ProtocolRepository) ExecuteReader(data string) (string, bool) { ExecuteReader Permet d'exécuter un Wrier et de récupérer la commande construite avec nos argument et un indicateur de réussite. */ func (repo ProtocolRepository) ExecuteWriter(ruleName string, data ...string) (string, bool) { - protWriter, has := repo.protocolWriters[ruleName] + protocolWriter, has := repo.protocolWriters[ruleName] if has { println("OHOH") - return (*protWriter).Execute(data...) + return (*protocolWriter).Execute(data...) } else { return "", false } diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 34bfbbf..5d68d47 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -2,8 +2,8 @@ package readers import "StoreBackEnd/pkg/protocol" -// EraseFileRuleName Identifiant de cette règle -const EraseFileRuleName = "ffe_erasefile" +// EraseFileRulePrefix Identifiant de cette règle +const EraseFileRulePrefix = "ERASEFILE" // EraseFileRule Demande de suppression d'un fichier type EraseFileRule struct { @@ -17,7 +17,7 @@ type EraseFileRule struct { // CreateEraseFileRule Création d'une instance de EraseFileRule func CreateEraseFileRule(pattern string) protocol.IProtocolReader { return &EraseFileRule{ - Cmd: EraseFileRuleName, + Cmd: EraseFileRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), } } diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go new file mode 100644 index 0000000..9cd6f93 --- /dev/null +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -0,0 +1,49 @@ +package readers + +import "StoreBackEnd/pkg/protocol" + +// SendFileRulePrefix Rule command prefix +const SendFileRulePrefix = "ffe_sendfile" + +// SendFileRule Storage structure for the SendFileRule +type SendFileRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendFileRule Creating an instance of SendFileRule +func CreateSendFileRule(pattern string) protocol.IProtocolReader { + return &SendFileRule{ + cmd: SendFileRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendFileRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +func (rule SendFileRule) Execute(data string) (string, bool) { + if rule.Match(data) { // TODO : cloture this command. + values := rule.matcher.Parse(data) + //callback := func() { + // + //} + //callback() + println(values[1], " File Name Hash") + println(values[2], " File Size") + println(values[3], " File Content Hash") + return "SEND_OK\r\n", true + } else { + return "jjdjd", false + } +} + +// Match make a match with the current Rule and check if it matches. +func (rule SendFileRule) Match(data string) bool { + return rule.matcher.Match(data) +} diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index e61f26b..e5b0b9f 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -2,28 +2,31 @@ package writers import "StoreBackEnd/pkg/protocol" -const HelloRuleName = "sbe_hello" +// HelloRulePrefix Rule command prefix +const HelloRulePrefix = "HELLO" +// HelloRule Storage structure for the HelloRule type HelloRule struct { - // Cmd Nom de la règle - Cmd string - - // matcher Permet d'extraire de éléments d'une chaine + // cmd Rule name + cmd string + // matcher Allow to make a regex match matcher *protocol.RegexMatcher } -// CreateHelloRule Création d'une instance de HelloRule +// CreateHelloRule Creating an instance of HelloRule func CreateHelloRule(pattern string) protocol.IProtocolWriter { return &HelloRule{ - Cmd: HelloRuleName, + cmd: HelloRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), } } +// GetCmd retrieve the command name. func (rule HelloRule) GetCmd() string { - return rule.Cmd + return rule.cmd } +// Execute the Rule with a string command. func (rule HelloRule) Execute(argsData ...string) (string, bool) { - return rule.matcher.Build("HELLO", argsData...) + return rule.matcher.Build(HelloRulePrefix, argsData...) } diff --git a/pkg/protocol/rules/writers/SendErrorRule.go b/pkg/protocol/rules/writers/SendErrorRule.go new file mode 100644 index 0000000..fe6c27b --- /dev/null +++ b/pkg/protocol/rules/writers/SendErrorRule.go @@ -0,0 +1,32 @@ +package writers + +import "StoreBackEnd/pkg/protocol" + +// SendErrorRulePrefix Rule command prefix +const SendErrorRulePrefix = "SEND_ERROR" + +// SendErrorRule Storage structure for the SendErrorRule +type SendErrorRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendErrorRule Creating an instance of SendErrorRule +func CreateSendErrorRule(pattern string) protocol.IProtocolWriter { + return &SendErrorRule{ + cmd: SendErrorRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendErrorRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +func (rule SendErrorRule) Execute(argsData ...string) (string, bool) { + return rule.matcher.Build(SendErrorRulePrefix, argsData...) +} diff --git a/pkg/protocol/rules/writers/SendOkRule.go b/pkg/protocol/rules/writers/SendOkRule.go new file mode 100644 index 0000000..2d1e850 --- /dev/null +++ b/pkg/protocol/rules/writers/SendOkRule.go @@ -0,0 +1,32 @@ +package writers + +import "StoreBackEnd/pkg/protocol" + +// SendOkRulePrefix Rule command prefix +const SendOkRulePrefix = "SEND_OK" + +// SendOkRule Storage structure for the SendOkRule +type SendOkRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendOkRule Creating an instance of SendOkRule +func CreateSendOkRule(pattern string) protocol.IProtocolWriter { + return &SendOkRule{ + cmd: SendOkRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendOkRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +func (rule SendOkRule) Execute(argsData ...string) (string, bool) { + return rule.matcher.Build(SendOkRulePrefix, argsData...) +}