- Changement des entêtes dans le projets.

This commit is contained in:
Maximilien LEDOUX 2022-03-12 15:57:27 +01:00
parent 4975c4e454
commit f75948c3bb
14 changed files with 124 additions and 72 deletions

View File

@ -51,11 +51,11 @@ func main() {
===== Init all Reader here ===== ===== Init all Reader here =====
*/ */
// Creation of the EraseFileRule // 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 // TODO reset to 50,200 // 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$") sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository)
protocolRepository.AddReader(&sendFileRule) protocolRepository.AddReader(&sendFileRule)
// Create a Multicast Client & run it // Create a Multicast Client & run it

View File

@ -58,14 +58,14 @@ func (client ClientMulticast) Run() {
return return
} }
cmd, correct := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port)) writerResult := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port))
if !correct { if writerResult == nil {
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")") println("[ClientMulticast] Hello rule isn't correct")
return return
} }
for { for {
_, _ = con.Write([]byte(cmd)) _, _ = con.Write([]byte(writerResult.Cmd))
time.Sleep(time.Second * client.second) time.Sleep(time.Second * client.second)
} }
} }

View File

@ -36,9 +36,22 @@ func (server ServerUnicast) Run() {
if err != nil { if err != nil {
return return
} }
result := server.ReqManager.Execute(line, reader) response := server.ReqManager.Execute(line, reader)
println("[RESPONSE] : ", result)
_, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n if response == nil {
break
} else {
println("[RESPONSE] : ", response.Cmd)
_, _ = con.Write([]byte(response.Cmd)) // TODO : ATTENTION laisser les \n
if response.Write != nil {
response.Write(bufio.NewWriter(con))
}
}
} }
con.Close()
} }
listen.Close()
} }

View File

@ -10,7 +10,7 @@ type IProtocolReader interface {
GetCmd() string 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 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, func(reader *bufio.Reader) (string, bool)) Execute(data string) (*ProtocolWriterResult, func(reader *bufio.Reader) *ProtocolWriterResult)
// Match Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol // Match Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol
Match(data string) bool Match(data string) bool

View File

@ -1,10 +1,20 @@
package protocol package protocol
import "bufio"
// IProtocolWriter Représentation abstraite d'un protocol // IProtocolWriter Représentation abstraite d'un protocol
type IProtocolWriter interface { 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 créer une règle à envoyer // Execute Permet de créer une règle à envoyer
Execute(argsData ...string) (string, bool) Execute(argsData ...string) *ProtocolWriterResult
}
type ProtocolWriterResult struct {
// Cmd Commande générée par le ProtocolWriter
Cmd string
// Write Fonction appelée
Write func(writer *bufio.Writer)
} }

View File

@ -1,6 +1,7 @@
package managers package managers
import ( import (
"StoreBackEnd/pkg/protocol"
"StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/repository"
"bufio" "bufio"
) )
@ -9,20 +10,20 @@ type RequestManager struct {
Repository *repository.ProtocolRepository Repository *repository.ProtocolRepository
} }
func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string { func (receiver RequestManager) Execute(request string, reader *bufio.Reader) *protocol.ProtocolWriterResult {
// On lis ce que l'on reçoit // On lis ce que l'on reçoit
result, executed, readCb := receiver.Repository.ExecuteReader(request) writeCmd, readCb := receiver.Repository.ExecuteReader(request)
// On renvoie la réponse (Comment pour fichier ?) // On renvoie la réponse (Comment pour fichier ?)
if executed { if writeCmd != nil {
if readCb != nil { if readCb != nil {
cbResult, _ := readCb(reader) cbResult := readCb(reader)
if cbResult != "" { if cbResult != nil {
result = cbResult writeCmd = cbResult
} }
} }
return result return writeCmd
} else { } else {
return "Error occurred while execute command" return nil
} }
} }

View File

