2022-03-08 10:25:00 +01:00
|
|
|
package readers
|
|
|
|
|
2022-03-08 12:01:51 +01:00
|
|
|
import (
|
|
|
|
"StoreBackEnd/pkg/protocol"
|
2022-03-12 15:57:27 +01:00
|
|
|
"StoreBackEnd/pkg/protocol/repository"
|
|
|
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
2022-03-08 20:37:49 +01:00
|
|
|
"StoreBackEnd/pkg/utils"
|
|
|
|
"bufio"
|
|
|
|
"strconv"
|
2022-03-08 12:01:51 +01:00
|
|
|
)
|
2022-03-08 10:25:00 +01:00
|
|
|
|
|
|
|
// 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
|
2022-03-12 15:57:27 +01:00
|
|
|
// protocolRepo Instance de ProtocolRepository
|
|
|
|
protocolRepo *repository.ProtocolRepository
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSendFileRule Creating an instance of SendFileRule
|
2022-03-12 15:57:27 +01:00
|
|
|
func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader {
|
2022-03-08 10:25:00 +01:00
|
|
|
return &SendFileRule{
|
2022-03-12 15:57:27 +01:00
|
|
|
cmd: SendFileRulePrefix,
|
|
|
|
matcher: protocol.CreateRegexMatcher(pattern),
|
|
|
|
protocolRepo: protocolRepo,
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmd retrieve the command name.
|
|
|
|
func (rule SendFileRule) GetCmd() string {
|
|
|
|
return rule.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the Rule with a string command.
|
2022-03-12 15:57:27 +01:00
|
|
|
func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
|
|
|
|
println(0)
|
2022-03-08 10:25:00 +01:00
|
|
|
if rule.Match(data) { // TODO : cloture this command.
|
2022-03-12 15:57:27 +01:00
|
|
|
println("OK")
|
2022-03-08 10:25:00 +01:00
|
|
|
values := rule.matcher.Parse(data)
|
2022-03-08 12:01:51 +01:00
|
|
|
|
2022-03-08 17:44:10 +01:00
|
|
|
// Values
|
|
|
|
fileName := values[1]
|
2022-03-08 20:37:49 +01:00
|
|
|
fileSize, _ := strconv.Atoi(values[2])
|
2022-03-08 17:44:10 +01:00
|
|
|
// fileContentHash := values[3]
|
2022-03-12 15:57:27 +01:00
|
|
|
println(1)
|
2022-03-08 12:01:51 +01:00
|
|
|
// function callback
|
2022-03-12 15:57:27 +01:00
|
|
|
callback := rule.onRead(fileName, fileSize)
|
|
|
|
println(2)
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
|
|
|
|
} else {
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rule SendFileRule) onRead(fileName string, fileSize int) func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
2022-03-08 20:37:49 +01:00
|
|
|
|
2022-03-12 15:57:27 +01:00
|
|
|
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)
|
2022-03-08 12:01:51 +01:00
|
|
|
}
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|