Ajout des 3règles SendFileRule.go, SendOkRule.go, SendErrorRule.go et corrections de bugs mineurs.

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-08 10:25:00 +01:00
parent 8c443d43e4
commit e9fafd3cd8
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
9 changed files with 163 additions and 25 deletions

View File

@ -29,19 +29,43 @@ func main() {
protocolRepository := repository.CreateProtocolRepository() 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$") helloRule := writers.CreateHelloRule("^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$")
protocolRepository.AddWriter(&helloRule) protocolRepository.AddWriter(&helloRule)
// Création des Readers // Creation of the SendOkRule
sendOkRule := writers.CreateSendOkRule("^SEND_OK\n$")
protocolRepository.AddWriter(&sendOkRule)
// Creation of the SendErrorRule
sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\n$")
protocolRepository.AddWriter(&sendErrorRule)
/**
===== Init all Reader here =====
*/
// Creation of the EraseFileRule
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$") eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$")
protocolRepository.AddReader(&eraseFileRule) 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$")
protocolRepository.AddReader(&sendFileRule)
// Create a Multicast Client & run it
multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository) multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository)
go multicast.Run() go multicast.Run()
requestManager := managers.RequestManager{Repository: protocolRepository} requestManager := managers.RequestManager{Repository: protocolRepository}
server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager} 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)
} }

View File

@ -40,17 +40,17 @@ type ClientMulticast struct {
// Run Cette méthode démarre une commmunication multicast // Run Cette méthode démarre une commmunication multicast
func (client ClientMulticast) Run() { func (client ClientMulticast) Run() {
addr, done := client.ResolveAddr() addr, failedRA := client.ResolveAddr()
if done { if failedRA {
return return
} }
con, done2 := client.DialUdp(addr) con, failedDU := client.DialUdp(addr)
if done2 { if failedDU {
return return
} }
cmd, correct := client.repository.ExecuteWriter(writers.HelloRuleName, client.domain, fmt.Sprintf("%d", client.port)) cmd, correct := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port))
if !correct { if !correct {
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")") println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")")
return return

View File

@ -5,6 +5,6 @@ type IProtocolWriter interface {
// GetCmd Permet de récupérer le nom de la commande // GetCmd Permet de récupérer le nom de la commande
GetCmd() string 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) Execute(argsData ...string) (string, bool)
} }

View File

@ -17,7 +17,6 @@ type ProtocolRepository struct {
// AddReader Permet d'ajouter une règle servant à lire une commande // AddReader Permet d'ajouter une règle servant à lire une commande
func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) { func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) {
cmd := (*reader).GetCmd() cmd := (*reader).GetCmd()
if cmd != "" && !repo.containsReader(cmd) { if cmd != "" && !repo.containsReader(cmd) {
repo.protocolReaders[cmd] = reader repo.protocolReaders[cmd] = reader
} }
@ -26,7 +25,6 @@ func (repo ProtocolRepository) AddReader(reader *protocol.IProtocolReader) {
// AddWriter Permet d'ajouter une règle servant à construire une commande // AddWriter Permet d'ajouter une règle servant à construire une commande
func (repo ProtocolRepository) AddWriter(writer *protocol.IProtocolWriter) { func (repo ProtocolRepository) AddWriter(writer *protocol.IProtocolWriter) {
cmd := (*writer).GetCmd() cmd := (*writer).GetCmd()
if cmd != "" && !repo.containsWriter(cmd) { if cmd != "" && !repo.containsWriter(cmd) {
repo.protocolWriters[cmd] = writer repo.protocolWriters[cmd] = writer
} }
@ -49,10 +47,10 @@ func (repo ProtocolRepository) ExecuteReader(data string) (string, bool) {
ExecuteReader Permet d'exécuter un Wrier et de récupérer la commande construite avec nos argument et un indicateur de réussite. 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) (string, bool) {
protWriter, has := repo.protocolWriters[ruleName] protocolWriter, has := repo.protocolWriters[ruleName]
if has { if has {
println("OHOH") println("OHOH")
return (*protWriter).Execute(data...) return (*protocolWriter).Execute(data...)
} else { } else {
return "", false return "", false
} }

View File

@ -2,8 +2,8 @@ package readers
import "StoreBackEnd/pkg/protocol" import "StoreBackEnd/pkg/protocol"
// EraseFileRuleName Identifiant de cette règle // EraseFileRulePrefix Identifiant de cette règle
const EraseFileRuleName = "ffe_erasefile" const EraseFileRulePrefix = "ERASEFILE"
// EraseFileRule Demande de suppression d'un fichier // EraseFileRule Demande de suppression d'un fichier
type EraseFileRule struct { type EraseFileRule struct {
@ -17,7 +17,7 @@ type EraseFileRule struct {
// CreateEraseFileRule Création d'une instance de EraseFileRule // CreateEraseFileRule Création d'une instance de EraseFileRule
func CreateEraseFileRule(pattern string) protocol.IProtocolReader { func CreateEraseFileRule(pattern string) protocol.IProtocolReader {
return &EraseFileRule{ return &EraseFileRule{
Cmd: EraseFileRuleName, Cmd: EraseFileRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern), matcher: protocol.CreateRegexMatcher(pattern),
} }
} }

View File

@ -0,0 +1,49 @@
package readers
import "StoreBackEnd/pkg/protocol"
// 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) {
if rule.Match(data) { // TODO : cloture this command.
values := rule.matcher.Parse(data)
//callback := func() {
//
//}
//callback()
println(values[1], " File Name Hash")
println(values[2], " File Size")
println(values[3], " File Content Hash")
return "SEND_OK\r\n", true
} else {
return "jjdjd", false
}
}
// 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)
}

View File

@ -2,28 +2,31 @@ package writers
import "StoreBackEnd/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 { type HelloRule struct {
// Cmd Nom de la règle // cmd Rule name
Cmd string cmd string
// matcher Allow to make a regex match
// matcher Permet d'extraire de éléments d'une chaine
matcher *protocol.RegexMatcher matcher *protocol.RegexMatcher
} }
// CreateHelloRule Création d'une instance de HelloRule // CreateHelloRule Creating an instance of HelloRule
func CreateHelloRule(pattern string) protocol.IProtocolWriter { func CreateHelloRule(pattern string) protocol.IProtocolWriter {
return &HelloRule{ return &HelloRule{
Cmd: HelloRuleName, cmd: HelloRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern), matcher: protocol.CreateRegexMatcher(pattern),
} }
} }
// GetCmd retrieve the command name.
func (rule HelloRule) GetCmd() string { 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) { func (rule HelloRule) Execute(argsData ...string) (string, bool) {
return rule.matcher.Build("HELLO", argsData...) return rule.matcher.Build(HelloRulePrefix, argsData...)
} }

View File

@ -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...)
}

View File

@ -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...)
}