@ -36,26 +36,25 @@ 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é 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(reader *bufio.Reader) (string, bool)) { func (repo ProtocolRepository) ExecuteReader(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
for _, reader := range repo.protocolReaders { for _, reader := range repo.protocolReaders {
if (*reader).Match(data) { // Exécuter si match if (*reader).Match(data) { // Exécuter si match
// Récupérer résultat à renvoyer (Donc donner ProtocolRepository aux reader ?!) // Récupérer résultat à renvoyer (Donc donner ProtocolRepository aux reader ?!)
return (*reader).Execute(data) return (*reader).Execute(data)
} }
} }
return "", false, nil return nil, 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. 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) *protocol.ProtocolWriterResult {
protocolWriter, has := repo.protocolWriters[ruleName] protocolWriter, has := repo.protocolWriters[ruleName]
if has { if has {
println("OHOH")
return (*protocolWriter).Execute(data...) return (*protocolWriter).Execute(data...)
} else { } else {
return "", false return nil
} }
} }

View File

@ -1,10 +1,6 @@
package readers package readers
import ( /*
"StoreBackEnd/pkg/protocol"
"bufio"
)
// EraseFileRulePrefix Identifiant de cette règle // EraseFileRulePrefix Identifiant de cette règle
const EraseFileRulePrefix = "ERASEFILE" const EraseFileRulePrefix = "ERASEFILE"
@ -29,7 +25,7 @@ func (rule EraseFileRule) GetCmd() string {
return rule.Cmd return rule.Cmd
} }
func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) { func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, bool, func(reader *bufio.Reader) (*protocol.ProtocolWriterResult, bool)) {
if rule.Match(data) { if rule.Match(data) {
values := rule.matcher.Parse(data) values := rule.matcher.Parse(data)
println(values[1], " est le hash du fichier à supprimer") println(values[1], " est le hash du fichier à supprimer")
@ -42,3 +38,5 @@ func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Read
func (rule EraseFileRule) Match(data string) bool { func (rule EraseFileRule) Match(data string) bool {
return rule.matcher.Match(data) return rule.matcher.Match(data)
} }
*/

View File

@ -2,6 +2,8 @@ package readers
import ( import (
"StoreBackEnd/pkg/protocol" "StoreBackEnd/pkg/protocol"
"StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils" "StoreBackEnd/pkg/utils"
"bufio" "bufio"
"strconv" "strconv"
@ -16,13 +18,16 @@ type SendFileRule struct {
cmd string cmd string
// matcher Allow to make a regex match // matcher Allow to make a regex match
matcher *protocol.RegexMatcher matcher *protocol.RegexMatcher
// protocolRepo Instance de ProtocolRepository
protocolRepo *repository.ProtocolRepository
} }
// CreateSendFileRule Creating an instance of SendFileRule // CreateSendFileRule Creating an instance of SendFileRule
func CreateSendFileRule(pattern string) protocol.IProtocolReader { func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader {
return &SendFileRule{ return &SendFileRule{
cmd: SendFileRulePrefix, cmd: SendFileRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern), matcher: protocol.CreateRegexMatcher(pattern),
protocolRepo: protocolRepo,
} }
} }
@ -32,44 +37,36 @@ func (rule SendFileRule) GetCmd() string {
} }
// Execute the Rule with a string command. // Execute the Rule with a string command.
func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) { func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
println(0)
if rule.Match(data) { // TODO : cloture this command. if rule.Match(data) { // TODO : cloture this command.
println("OK")
values := rule.matcher.Parse(data) values := rule.matcher.Parse(data)
// Values // Values
fileName := values[1] fileName := values[1]
fileSize, _ := strconv.Atoi(values[2]) fileSize, _ := strconv.Atoi(values[2])
// fileContentHash := values[3] // fileContentHash := values[3]
println(1)
// print received data
println(values[1], " File Name Hash")
println(values[2], " File Size")
println(values[3], " File Content Hash")
// function callback // function callback
callback := func(reader *bufio.Reader) (string, bool) { callback := rule.onRead(fileName, fileSize)
hasReceive := utils.ReceiveFile(fileName, fileSize, reader) println(2)
println("HEY1") return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
/*
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 { } else {
println("AHAHAHAHAHAHA") return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil
return "SEND_ERROR\r", false, nil }
}
func (rule SendFileRule) onRead(fileName string, fileSize int) func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
return func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
hasReceive := utils.ReceiveFile(fileName, fileSize, reader)
println(3)
if !hasReceive {
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
} else {
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix)
}
} }
} }

View File

@ -27,6 +27,10 @@ func (rule HelloRule) GetCmd() string {
} }
// Execute the Rule with a string command. // Execute the Rule with a string command.
func (rule HelloRule) Execute(argsData ...string) (string, bool) { func (rule HelloRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
return rule.matcher.Build(HelloRulePrefix, argsData...) cmd, _ := rule.matcher.Build(HelloRulePrefix, argsData...)
return &protocol.ProtocolWriterResult{
Cmd: cmd,
}
} }

View File

@ -1,6 +1,8 @@
package writers package writers
import "StoreBackEnd/pkg/protocol" import (
"StoreBackEnd/pkg/protocol"
)
// SendErrorRulePrefix Rule command prefix // SendErrorRulePrefix Rule command prefix
const SendErrorRulePrefix = "SEND_ERROR" const SendErrorRulePrefix = "SEND_ERROR"
@ -27,6 +29,17 @@ func (rule SendErrorRule) GetCmd() string {
} }
// Execute the Rule with a string command. // Execute the Rule with a string command.
func (rule SendErrorRule) Execute(argsData ...string) (string, bool) { func (rule SendErrorRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
return rule.matcher.Build(SendErrorRulePrefix, argsData...) buildedCmd, _ := rule.matcher.Build(SendErrorRulePrefix, argsData...)
return &protocol.ProtocolWriterResult{
Cmd: buildedCmd,
}
} }
/*
func onWrite(writer *bufio.Writer) {
println("Ecriture du fichier sur le réseau")
}
*/

View File

@ -1,6 +1,8 @@
package writers package writers
import "StoreBackEnd/pkg/protocol" import (
"StoreBackEnd/pkg/protocol"
)
// SendOkRulePrefix Rule command prefix // SendOkRulePrefix Rule command prefix
const SendOkRulePrefix = "SEND_OK" const SendOkRulePrefix = "SEND_OK"
@ -27,6 +29,10 @@ func (rule SendOkRule) GetCmd() string {
} }
// Execute the Rule with a string command. // Execute the Rule with a string command.
func (rule SendOkRule) Execute(argsData ...string) (string, bool) { func (rule SendOkRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
return rule.matcher.Build(SendOkRulePrefix, argsData...) buildedCmd, _ := rule.matcher.Build(SendOkRulePrefix, argsData...)
return &protocol.ProtocolWriterResult{
Cmd: buildedCmd,
}
} }

View File

@ -8,7 +8,7 @@ import (
// ReceiveFile Permet de récupérer un fichier sur un reader // ReceiveFile Permet de récupérer un fichier sur un reader
func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool {
file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) file, fileErr := os.Create(fmt.Sprintf("C:\\Users\\ledou\\Documents\\sbe\\%s", fileName))
if fileErr != nil { if fileErr != nil {
return false return false
} }
@ -18,6 +18,7 @@ func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool {
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
for currentSize < fileSize { for currentSize < fileSize {
println("LIS")
length, err := reader.Read(buffer) length, err := reader.Read(buffer)
if err != nil { if err != nil {
return false return false

10
pkg/utils/FileSender.go Normal file
View File

@ -0,0 +1,10 @@
package utils
import (
"bufio"
)
func SendFile(fileName string, fileSize int, writer *bufio.Writer) bool {
return false
}