From 38a177aa51cc98d5682efb1c5137d5f9903aa652 Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 8 Mar 2022 17:44:10 +0100 Subject: [PATCH] =?UTF-8?q?Tentative=20de=20r=C3=A9cup=C3=A9ration=20de=20?= =?UTF-8?q?fichier=20(probl=C3=A8me:=20r=C3=A9cup=C3=A9ration=20bloquante)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 3 +- pkg/network/ServerUnicast.go | 9 +++--- pkg/protocol/IProtocolReader.go | 6 ++-- pkg/protocol/managers/RequestManager.go | 8 ++--- pkg/protocol/repository/ProtocolRepository.go | 4 +-- pkg/protocol/rules/readers/EraseFileRule.go | 4 +-- pkg/protocol/rules/readers/SendFileRule.go | 32 +++++++++++-------- 7 files changed, 34 insertions(+), 32 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 93747c3..993f11a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,7 +7,6 @@ import ( "StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/rules/readers" "StoreBackEnd/pkg/protocol/rules/writers" - "StoreBackEnd/pkg/utils" "time" ) @@ -16,7 +15,7 @@ const ( ) func main() { - utils.NetworkLister() // TODO REMOVE + //utils.NetworkLister() // TODO REMOVE println("StoreBackEnd started !") // Loading App config diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index bdb838f..7e42a76 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -30,14 +30,13 @@ func (server ServerUnicast) Run() { return } else { for { // TODO : Extraire cette partie de code - reader := bufio.NewReader(con) - line, err := reader.ReadString('\n') - println("AH " + line) + line, err := bufio.NewReader(con).ReadString('\n') + println("[REQUEST] " + line) if err != nil { return } - result := server.ReqManager.Execute(line, reader) - println("Réponse : ", result) + result := server.ReqManager.Execute(line, con) + println("[RESPONSE] : ", result) _, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n } } diff --git a/pkg/protocol/IProtocolReader.go b/pkg/protocol/IProtocolReader.go index b34727b..e63cd02 100644 --- a/pkg/protocol/IProtocolReader.go +++ b/pkg/protocol/IProtocolReader.go @@ -1,6 +1,8 @@ package protocol -import "bufio" +import ( + "net" +) // IProtocolReader Représentation abstraite d'un protocol type IProtocolReader interface { @@ -8,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(r *bufio.Reader) (string, bool)) + Execute(data string) (string, bool, func(r net.Conn) (string, bool)) // 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/managers/RequestManager.go b/pkg/protocol/managers/RequestManager.go index 77aa6f9..81b28cb 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -2,17 +2,17 @@ package managers import ( "StoreBackEnd/pkg/protocol/repository" - "bufio" + "net" ) type RequestManager struct { Repository *repository.ProtocolRepository } -func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string { +func (receiver RequestManager) Execute(request string, reader net.Conn) string { // On lis ce que l'on reçoit result, executed, readCb := receiver.Repository.ExecuteReader(request) - println("TESTTESTETTETT") + // On renvoie la réponse (Comment pour fichier ?) if executed { if readCb != nil { @@ -23,8 +23,6 @@ func (receiver RequestManager) Execute(request string, reader *bufio.Reader) str } return result } else { - println("HMMM") - // TODO : Renvoyer qu'une erreur est survenue return "Error occurred while execute command" } } diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 27d5669..168717a 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -2,7 +2,7 @@ package repository import ( "StoreBackEnd/pkg/protocol" - "bufio" + "net" ) func CreateProtocolRepository() *ProtocolRepository { @@ -36,7 +36,7 @@ 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(r *bufio.Reader) (string, bool)) { +func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(r net.Conn) (string, bool)) { for _, reader := range repo.protocolReaders { if (*reader).Match(data) { // Exécuter si match // Récupérer résultat à renvoyer (Donc donner ProtocolRepository aux reader ?!) diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 1467aa6..54bc455 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -2,7 +2,7 @@ package readers import ( "StoreBackEnd/pkg/protocol" - "bufio" + "net" ) // EraseFileRulePrefix Identifiant de cette règle @@ -29,7 +29,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) (string, bool, func(r net.Conn) (string, bool)) { if rule.Match(data) { values := rule.matcher.Parse(data) println(values[1], " est le hash du fichier à supprimer") diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go index 99c8f4d..abf4238 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -2,8 +2,10 @@ package readers import ( "StoreBackEnd/pkg/protocol" - "bufio" + "fmt" "io" + "net" + "os" ) // SendFileRulePrefix Rule command prefix @@ -31,29 +33,31 @@ func (rule SendFileRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) { +func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) { if rule.Match(data) { // TODO : cloture this command. values := rule.matcher.Parse(data) + // Values + fileName := values[1] + // fileSize := 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") // function callback - callback := func(r *bufio.Reader) (string, bool) { - //var fileContent []byte - // readedByte := -1 - /* - for readedByte == -1 || readedByte > 0 { - readedByte, _ = r.Read(fileContent) - println("SendFileRule Reading file ", " - Hey - ", string(readedByte)) - } + callback := func(con net.Conn) (string, bool) { + file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName)) - */ - var fileContent []byte - readedCount, err := io.ReadFull(r, fileContent) - println("Un test ici : ", readedCount, err) + _, err := io.Copy(file, con) + println("HEY1") + if err != nil { + println("Can't copy file") + return "SEND_ERROR\r", false + } + println("HEY2") return "SEND_OK\r", true } return "SEND_OK\r", true, callback