- Changement des entêtes dans le projets.
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
package readers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
/*
|
||||
// EraseFileRulePrefix Identifiant de cette règle
|
||||
const EraseFileRulePrefix = "ERASEFILE"
|
||||
|
||||
@@ -29,7 +25,7 @@ func (rule EraseFileRule) GetCmd() string {
|
||||
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) {
|
||||
values := rule.matcher.Parse(data)
|
||||
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 {
|
||||
return rule.matcher.Match(data)
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,8 @@ package readers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"StoreBackEnd/pkg/protocol/repository"
|
||||
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||
"StoreBackEnd/pkg/utils"
|
||||
"bufio"
|
||||
"strconv"
|
||||
@@ -16,13 +18,16 @@ type SendFileRule struct {
|
||||
cmd string
|
||||
// matcher Allow to make a regex match
|
||||
matcher *protocol.RegexMatcher
|
||||
// protocolRepo Instance de ProtocolRepository
|
||||
protocolRepo *repository.ProtocolRepository
|
||||
}
|
||||
|
||||
// CreateSendFileRule Creating an instance of SendFileRule
|
||||
func CreateSendFileRule(pattern string) protocol.IProtocolReader {
|
||||
func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader {
|
||||
return &SendFileRule{
|
||||
cmd: SendFileRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
cmd: SendFileRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
protocolRepo: protocolRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,44 +37,36 @@ func (rule SendFileRule) GetCmd() string {
|
||||
}
|
||||
|
||||
// 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.
|
||||
println("OK")
|
||||
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")
|
||||
|
||||
println(1)
|
||||
// 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
|
||||
callback := rule.onRead(fileName, fileSize)
|
||||
println(2)
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
|
||||
} else {
|
||||
println("AHAHAHAHAHAHA")
|
||||
return "SEND_ERROR\r", false, nil
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user