From 089b033fbeae583bc4a455f034d816704fb72d2e Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 12:22:38 +0100 Subject: [PATCH 1/7] =?UTF-8?q?Cr=C3=A9ation=20fichier=20json=20contenant?= =?UTF-8?q?=20la=20configurationdu=20serveur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/AppConfig.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 resources/AppConfig.json diff --git a/resources/AppConfig.json b/resources/AppConfig.json new file mode 100644 index 0000000..1b61acd --- /dev/null +++ b/resources/AppConfig.json @@ -0,0 +1,5 @@ +{ + "multicastAddress" : "226.66.66.1:42500", + "multicastSecond" : 10, + "unicastAddress" : "0.0.0.0:58000" +} \ No newline at end of file From a2b705ed1c3e36993120c9d63665f356862d3934 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 12:23:01 +0100 Subject: [PATCH 2/7] Ajout d'un TODO --- pkg/network/ServerUnicast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 7abee44..65b1105 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -36,7 +36,7 @@ func (server ServerUnicast) Run() { } result := server.ReqManager.Execute(line) - con.Write(append([]byte(result), '\n')) + con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n } } From 6553ebead033b71134e49c9ded39aff8e9690f28 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 12:23:24 +0100 Subject: [PATCH 3/7] Ajout des tag pour Json --- pkg/config/AppConfig.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 673bc5a..566e9bb 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -3,13 +3,13 @@ package config // AppConfig Contient toute la configuration du server type AppConfig struct { // multicastAddress Contient l'adresse multicast du FileFrontEnd (adresse:port) - multicastAddress string + multicastAddress string `json:"multicastAddress"` // multicastSecond Contient le nombre de seconde entre chaque annonce - multicastSecond int + multicastSecond int `json:"multicastSecond"` // unicastAddress Contient l'adresse unicast auquel le FileFrontEnd se connecte (adresse:port) - unicastAddress string + unicastAddress string `json:"unicastAddress"` } // MulticastAddress Accesseur pour multicastAddress From f6a700413707efdcc1eca92848335f121f02e1a1 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 13:05:33 +0100 Subject: [PATCH 4/7] =?UTF-8?q?Chargement=20du=20fichier=20de=20config=20f?= =?UTF-8?q?onctionnelle.=20Reste=20=C3=A0=20utiliser=20ses=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 11 +++++++++++ pkg/config/AppConfig.go | 21 +++------------------ pkg/config/JsonConfigReader.go | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 pkg/config/JsonConfigReader.go diff --git a/cmd/main.go b/cmd/main.go index ef8b308..0526c46 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "_StorBackEnd/pkg/config" "_StorBackEnd/pkg/network" "_StorBackEnd/pkg/protocol/managers" "_StorBackEnd/pkg/protocol/repository" @@ -9,6 +10,7 @@ import ( ) const ( + FILE_PATH = "resources/AppConfig.json" MULTICAST_ADDRESS = "226.66.66.1:42500" MULTICAST_SECOND = 10 // TODO : Changer en 30 secondes UNICAST_ADDRESS = "0.0.0.0:58000" @@ -17,6 +19,15 @@ const ( func main() { println("StorBackEnd started !") + // Loading App config + appConfig, err := config.Read(FILE_PATH) + if err != nil { + println("Impossible de charger la configuration du server : " + err.Error()) + return + } + + println("Adresse multicast : " + appConfig.MulticastAddress) + protocolRepository := repository.CreateProtocolRepository() // Création des Writers diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 566e9bb..0d04491 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -3,26 +3,11 @@ package config // AppConfig Contient toute la configuration du server type AppConfig struct { // multicastAddress Contient l'adresse multicast du FileFrontEnd (adresse:port) - multicastAddress string `json:"multicastAddress"` + MulticastAddress string `json:"multicastAddress"` // multicastSecond Contient le nombre de seconde entre chaque annonce - multicastSecond int `json:"multicastSecond"` + MulticastSecond int `json:"multicastSecond"` // unicastAddress Contient l'adresse unicast auquel le FileFrontEnd se connecte (adresse:port) - unicastAddress string `json:"unicastAddress"` -} - -// MulticastAddress Accesseur pour multicastAddress -func (a *AppConfig) MulticastAddress() string { - return a.multicastAddress -} - -// MulticastSecond Accesseur pour multicastSecond -func (a *AppConfig) MulticastSecond() int { - return a.multicastSecond -} - -// UnicastAddress Accesseur pour unicastAddress -func (a *AppConfig) UnicastAddress() string { - return a.unicastAddress + UnicastAddress string `json:"unicastAddress"` } diff --git a/pkg/config/JsonConfigReader.go b/pkg/config/JsonConfigReader.go new file mode 100644 index 0000000..1cd3e96 --- /dev/null +++ b/pkg/config/JsonConfigReader.go @@ -0,0 +1,21 @@ +package config + +import ( + "encoding/json" + "io/ioutil" +) + +func Read(filePath string) (*AppConfig, error) { + config := AppConfig{} + file, err := ioutil.ReadFile(filePath) + if err != nil { + return nil, err + } + + errJson := json.Unmarshal(file, &config) + if errJson != nil { + return nil, errJson + } + + return &config, nil +} From 0808ed493daf079ba63d27859211e38306d51b15 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 23 Feb 2022 13:07:48 +0100 Subject: [PATCH 5/7] Fix project : Suppression des _ --- cmd/main.go | 12 ++++++------ go.mod | 2 +- pkg/network/ClientMulticast.go | 4 ++-- pkg/network/ServerUnicast.go | 2 +- pkg/protocol/managers/RequestManager.go | 2 +- pkg/protocol/repository/ProtocolRepository.go | 2 +- pkg/protocol/rules/readers/EraseFileRule.go | 2 +- pkg/protocol/rules/writers/HelloRule.go | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 0526c46..0b9f69c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,12 +1,12 @@ 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" + "StorBackEnd/pkg/config" + "StorBackEnd/pkg/network" + "StorBackEnd/pkg/protocol/managers" + "StorBackEnd/pkg/protocol/repository" + "StorBackEnd/pkg/protocol/rules/readers" + "StorBackEnd/pkg/protocol/rules/writers" ) const ( diff --git a/go.mod b/go.mod index fd0c793..6a92a91 100644 --- a/go.mod +++ b/go.mod @@ -1 +1 @@ -module _StorBackEnd +module StorBackEnd diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index c136a3a..f902bdb 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -1,8 +1,8 @@ package network import ( - "_StorBackEnd/pkg/protocol/repository" - "_StorBackEnd/pkg/protocol/rules/writers" + "StorBackEnd/pkg/protocol/repository" + "StorBackEnd/pkg/protocol/rules/writers" "net" "time" ) diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 65b1105..079621d 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -1,7 +1,7 @@ package network import ( - "_StorBackEnd/pkg/protocol/managers" + "StorBackEnd/pkg/protocol/managers" "bufio" "fmt" "net" diff --git a/pkg/protocol/managers/RequestManager.go b/pkg/protocol/managers/RequestManager.go index 14176ec..9c0b7d2 100644 --- a/pkg/protocol/managers/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,6 +1,6 @@ package managers -import "_StorBackEnd/pkg/protocol/repository" +import "StorBackEnd/pkg/protocol/repository" type RequestManager struct { Repository *repository.ProtocolRepository diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 1901ece..9e613c5 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -1,6 +1,6 @@ package repository -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" func CreateProtocolRepository() *ProtocolRepository { return &ProtocolRepository{ diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index d3e3522..0d75f14 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -1,6 +1,6 @@ package readers -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" // EraseFileRuleName Identifiant de cette règle const EraseFileRuleName = "ffe_erasefile" diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index 5d420bb..e2d3cf6 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -1,6 +1,6 @@ package writers -import "_StorBackEnd/pkg/protocol" +import "StorBackEnd/pkg/protocol" const HelloRuleName = "sbe_hello" From dfd60968989e45254c6e6656d655d059fa4ab4eb Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 23 Feb 2022 14:08:41 +0100 Subject: [PATCH 6/7] Replace project constant by dynamic AppConfig. --- cmd/main.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 0b9f69c..dc6d83f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,13 +7,11 @@ import ( "StorBackEnd/pkg/protocol/repository" "StorBackEnd/pkg/protocol/rules/readers" "StorBackEnd/pkg/protocol/rules/writers" + "time" ) const ( - FILE_PATH = "resources/AppConfig.json" - MULTICAST_ADDRESS = "226.66.66.1:42500" - MULTICAST_SECOND = 10 // TODO : Changer en 30 secondes - UNICAST_ADDRESS = "0.0.0.0:58000" + FILE_PATH = "resources/AppConfig.json" ) func main() { @@ -38,12 +36,12 @@ func main() { eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") protocolRepository.AddReader(&eraseFileRule) - multicast := network.CreateClientMulticast(MULTICAST_ADDRESS, MULTICAST_SECOND, protocolRepository) + multicast := network.CreateClientMulticast(appConfig.MulticastAddress, time.Duration(appConfig.MulticastSecond), protocolRepository) go multicast.Run() requestManager := managers.RequestManager{Repository: protocolRepository} - server := network.ServerUnicast{Network: "tcp", Address: UNICAST_ADDRESS, ReqManager: &requestManager} + server := network.ServerUnicast{Network: "tcp", Address: appConfig.UnicastAddress, ReqManager: &requestManager} server.Run() } From c379c15932653be646368a179d24250e898df9ee Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sat, 26 Feb 2022 18:08:52 +0100 Subject: [PATCH 7/7] =?UTF-8?q?Mise=20=C3=A0=20jours=20du=20App=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 4 ++-- pkg/config/AppConfig.go | 9 ++++++--- pkg/network/ClientMulticast.go | 16 ++++++++++++---- pkg/network/ServerUnicast.go | 4 ++-- resources/AppConfig.json | 3 ++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index dc6d83f..2807cd8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,12 +36,12 @@ func main() { eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") protocolRepository.AddReader(&eraseFileRule) - multicast := network.CreateClientMulticast(appConfig.MulticastAddress, time.Duration(appConfig.MulticastSecond), protocolRepository) + multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository) go multicast.Run() requestManager := managers.RequestManager{Repository: protocolRepository} - server := network.ServerUnicast{Network: "tcp", Address: appConfig.UnicastAddress, ReqManager: &requestManager} + server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager} server.Run() } diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 0d04491..9b5e2e5 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -2,12 +2,15 @@ package config // AppConfig Contient toute la configuration du server type AppConfig struct { - // multicastAddress Contient l'adresse multicast du FileFrontEnd (adresse:port) + // multicastAddress Contient l'adresse multicast du FileFrontEnd MulticastAddress string `json:"multicastAddress"` // multicastSecond Contient le nombre de seconde entre chaque annonce MulticastSecond int `json:"multicastSecond"` - // unicastAddress Contient l'adresse unicast auquel le FileFrontEnd se connecte (adresse:port) - UnicastAddress string `json:"unicastAddress"` + // domain Domain du StorBackEnd + Domain string `json:"domain"` + + // unicastPort Contient le port unicast auquel le FileFrontEnd se connecte + UnicastPort int `json:"unicastPort"` } diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index f902bdb..8a26121 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -3,14 +3,17 @@ package network import ( "StorBackEnd/pkg/protocol/repository" "StorBackEnd/pkg/protocol/rules/writers" + "fmt" "net" "time" ) // CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast -func CreateClientMulticast(address string, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast { +func CreateClientMulticast(address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast { return ClientMulticast{ address: address, + domain: domain, + port: port, second: second, repository: repository, } @@ -19,9 +22,15 @@ func CreateClientMulticast(address string, second time.Duration, repository *rep // ClientMulticast Cette structure représente une communication en multicast. // TODO : Prévoir une fermeture de la connection (con.Close()) type ClientMulticast struct { - // address Adresse de multicast (address:port) + // address Adresse de multicast address string + // address Domain de du StorBackEnd + domain string + + // port Port de connexion en unicast + port int + // second Temps en seconde entre chaque ping second time.Duration @@ -41,8 +50,7 @@ func (cMult ClientMulticast) Run() { return } - // Fake test - cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, "benja", "5078") + cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, cMult.domain, fmt.Sprintf("%d", cMult.port)) if !correct { println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")") return diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 079621d..ecb24ed 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -9,13 +9,13 @@ import ( type ServerUnicast struct { Network string - Address string + Port int ReqManager *managers.RequestManager } func (server ServerUnicast) Run() { - listen, err := net.Listen(server.Network, server.Address) // "tcp", "0.0.0.0:58000" + listen, err := net.Listen(server.Network, fmt.Sprintf("0.0.0.0:%d", server.Port)) // "tcp", "0.0.0.0:58000" if err != nil { fmt.Printf("Can't start server : %s\n", err) diff --git a/resources/AppConfig.json b/resources/AppConfig.json index 1b61acd..a25c31e 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -1,5 +1,6 @@ { "multicastAddress" : "226.66.66.1:42500", "multicastSecond" : 10, - "unicastAddress" : "0.0.0.0:58000" + "domain" : "benjamin", + "unicastPort" : 58000 } \ No newline at end of file