diff --git a/cmd/main.go b/cmd/main.go index 9735c98..9e76eee 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,8 +51,8 @@ func main() { 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$") + // 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$") protocolRepository.AddReader(&sendFileRule) // Create a Multicast Client & run it diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index f76b81d..bd9bccd 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -23,18 +23,21 @@ func (server ServerUnicast) Run() { // Attente connexion du FileFrontEnd con, err := listen.Accept() + print("connection ok") if err != nil { fmt.Printf("Error while accepting client : %s\n", err) 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("OK ") // TODO REMOVE + println(line) if err != nil { return } - - result := server.ReqManager.Execute(line) + result := server.ReqManager.Execute(line, reader) _, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n } } diff --git a/pkg/protocol/IProtocolReader.go b/pkg/protocol/IProtocolReader.go index 26b0ea4..b34727b 100644 --- a/pkg/protocol/IProtocolReader.go +++ b/pkg/protocol/IProtocolReader.go @@ -1,12 +1,14 @@ package protocol +import "bufio" + // IProtocolReader Représentation abstraite d'un protocol type IProtocolReader interface { // GetCmd Permet de récupérer le nom de la commande 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) + Execute(data string) (string, bool, func(r *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 6ee6044..946d728 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,17 +1,26 @@ package managers -import "StoreBackEnd/pkg/protocol/repository" +import ( + "StoreBackEnd/pkg/protocol/repository" + "bufio" +) type RequestManager struct { Repository *repository.ProtocolRepository } -func (receiver RequestManager) Execute(request string) string { +func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string { // On lis ce que l'on reçoit - result, executed := receiver.Repository.ExecuteReader(request) + result, executed, readCb := receiver.Repository.ExecuteReader(request) // On renvoie la réponse (Comment pour fichier ?) if executed { + if readCb != nil { + cbResult, _ := readCb(reader) + if cbResult != "" { + result = cbResult + } + } return result } else { // TODO : Renvoyer qu'une erreur est survenue diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index e47615a..27d5669 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -1,6 +1,9 @@ package repository -import "StoreBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" + "bufio" +) func CreateProtocolRepository() *ProtocolRepository { return &ProtocolRepository{ @@ -33,14 +36,14 @@ 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 (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(r *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 ?!) return (*reader).Execute(data) } } - return "", false + return "", false, nil } /* diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 5d68d47..1467aa6 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -1,6 +1,9 @@ package readers -import "StoreBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" + "bufio" +) // EraseFileRulePrefix Identifiant de cette règle const EraseFileRulePrefix = "ERASEFILE" @@ -26,13 +29,13 @@ func (rule EraseFileRule) GetCmd() string { return rule.Cmd } -func (rule EraseFileRule) Execute(data string) (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") - return "Parsing : Fichier avec le hash " + values[1] + " supprimé", true + return "Parsing : Fichier avec le hash " + values[1] + " supprimé", true, nil } else { - return "Parsing : EraseFileRule command incorrecte", false + return "Parsing : EraseFileRule command incorrecte", false, nil } } diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go index 9cd6f93..1cf2c20 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -1,6 +1,9 @@ package readers -import "StoreBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" + "bufio" +) // SendFileRulePrefix Rule command prefix const SendFileRulePrefix = "ffe_sendfile" @@ -27,19 +30,23 @@ func (rule SendFileRule) GetCmd() string { } // Execute the Rule with a string command. -func (rule SendFileRule) Execute(data string) (string, bool) { +func (rule SendFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) { if rule.Match(data) { // TODO : cloture this command. values := rule.matcher.Parse(data) - //callback := func() { - // - //} - //callback() + + // print received data println(values[1], " File Name Hash") println(values[2], " File Size") println(values[3], " File Content Hash") - return "SEND_OK\r\n", true + + // function callback + callback := func(r *bufio.Reader) (string, bool) { + + return "", false + } + return "SEND_OK\r\n", true, callback } else { - return "jjdjd", false + return "jjdjd", false, nil } } diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go new file mode 100644 index 0000000..d4b585b --- /dev/null +++ b/pkg/utils/FileReceiver.go @@ -0,0 +1 @@ +package utils diff --git a/resources/AppConfig.json b/resources/AppConfig.json index feb7042..6908b10 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -1,5 +1,5 @@ { - "multicastAddress" : "226.66.66.1:42500", + "multicastAddress" : "226.66.66.1:15502", "multicastSecond" : 10, "domain" : "lightcontainerSB01", "unicastPort" : 58000