diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 7e42a76..8bfbe78 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -30,12 +30,13 @@ func (server ServerUnicast) Run() { return } else { for { // TODO : Extraire cette partie de code - line, err := bufio.NewReader(con).ReadString('\n') + reader := bufio.NewReader(con) + line, err := reader.ReadString('\n') println("[REQUEST] " + line) if err != nil { return } - result := server.ReqManager.Execute(line, con) + result := server.ReqManager.Execute(line, reader) 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 e63cd02..ad6d0ed 100644 --- a/pkg/protocol/IProtocolReader.go +++ b/pkg/protocol/IProtocolReader.go @@ -1,7 +1,7 @@ package protocol import ( - "net" + "bufio" ) // IProtocolReader Représentation abstraite d'un protocol @@ -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(r net.Conn) (string, bool)) + Execute(data string) (string, bool, func(reader *bufio.Reader) (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 81b28cb..0ce0d9c 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -2,14 +2,14 @@ package managers import ( "StoreBackEnd/pkg/protocol/repository" - "net" + "bufio" ) type RequestManager struct { Repository *repository.ProtocolRepository } -func (receiver RequestManager) Execute(request string, reader net.Conn) string { +func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string { // On lis ce que l'on reçoit result, executed, readCb := receiver.Repository.ExecuteReader(request) diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 168717a..fc3341c 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -2,7 +2,7 @@ package repository import ( "StoreBackEnd/pkg/protocol" - "net" + "bufio" ) 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 net.Conn) (string, bool)) { +func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(reader *bufio.Reader) (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 54bc455..1467aa6 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" - "net" + "bufio" ) // 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 net.Conn) (string, bool)) { +func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (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 abf4238..ee0f96c 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -2,10 +2,9 @@ package readers import ( "StoreBackEnd/pkg/protocol" - "fmt" - "io" - "net" - "os" + "StoreBackEnd/pkg/utils" + "bufio" + "strconv" ) // SendFileRulePrefix Rule command prefix @@ -33,13 +32,13 @@ func (rule SendFileRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) { +func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) { if rule.Match(data) { // TODO : cloture this command. values := rule.matcher.Parse(data) // Values fileName := values[1] - // fileSize := values[2] + fileSize, _ := strconv.Atoi(values[2]) // fileContentHash := values[3] // print received data @@ -48,13 +47,20 @@ func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (s println(values[3], " File Content Hash") // function callback - callback := func(con net.Conn) (string, bool) { - file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName)) - - _, err := io.Copy(file, con) + callback := func(reader *bufio.Reader) (string, bool) { + hasReceive := utils.ReceiveFile(fileName, fileSize, reader) println("HEY1") - if err != nil { - println("Can't copy file") + /* + 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") diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index d4b585b..292cd5f 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -1 +1,25 @@ package utils + +import ( + "bufio" + "fmt" + "io" + "os" +) + +// 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)) + if fileErr != nil { + return false + } + + println(1) + defer file.Close() + _, err := io.CopyN(file, reader, int64(fileSize)) + println(2) + if err != nil { + return false + } + return true +}