diff --git a/.idea/ StorBackEnd.iml b/.idea/StoreBackEnd.iml similarity index 75% rename from .idea/ StorBackEnd.iml rename to .idea/StoreBackEnd.iml index 5e764c4..eeb7bce 100644 --- a/.idea/ StorBackEnd.iml +++ b/.idea/StoreBackEnd.iml @@ -1,9 +1,8 @@ - + - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 2dd3b57..e9fb66c 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index 2807cd8..93747c3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,47 +1,76 @@ package main import ( - "StorBackEnd/pkg/config" - "StorBackEnd/pkg/network" - "StorBackEnd/pkg/protocol/managers" - "StorBackEnd/pkg/protocol/repository" - "StorBackEnd/pkg/protocol/rules/readers" - "StorBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/config" + "StoreBackEnd/pkg/network" + "StoreBackEnd/pkg/protocol/managers" + "StoreBackEnd/pkg/protocol/repository" + "StoreBackEnd/pkg/protocol/rules/readers" + "StoreBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/utils" "time" ) const ( - FILE_PATH = "resources/AppConfig.json" + FilePath = "resources/AppConfig.json" ) func main() { - println("StorBackEnd started !") + utils.NetworkLister() // TODO REMOVE + println("StoreBackEnd started !") // Loading App config - appConfig, err := config.Read(FILE_PATH) + appConfig, err := config.Read(FilePath) if err != nil { println("Impossible de charger la configuration du server : " + err.Error()) return } - println("Adresse multicast : " + appConfig.MulticastAddress) + println(" - Multicast Network Interface : " + appConfig.MulticastNetworkInterface) + println(" - Multicast Address : " + appConfig.MulticastAddress) + println(" - StoreBacked Domain : " + appConfig.Domain) protocolRepository := repository.CreateProtocolRepository() - // Création des Writers + /** + ===== Init all Write here ===== + */ + // Creation of the HelloRule helloRule := writers.CreateHelloRule("^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$") protocolRepository.AddWriter(&helloRule) - // Création des Readers + // Creation of the SendOkRule + sendOkRule := writers.CreateSendOkRule("^SEND_OK\r\n$") + protocolRepository.AddWriter(&sendOkRule) + + // Creation of the SendErrorRule + sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\r\n$") + protocolRepository.AddWriter(&sendErrorRule) + + /** + ===== Init all Reader here ===== + */ + // Creation of the EraseFileRule eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") protocolRepository.AddReader(&eraseFileRule) - multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository) + // 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 + multicast := network.CreateClientMulticast( + appConfig.MulticastNetworkInterface, appConfig.MulticastAddress, appConfig.Domain, + appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository) go multicast.Run() requestManager := managers.RequestManager{Repository: protocolRepository} server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager} - server.Run() + server.Run() // TODO : -> pourquoi ne pas partir dans un thread ici. + //reader := bufio.NewReader(os.Stdin) TODO ne pas oublier ici de mettre en place un point de sortie pour le programme. + //fmt.Print("Type Enter to quite: ") + //cmd, _ := reader.ReadString('\n') + //println(cmd) } diff --git a/go.mod b/go.mod index 6a92a91..7b67ed0 100644 --- a/go.mod +++ b/go.mod @@ -1 +1 @@ -module StorBackEnd +module StoreBackEnd diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 9b5e2e5..3777a54 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -2,15 +2,18 @@ package config // AppConfig Contient toute la configuration du server type AppConfig struct { - // multicastAddress Contient l'adresse multicast du FileFrontEnd + // MulticastNetworkInterface + MulticastNetworkInterface string `json:"multicastNetworkInterface"` + + // MulticastAddress Contient l'adresse multicast du FileFrontEnd MulticastAddress string `json:"multicastAddress"` - // multicastSecond Contient le nombre de seconde entre chaque annonce + // MulticastSecond Contient le nombre de seconde entre chaque annonce MulticastSecond int `json:"multicastSecond"` - // domain Domain du StorBackEnd + // Domain du StoreBackEnd Domain string `json:"domain"` - // unicastPort Contient le port unicast auquel le FileFrontEnd se connecte + // UnicastPort Contient le port unicast auquel le FileFrontEnd se connecte UnicastPort int `json:"unicastPort"` } diff --git a/pkg/config/JsonConfigReader.go b/pkg/config/JsonConfigReader.go index 1cd3e96..f063606 100644 --- a/pkg/config/JsonConfigReader.go +++ b/pkg/config/JsonConfigReader.go @@ -5,6 +5,8 @@ import ( "io/ioutil" ) +// Read Méthode permettant de lire le fichier de donfiguration +// return AppConfig func Read(filePath string) (*AppConfig, error) { config := AppConfig{} file, err := ioutil.ReadFile(filePath) @@ -16,6 +18,5 @@ func Read(filePath string) (*AppConfig, error) { if errJson != nil { return nil, errJson } - return &config, nil } diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 8a26121..1bda350 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -1,16 +1,17 @@ package network import ( - "StorBackEnd/pkg/protocol/repository" - "StorBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/protocol/repository" + "StoreBackEnd/pkg/protocol/rules/writers" "fmt" "net" "time" ) // CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast -func CreateClientMulticast(address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast { +func CreateClientMulticast(netInterface string, address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast { return ClientMulticast{ + netInter: netInterface, address: address, domain: domain, port: port, @@ -22,62 +23,102 @@ func CreateClientMulticast(address string, domain string, port int, second time. // ClientMulticast Cette structure représente une communication en multicast. // TODO : Prévoir une fermeture de la connection (con.Close()) type ClientMulticast struct { + // netInter Interface réseaux multicast + netInter string // address Adresse de multicast address string - - // address Domain de du StorBackEnd + // address Domain de du StoreBackEnd domain string - // port Port de connexion en unicast port int - // second Temps en seconde entre chaque ping second time.Duration - - // Repository de protocol permettant de + // repository de protocol permettant de repository *repository.ProtocolRepository } // Run Cette méthode démarre une commmunication multicast -func (cMult ClientMulticast) Run() { - addr, done := cMult.ResolveAddr() - if done { +func (client ClientMulticast) Run() { + // Resolve multicast addr + rAddr, failedRA := client.ResolveAddr() + if failedRA { return } - con, done2 := cMult.DialUdp(addr) - if done2 { + // Resolve interface addr + lAddr, failedRIA := ResolveInterfaceAddr(client.netInter) + if failedRIA { + println("finish") return } - cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, cMult.domain, fmt.Sprintf("%d", cMult.port)) + // Init UDP server flux + con, failedDU := client.DialUdp(lAddr, rAddr) + if failedDU { + 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 + ")") return } for { - con.Write([]byte(cmd)) - time.Sleep(time.Second * cMult.second) + _, _ = con.Write([]byte(cmd)) + time.Sleep(time.Second * client.second) } - } -// ResolveAddr Permet de résoude l'addresse -func (cMult ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) { - addr, errResUdp := net.ResolveUDPAddr("udp", cMult.address) - - if errResUdp != nil { - println(errResUdp.Error()) +// ResolveAddr Permet de résoude l'addresse multicast +func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) { + addr, err := net.ResolveUDPAddr("udp", client.address) + if err != nil { + println(err.Error()) return nil, true } - return addr, false } +// ResolveInterfaceAddr Resolves the network interface address. +func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { + //i, err := net.InterfaceByName(inter) + //addrs, err := i.Addrs() + //println(addrs[0]) + //addr, err := net.ResolveUDPAddr("udp", addrs[0].String()) + //if err != nil { + // println(err.Error()) + // return nil, true + //} + return nil, false + //var ipv4Addr net.IP + //ief, err := net.InterfaceByName(inter) + //if err != nil { + // println(err.Error()) + // return nil, true + //} + //addrs, err := ief.Addrs() + //if err != nil { + // println(err.Error()) + // return nil, true + //} + //for _, addr := range addrs { + // if ipv4Addr = addr.(*net.IPNet).IP; ipv4Addr != nil { + // break + // } + //} + //println(ipv4Addr.String()) + //addr, err := net.ResolveUDPAddr("udp", ipv4Addr.String()) + //if err != nil { + // println(err.Error()) + // return nil, true + //} + //return addr, false +} + // DialUdp Ouvre une connection UDP -func (cMult ClientMulticast) DialUdp(addr *net.UDPAddr) (*net.UDPConn, bool) { - con, errDial := net.DialUDP("udp", nil, addr) +func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { + con, errDial := net.DialUDP("udp", nil, rAddr) if errDial != nil { println(errDial.Error()) return nil, true diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index ecb24ed..8bfbe78 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -1,7 +1,7 @@ package network import ( - "StorBackEnd/pkg/protocol/managers" + "StoreBackEnd/pkg/protocol/managers" "bufio" "fmt" "net" @@ -14,7 +14,6 @@ type ServerUnicast struct { } func (server ServerUnicast) Run() { - listen, err := net.Listen(server.Network, fmt.Sprintf("0.0.0.0:%d", server.Port)) // "tcp", "0.0.0.0:58000" if err != nil { @@ -24,20 +23,22 @@ 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("[REQUEST] " + line) if err != nil { return } - - result := server.ReqManager.Execute(line) - con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n + 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 26b0ea4..ad6d0ed 100644 --- a/pkg/protocol/IProtocolReader.go +++ b/pkg/protocol/IProtocolReader.go @@ -1,12 +1,16 @@ 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(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/IProtocolWriter.go b/pkg/protocol/IProtocolWriter.go index adbd040..d12d960 100644 --- a/pkg/protocol/IProtocolWriter.go +++ b/pkg/protocol/IProtocolWriter.go @@ -1,10 +1,10 @@ package protocol -// IProtocolReader Représentation abstraite d'un protocol +// 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 vérifier la validité d'une donnée censée suivre les règles d'un protocol + // Execute Permet de créer une règle à envoyer Execute(argsData ...string) (string, bool) } diff --git a/pkg/protocol/RegexMatcher.go b/pkg/protocol/RegexMatcher.go index 517be7e..679cadb 100644 --- a/pkg/protocol/RegexMatcher.go +++ b/pkg/protocol/RegexMatcher.go @@ -8,7 +8,6 @@ func CreateRegexMatcher(pattern string) *RegexMatcher { if err != nil { return nil } - return &RegexMatcher{matcher: compile} } diff --git a/pkg/protocol/managers/RequestManager.go b/pkg/protocol/managers/RequestManager.go index 9c0b7d2..0ce0d9c 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,20 +1,28 @@ package managers -import "StorBackEnd/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 - return "Error occured while execute command" + return "Error occurred while execute command" } } diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 9e613c5..fc3341c 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -1,6 +1,9 @@ package repository -import "StorBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" + "bufio" +) func CreateProtocolRepository() *ProtocolRepository { return &ProtocolRepository{ @@ -17,7 +20,6 @@ type ProtocolRepository struct { // AddReader Permet d'ajouter une règle servant à lire une commande func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) { cmd := (*reader).GetCmd() - if cmd != "" && !repo.containsReader(cmd) { repo.protocolReaders[cmd] = reader } @@ -26,7 +28,6 @@ func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) { // AddWriter Permet d'ajouter une règle servant à construire une commande func (repo ProtocolRepository) AddWriter(writer *protocol.IProtocolWriter) { cmd := (*writer).GetCmd() - if cmd != "" && !repo.containsWriter(cmd) { repo.protocolWriters[cmd] = writer } @@ -35,25 +36,24 @@ 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(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 ?!) return (*reader).Execute(data) } } - - return "", false + return "", false, 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) { - protWriter, has := repo.protocolWriters[ruleName] + protocolWriter, has := repo.protocolWriters[ruleName] if has { println("OHOH") - return (*protWriter).Execute(data...) + return (*protocolWriter).Execute(data...) } else { return "", false } diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 0d75f14..1467aa6 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -1,9 +1,12 @@ package readers -import "StorBackEnd/pkg/protocol" +import ( + "StoreBackEnd/pkg/protocol" + "bufio" +) -// EraseFileRuleName Identifiant de cette règle -const EraseFileRuleName = "ffe_erasefile" +// EraseFileRulePrefix Identifiant de cette règle +const EraseFileRulePrefix = "ERASEFILE" // EraseFileRule Demande de suppression d'un fichier type EraseFileRule struct { @@ -17,7 +20,7 @@ type EraseFileRule struct { // CreateEraseFileRule Création d'une instance de EraseFileRule func CreateEraseFileRule(pattern string) protocol.IProtocolReader { return &EraseFileRule{ - Cmd: EraseFileRuleName, + Cmd: EraseFileRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), } } @@ -26,14 +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 new file mode 100644 index 0000000..ee0f96c --- /dev/null +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -0,0 +1,79 @@ +package readers + +import ( + "StoreBackEnd/pkg/protocol" + "StoreBackEnd/pkg/utils" + "bufio" + "strconv" +) + +// SendFileRulePrefix Rule command prefix +const SendFileRulePrefix = "ffe_sendfile" + +// SendFileRule Storage structure for the SendFileRule +type SendFileRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendFileRule Creating an instance of SendFileRule +func CreateSendFileRule(pattern string) protocol.IProtocolReader { + return &SendFileRule{ + cmd: SendFileRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendFileRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +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, _ := 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") + + // 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 + } else { + println("AHAHAHAHAHAHA") + return "SEND_ERROR\r", false, nil + } +} + +// Match make a match with the current Rule and check if it matches. +func (rule SendFileRule) Match(data string) bool { + return rule.matcher.Match(data) +} diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index e2d3cf6..e5b0b9f 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -1,29 +1,32 @@ package writers -import "StorBackEnd/pkg/protocol" +import "StoreBackEnd/pkg/protocol" -const HelloRuleName = "sbe_hello" +// HelloRulePrefix Rule command prefix +const HelloRulePrefix = "HELLO" +// HelloRule Storage structure for the HelloRule type HelloRule struct { - // Cmd Nom de la règle - Cmd string - - // matcher Permet d'extraire de éléments d'une chaine + // cmd Rule name + cmd string + // matcher Allow to make a regex match matcher *protocol.RegexMatcher } -// CreateHelloRule Création d'une instance de HelloRule +// CreateHelloRule Creating an instance of HelloRule func CreateHelloRule(pattern string) protocol.IProtocolWriter { return &HelloRule{ - Cmd: HelloRuleName, + cmd: HelloRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), } } +// GetCmd retrieve the command name. func (rule HelloRule) GetCmd() string { - return rule.Cmd + return rule.cmd } +// Execute the Rule with a string command. func (rule HelloRule) Execute(argsData ...string) (string, bool) { - return rule.matcher.Build("HELLO", argsData...) + return rule.matcher.Build(HelloRulePrefix, argsData...) } diff --git a/pkg/protocol/rules/writers/SendErrorRule.go b/pkg/protocol/rules/writers/SendErrorRule.go new file mode 100644 index 0000000..fe6c27b --- /dev/null +++ b/pkg/protocol/rules/writers/SendErrorRule.go @@ -0,0 +1,32 @@ +package writers + +import "StoreBackEnd/pkg/protocol" + +// SendErrorRulePrefix Rule command prefix +const SendErrorRulePrefix = "SEND_ERROR" + +// SendErrorRule Storage structure for the SendErrorRule +type SendErrorRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendErrorRule Creating an instance of SendErrorRule +func CreateSendErrorRule(pattern string) protocol.IProtocolWriter { + return &SendErrorRule{ + cmd: SendErrorRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendErrorRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +func (rule SendErrorRule) Execute(argsData ...string) (string, bool) { + return rule.matcher.Build(SendErrorRulePrefix, argsData...) +} diff --git a/pkg/protocol/rules/writers/SendOkRule.go b/pkg/protocol/rules/writers/SendOkRule.go new file mode 100644 index 0000000..2d1e850 --- /dev/null +++ b/pkg/protocol/rules/writers/SendOkRule.go @@ -0,0 +1,32 @@ +package writers + +import "StoreBackEnd/pkg/protocol" + +// SendOkRulePrefix Rule command prefix +const SendOkRulePrefix = "SEND_OK" + +// SendOkRule Storage structure for the SendOkRule +type SendOkRule struct { + // cmd Rule name + cmd string + // matcher Allow to make a regex match + matcher *protocol.RegexMatcher +} + +// CreateSendOkRule Creating an instance of SendOkRule +func CreateSendOkRule(pattern string) protocol.IProtocolWriter { + return &SendOkRule{ + cmd: SendOkRulePrefix, + matcher: protocol.CreateRegexMatcher(pattern), + } +} + +// GetCmd retrieve the command name. +func (rule SendOkRule) GetCmd() string { + return rule.cmd +} + +// Execute the Rule with a string command. +func (rule SendOkRule) Execute(argsData ...string) (string, bool) { + return rule.matcher.Build(SendOkRulePrefix, argsData...) +} diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go new file mode 100644 index 0000000..46b299b --- /dev/null +++ b/pkg/utils/FileReceiver.go @@ -0,0 +1,31 @@ +package utils + +import ( + "bufio" + "fmt" + "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 + } + + defer file.Close() + currentSize := 0 + buffer := make([]byte, 1024) + + for currentSize < fileSize { + length, err := reader.Read(buffer) + if err != nil { + return false + } + + currentSize += length + file.WriteAt(buffer, int64(currentSize)) + } + + return true +} diff --git a/pkg/utils/NetworkLister.go b/pkg/utils/NetworkLister.go new file mode 100644 index 0000000..3e0c0de --- /dev/null +++ b/pkg/utils/NetworkLister.go @@ -0,0 +1,24 @@ +package utils + +import ( + "fmt" + "net" +) + +func NetworkLister() { + // Retrieve Interfaces + inter, err := net.Interfaces() + + // Process errors + if err != nil { + println("[ERROR] An error occurred : " + err.Error()) + return + } + + // Display items + println("\n\nNetwork interface list :") + for i, val := range inter { + fmt.Printf("%d. %s\n", i, val.Name) + } + print("\n") +} diff --git a/resources/AppConfig.json b/resources/AppConfig.json index a25c31e..16552c9 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -1,6 +1,7 @@ { - "multicastAddress" : "226.66.66.1:42500", + "multicastNetworkInterface" : "Wi-Fi", + "multicastAddress" : "226.66.66.1:15502", "multicastSecond" : 10, - "domain" : "benjamin", + "domain" : "lightcontainerSB01", "unicastPort" : 58000 } \ No newline at end of file