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 173bf4a..12dd29d 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/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 +}