From 47b91d96d1757fbff9a7901850c668288912c66c Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Wed, 23 Feb 2022 13:06:29 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Probl=C3=A8me=20imports=20r=C3=A9gl=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 10 +++++----- go.mod | 2 +- pkg/network/ClientMulticast.go | 4 ++-- pkg/network/ServerUnicast.go | 2 +- pkg/protocol/managers/RequestManager.go | 2 +- pkg/protocol/repository/ProtocolRepository.go | 2 +- pkg/protocol/rules/readers/EraseFileRule.go | 2 +- pkg/protocol/rules/writers/HelloRule.go | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ef8b308..ae12b33 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,11 +1,11 @@ package main import ( - "_StorBackEnd/pkg/network" - "_StorBackEnd/pkg/protocol/managers" - "_StorBackEnd/pkg/protocol/repository" - "_StorBackEnd/pkg/protocol/rules/readers" - "_StorBackEnd/pkg/protocol/rules/writers" + "StorBackEnd/pkg/network" + "StorBackEnd/pkg/protocol/managers" + "StorBackEnd/pkg/protocol/repository" + "StorBackEnd/pkg/protocol/rules/readers" + "StorBackEnd/pkg/protocol/rules/writers" ) const ( diff --git a/go.mod b/go.mod index fd0c793..6a92a91 100644 --- a/go.mod +++ b/go.mod @@ -1 +1 @@ -module _StorBackEnd +module StorBackEnd diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index c136a3a..f902bdb 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -1,8 +1,8 @@ package network import ( - "_StorBackEnd/pkg/protocol/repository" - "_StorBackEnd/pkg/protocol/rules/writers" + "StorBackEnd/pkg/protocol/repository" + "StorBackEnd/pkg/protocol/rules/writers" "net" "time" ) diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 7abee44..82e15b8 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -1,7 +1,7 @@ package network import ( - "_StorBackEnd/pkg/protocol/managers" + "StorBackEnd/pkg/protocol/managers" "bufio" "fmt" "net" diff --git a/pkg/protocol/managers/RequestManager.go b/pkg/protocol/managers/RequestManager.go index 14176ec..9c0b7d2 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,6 +1,6 @@ package managers -import "_StorBackEnd/pkg/protocol/repository" +import "StorBackEnd/pkg/protocol/repository" type RequestManager struct { Repository *repository.ProtocolRepository diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 1901ece..9e613c5 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -1,6 +1,6 @@ package repository -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" func CreateProtocolRepository() *ProtocolRepository { return &ProtocolRepository{ diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index d3e3522..0d75f14 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -1,6 +1,6 @@ package readers -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" // EraseFileRuleName Identifiant de cette règle const EraseFileRuleName = "ffe_erasefile" diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index 5d420bb..e2d3cf6 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -1,6 +1,6 @@ package writers -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" const HelloRuleName = "sbe_hello" From f75948c3bba1687620597dbfc7f7b2d2d121b84b Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 12 Mar 2022 15:57:27 +0100 Subject: [PATCH 2/2] =?UTF-8?q?-=20Changement=20des=20ent=C3=AAtes=20dans?= =?UTF-8?q?=20le=20projets.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 6 +- pkg/network/ClientMulticast.go | 8 +-- pkg/network/ServerUnicast.go | 19 +++++- pkg/protocol/IProtocolReader.go | 2 +- pkg/protocol/IProtocolWriter.go | 12 +++- pkg/protocol/managers/RequestManager.go | 17 +++--- pkg/protocol/repository/ProtocolRepository.go | 9 ++- pkg/protocol/rules/readers/EraseFileRule.go | 10 ++- pkg/protocol/rules/readers/SendFileRule.go | 61 +++++++++---------- pkg/protocol/rules/writers/HelloRule.go | 8 ++- pkg/protocol/rules/writers/SendErrorRule.go | 19 +++++- pkg/protocol/rules/writers/SendOkRule.go | 12 +++- pkg/utils/FileReceiver.go | 3 +- pkg/utils/FileSender.go | 10 +++ 14 files changed, 124 insertions(+), 72 deletions(-) create mode 100644 pkg/utils/FileSender.go diff --git a/cmd/main.go b/cmd/main.go index 93747c3..b23fe07 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,11 +51,11 @@ func main() { ===== Init all Reader here ===== */ // Creation of the EraseFileRule - eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") - protocolRepository.AddReader(&eraseFileRule) + // eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") + // protocolRepository.AddReader(&eraseFileRule) // Creation of the SendFileRule // TODO reset to 50,200 - sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$") + sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository) protocolRepository.AddReader(&sendFileRule) // Create a Multicast Client & run it diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 1bda350..ca192d9 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -58,14 +58,14 @@ func (client ClientMulticast) Run() { return } - cmd, correct := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port)) - if !correct { - println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")") + writerResult := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port)) + if writerResult == nil { + println("[ClientMulticast] Hello rule isn't correct") return } for { - _, _ = con.Write([]byte(cmd)) + _, _ = con.Write([]byte(writerResult.Cmd)) time.Sleep(time.Second * client.second) } } diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 8bfbe78..cb91f16 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -36,9 +36,22 @@ func (server ServerUnicast) Run() { if err != nil { return } - result := server.ReqManager.Execute(line, reader) - println("[RESPONSE] : ", result) - _, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n + response := server.ReqManager.Execute(line, reader) + + if response == nil { + break + } else { + println("[RESPONSE] : ", response.Cmd) + _, _ = con.Write([]byte(response.Cmd)) // TODO : ATTENTION laisser les \n + if response.Write != nil { + response.Write(bufio.NewWriter(con)) + } + } + } + + con.Close() } + + listen.Close() } diff --git a/pkg/protocol/IProtocolReader.go b/pkg/protocol/IProtocolReader.go index ad6d0ed..fa4ad87 100644 --- a/pkg/protocol/IProtocolReader.go +++ b/pkg/protocol/IProtocolReader.go @@ -10,7 +10,7 @@ type IProtocolReader interface { GetCmd() string // Execute Permet d'exécuter l'action implémentée par une règle. Retourne le message (rule) de retour et bool pour savoir si tout s'est bien passé ou non - Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) + Execute(data string) (*ProtocolWriterResult, func(reader *bufio.Reader) *ProtocolWriterResult) // Match Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol Match(data string) bool diff --git a/pkg/protocol/IProtocolWriter.go b/pkg/protocol/IProtocolWriter.go index d12d960..bd9748c 100644 --- a/pkg/protocol/IProtocolWriter.go +++ b/pkg/protocol/IProtocolWriter.go @@ -1,10 +1,20 @@ package protocol +import "bufio" + // IProtocolWriter Représentation abstraite d'un protocol type IProtocolWriter interface { // GetCmd Permet de récupérer le nom de la commande GetCmd() string // Execute Permet de créer une règle à envoyer - Execute(argsData ...string) (string, bool) + Execute(argsData ...string) *ProtocolWriterResult +} + +type ProtocolWriterResult struct { + // Cmd Commande générée par le ProtocolWriter + Cmd string + + // Write Fonction appelée + Write func(writer *bufio.Writer) } diff --git a/pkg/protocol/managers/RequestManager.go b/pkg/protocol/managers/RequestManager.go index 0ce0d9c..a5feec5 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,6 +1,7 @@ package managers import ( + "StoreBackEnd/pkg/protocol" "StoreBackEnd/pkg/protocol/repository" "bufio" ) @@ -9,20 +10,20 @@ type RequestManager struct { Repository *repository.ProtocolRepository } -func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string { +func (receiver RequestManager) Execute(request string, reader *bufio.Reader) *protocol.ProtocolWriterResult { // On lis ce que l'on reçoit - result, executed, readCb := receiver.Repository.ExecuteReader(request) + writeCmd, readCb := receiver.Repository.ExecuteReader(request) // On renvoie la réponse (Comment pour fichier ?) - if executed { + if writeCmd != nil { if readCb != nil { - cbResult, _ := readCb(reader) - if cbResult != "" { - result = cbResult + cbResult := readCb(reader) + if cbResult != nil { + writeCmd = cbResult } } - return result + return writeCmd } else { - return "Error occurred while execute command" + return nil } } diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index fc3341c..032c37b 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -36,26 +36,25 @@ func (repo ProtocolRepository) AddWriter(writer *protocol.IProtocolWriter) { /* ExecuteReader Permet d'exécuter un Reader et de récupérer la commande à renvoyer. La deuxième valeur de retour permet de savoir si une commande a put être exécuté */ -func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) { +func (repo ProtocolRepository) ExecuteReader(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) { for _, reader := range repo.protocolReaders { if (*reader).Match(data) { // Exécuter si match // Récupérer résultat à renvoyer (Donc donner ProtocolRepository aux reader ?!) return (*reader).Execute(data) } } - return "", false, nil + return nil, nil } /* 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) { +func (repo ProtocolRepository) ExecuteWriter(ruleName string, data ...string) *protocol.ProtocolWriterResult { protocolWriter, has := repo.protocolWriters[ruleName] if has { - println("OHOH") return (*protocolWriter).Execute(data...) } else { - return "", false + return nil } } diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 1467aa6..e2236fd 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -1,10 +1,6 @@ package readers -import ( - "StoreBackEnd/pkg/protocol" - "bufio" -) - +/* // EraseFileRulePrefix Identifiant de cette règle const EraseFileRulePrefix = "ERASEFILE" @@ -29,7 +25,7 @@ func (rule EraseFileRule) GetCmd() string { return rule.Cmd } -func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) { +func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, bool, func(reader *bufio.Reader) (*protocol.ProtocolWriterResult, bool)) { if rule.Match(data) { values := rule.matcher.Parse(data) println(values[1], " est le hash du fichier à supprimer") @@ -42,3 +38,5 @@ func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Read func (rule EraseFileRule) Match(data string) bool { return rule.matcher.Match(data) } + +*/ diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go index ee0f96c..56f2dd2 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -2,6 +2,8 @@ package readers import ( "StoreBackEnd/pkg/protocol" + "StoreBackEnd/pkg/protocol/repository" + "StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/utils" "bufio" "strconv" @@ -16,13 +18,16 @@ type SendFileRule struct { cmd string // matcher Allow to make a regex match matcher *protocol.RegexMatcher + // protocolRepo Instance de ProtocolRepository + protocolRepo *repository.ProtocolRepository } // CreateSendFileRule Creating an instance of SendFileRule -func CreateSendFileRule(pattern string) protocol.IProtocolReader { +func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader { return &SendFileRule{ - cmd: SendFileRulePrefix, - matcher: protocol.CreateRegexMatcher(pattern), + cmd: SendFileRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + protocolRepo: protocolRepo, } } @@ -32,44 +37,36 @@ func (rule SendFileRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) { +func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) { + println(0) if rule.Match(data) { // TODO : cloture this command. + println("OK") values := rule.matcher.Parse(data) // Values fileName := values[1] fileSize, _ := strconv.Atoi(values[2]) // fileContentHash := values[3] - - // print received data - println(values[1], " File Name Hash") - println(values[2], " File Size") - println(values[3], " File Content Hash") - + println(1) // function callback - callback := func(reader *bufio.Reader) (string, bool) { - hasReceive := utils.ReceiveFile(fileName, fileSize, reader) - println("HEY1") - /* - file, _ := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) - _, err := io.Copy(file, reader) - println("HEY1") - if err != nil { - println("Can't copy file") - return "SEND_ERROR\r", false - } - - */ - if !hasReceive { - return "SEND_ERROR\r", false - } - println("HEY2") - return "SEND_OK\r", true - } - return "SEND_OK\r", true, callback + callback := rule.onRead(fileName, fileSize) + println(2) + return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback } else { - println("AHAHAHAHAHAHA") - return "SEND_ERROR\r", false, nil + return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil + } +} + +func (rule SendFileRule) onRead(fileName string, fileSize int) func(reader *bufio.Reader) *protocol.ProtocolWriterResult { + + return func(reader *bufio.Reader) *protocol.ProtocolWriterResult { + hasReceive := utils.ReceiveFile(fileName, fileSize, reader) + println(3) + if !hasReceive { + return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix) + } else { + return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix) + } } } diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index e5b0b9f..cc9335a 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -27,6 +27,10 @@ func (rule HelloRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule HelloRule) Execute(argsData ...string) (string, bool) { - return rule.matcher.Build(HelloRulePrefix, argsData...) +func (rule HelloRule) Execute(argsData ...string) *protocol.ProtocolWriterResult { + cmd, _ := rule.matcher.Build(HelloRulePrefix, argsData...) + + return &protocol.ProtocolWriterResult{ + Cmd: cmd, + } } diff --git a/pkg/protocol/rules/writers/SendErrorRule.go b/pkg/protocol/rules/writers/SendErrorRule.go index fe6c27b..f3992b6 100644 --- a/pkg/protocol/rules/writers/SendErrorRule.go +++ b/pkg/protocol/rules/writers/SendErrorRule.go @@ -1,6 +1,8 @@ package writers -import "StoreBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" +) // SendErrorRulePrefix Rule command prefix const SendErrorRulePrefix = "SEND_ERROR" @@ -27,6 +29,17 @@ func (rule SendErrorRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendErrorRule) Execute(argsData ...string) (string, bool) { - return rule.matcher.Build(SendErrorRulePrefix, argsData...) +func (rule SendErrorRule) Execute(argsData ...string) *protocol.ProtocolWriterResult { + buildedCmd, _ := rule.matcher.Build(SendErrorRulePrefix, argsData...) + + return &protocol.ProtocolWriterResult{ + Cmd: buildedCmd, + } } + +/* +func onWrite(writer *bufio.Writer) { + println("Ecriture du fichier sur le réseau") +} + +*/ diff --git a/pkg/protocol/rules/writers/SendOkRule.go b/pkg/protocol/rules/writers/SendOkRule.go index 2d1e850..6d4d653 100644 --- a/pkg/protocol/rules/writers/SendOkRule.go +++ b/pkg/protocol/rules/writers/SendOkRule.go @@ -1,6 +1,8 @@ package writers -import "StoreBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" +) // SendOkRulePrefix Rule command prefix const SendOkRulePrefix = "SEND_OK" @@ -27,6 +29,10 @@ func (rule SendOkRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendOkRule) Execute(argsData ...string) (string, bool) { - return rule.matcher.Build(SendOkRulePrefix, argsData...) +func (rule SendOkRule) Execute(argsData ...string) *protocol.ProtocolWriterResult { + buildedCmd, _ := rule.matcher.Build(SendOkRulePrefix, argsData...) + + return &protocol.ProtocolWriterResult{ + Cmd: buildedCmd, + } } diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index 46b299b..6cb2994 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -8,7 +8,7 @@ import ( // ReceiveFile Permet de récupérer un fichier sur un reader func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { - file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) + file, fileErr := os.Create(fmt.Sprintf("C:\\Users\\ledou\\Documents\\sbe\\%s", fileName)) if fileErr != nil { return false } @@ -18,6 +18,7 @@ func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { buffer := make([]byte, 1024) for currentSize < fileSize { + println("LIS") length, err := reader.Read(buffer) if err != nil { return false diff --git a/pkg/utils/FileSender.go b/pkg/utils/FileSender.go new file mode 100644 index 0000000..e7bbd29 --- /dev/null +++ b/pkg/utils/FileSender.go @@ -0,0 +1,10 @@ +package utils + +import ( + "bufio" +) + +func SendFile(fileName string, fileSize int, writer *bufio.Writer) bool { + + return false +}