diff --git a/cmd/main.go b/cmd/main.go index e48db51..f5ff706 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,8 +2,9 @@ package main import ( "_StorBackEnd/pkg/network" - "_StorBackEnd/pkg/protocol" + "_StorBackEnd/pkg/protocol/managers" "_StorBackEnd/pkg/protocol/repository" + "_StorBackEnd/pkg/protocol/rules/writers" ) const ( @@ -14,12 +15,15 @@ const ( func main() { println("StorBackEnd started !") + protocolRepository := repository.CreateProtocolRepository() + helloRule := writers.CreateHelloRule("") + protocolRepository.AddWriter(&helloRule) multicast := network.CreateClientMulticast(MULTICAST_ADDRESS, MULTICAST_SECOND, protocolRepository) go multicast.Run() - requestManager := protocol.RequestManager{Repository: protocolRepository} + requestManager := managers.RequestManager{Repository: protocolRepository} server := network.ServerUnicast{Network: "tcp", Address: UNICAST_ADDRESS, ReqManager: &requestManager} server.Run() diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 23832b9..b7a3c9d 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -2,6 +2,7 @@ package network import ( "_StorBackEnd/pkg/protocol/repository" + "_StorBackEnd/pkg/protocol/rules/writers" "net" "time" ) @@ -40,8 +41,15 @@ func (cMult ClientMulticast) Run() { return } + // Fake test + cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, "benja.be", "5078") + if !correct { + println("Hello rule isn't correct") + return + } + for { - con.Write([]byte("Hello")) + con.Write([]byte(cmd)) time.Sleep(time.Second * cMult.second) } diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index 1addabb..7abee44 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -1,7 +1,7 @@ package network import ( - "_StorBackEnd/pkg/protocol" + "_StorBackEnd/pkg/protocol/managers" "bufio" "fmt" "net" @@ -10,7 +10,7 @@ import ( type ServerUnicast struct { Network string Address string - ReqManager *protocol.RequestManager + ReqManager *managers.RequestManager } func (server ServerUnicast) Run() { diff --git a/pkg/protocol/IProtocolWriter.go b/pkg/protocol/IProtocolWriter.go index 5354e41..adbd040 100644 --- a/pkg/protocol/IProtocolWriter.go +++ b/pkg/protocol/IProtocolWriter.go @@ -6,5 +6,5 @@ type IProtocolWriter interface { GetCmd() string // Execute Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol - Execute(argsData []string) + Execute(argsData ...string) (string, bool) } diff --git a/pkg/protocol/RequestManager.go b/pkg/protocol/managers/RequestManager.go similarity index 96% rename from pkg/protocol/RequestManager.go rename to pkg/protocol/managers/RequestManager.go index 9e47fec..14176ec 100644 --- a/pkg/protocol/RequestManager.go +++ b/pkg/protocol/managers/RequestManager.go @@ -1,4 +1,4 @@ -package protocol +package managers import "_StorBackEnd/pkg/protocol/repository" diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 0e12c22..14202b5 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -1,8 +1,6 @@ package repository -import ( - "_StorBackEnd/pkg/protocol" -) +import "_StorBackEnd/pkg/protocol" func CreateProtocolRepository() *ProtocolRepository { return &ProtocolRepository{ @@ -48,6 +46,19 @@ func (repo ProtocolRepository) ExecuteReader(data string) (string, bool) { return "", false } +/* + 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] + if has { + println("OHOH") + return (*protWriter).Execute(data...) + } else { + return "", false + } +} + // containsReader Permet de vérifier si le reader est déjà contenu func (repo ProtocolRepository) containsReader(cmd string) bool { _, has := repo.protocolReaders[cmd] diff --git a/pkg/protocol/rules/writers/HelloRule.go b/pkg/protocol/rules/writers/HelloRule.go index c52ad84..5d420bb 100644 --- a/pkg/protocol/rules/writers/HelloRule.go +++ b/pkg/protocol/rules/writers/HelloRule.go @@ -13,7 +13,7 @@ type HelloRule struct { } // CreateHelloRule Création d'une instance de HelloRule -func CreateHelloRule(pattern string) *HelloRule { +func CreateHelloRule(pattern string) protocol.IProtocolWriter { return &HelloRule{ Cmd: HelloRuleName, matcher: protocol.CreateRegexMatcher(pattern), @@ -24,7 +24,6 @@ func (rule HelloRule) GetCmd() string { return rule.Cmd } -func (rule HelloRule) Execute(argsData []string) { - //TODO implement me - panic("implement me") +func (rule HelloRule) Execute(argsData ...string) (string, bool) { + return rule.matcher.Build("HELLO", argsData...) }