StoreBackEnd/pkg/protocol/rules/readers/SendFileRule.go

70 lines
1.8 KiB
Go
Raw Normal View History

package readers
import (
"StoreBackEnd/pkg/protocol"
"bufio"
"io"
)
// 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, func(r *bufio.Reader) (string, bool)) {
if rule.Match(data) { // TODO : cloture this command.
values := rule.matcher.Parse(data)
// print received data
println(values[1], " File Name Hash")
println(values[2], " File Size")
println(values[3], " File Content Hash")
// function callback
callback := func(r *bufio.Reader) (string, bool) {
//var fileContent []byte
// readedByte := -1
/*
for readedByte == -1 || readedByte > 0 {
readedByte, _ = r.Read(fileContent)
println("SendFileRule Reading file ", " - Hey - ", string(readedByte))
}
*/
var fileContent []byte
readedCount, err := io.ReadFull(r, fileContent)
println("Un test ici : ", readedCount, err)
return "SEND_OK\r", true
}
return "SEND_OK\r", true, callback
} else {
println("AHAHAHAHAHAHA")
return "SEND_ERROR\r", false, nil
}
}
// 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)
}