Tentative de récupération de fichier (problème: récupération bloquante)
This commit is contained in:
parent
b030af03c9
commit
38a177aa51
@ -7,7 +7,6 @@ import (
|
||||
"StoreBackEnd/pkg/protocol/repository"
|
||||
"StoreBackEnd/pkg/protocol/rules/readers"
|
||||
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||
"StoreBackEnd/pkg/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -16,7 +15,7 @@ const (
|
||||
)
|
||||
|
||||
func main() {
|
||||
utils.NetworkLister() // TODO REMOVE
|
||||
//utils.NetworkLister() // TODO REMOVE
|
||||
println("StoreBackEnd started !")
|
||||
|
||||
// Loading App config
|
||||
|
@ -30,14 +30,13 @@ func (server ServerUnicast) Run() {
|
||||
return
|
||||
} else {
|
||||
for { // TODO : Extraire cette partie de code
|
||||
reader := bufio.NewReader(con)
|
||||
line, err := reader.ReadString('\n')
|
||||
println("AH " + line)
|
||||
line, err := bufio.NewReader(con).ReadString('\n')
|
||||
println("[REQUEST] " + line)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result := server.ReqManager.Execute(line, reader)
|
||||
println("Réponse : ", result)
|
||||
result := server.ReqManager.Execute(line, con)
|
||||
println("[RESPONSE] : ", result)
|
||||
_, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package protocol
|
||||
|
||||
import "bufio"
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// IProtocolReader Représentation abstraite d'un protocol
|
||||
type IProtocolReader interface {
|
||||
@ -8,7 +10,7 @@ type IProtocolReader interface {
|
||||
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(data string) (string, bool, func(r *bufio.Reader) (string, bool))
|
||||
Execute(data string) (string, bool, func(r net.Conn) (string, bool))
|
||||
|
||||
// Match Permet de vérifier la validité d'une donnée censée suivre les règles d'un protocol
|
||||
Match(data string) bool
|
||||
|
@ -2,17 +2,17 @@ package managers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol/repository"
|
||||
"bufio"
|
||||
"net"
|
||||
)
|
||||
|
||||
type RequestManager struct {
|
||||
Repository *repository.ProtocolRepository
|
||||
}
|
||||
|
||||
func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string {
|
||||
func (receiver RequestManager) Execute(request string, reader net.Conn) string {
|
||||
// On lis ce que l'on reçoit
|
||||
result, executed, readCb := receiver.Repository.ExecuteReader(request)
|
||||
println("TESTTESTETTETT")
|
||||
|
||||
// On renvoie la réponse (Comment pour fichier ?)
|
||||
if executed {
|
||||
if readCb != nil {
|
||||
@ -23,8 +23,6 @@ func (receiver RequestManager) Execute(request string, reader *bufio.Reader) str
|
||||
}
|
||||
return result
|
||||
} else {
|
||||
println("HMMM")
|
||||
// TODO : Renvoyer qu'une erreur est survenue
|
||||
return "Error occurred while execute command"
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"bufio"
|
||||
"net"
|
||||
)
|
||||
|
||||
func CreateProtocolRepository() *ProtocolRepository {
|
||||
@ -36,7 +36,7 @@ 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é
|
||||
*/
|
||||
func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(r *bufio.Reader) (string, bool)) {
|
||||
func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(r net.Conn) (string, bool)) {
|
||||
for _, reader := range repo.protocolReaders {
|
||||
if (*reader).Match(data) { // Exécuter si match
|
||||
// Récupérer résultat à renvoyer (Donc donner ProtocolRepository aux reader ?!)
|
||||
|
@ -2,7 +2,7 @@ package readers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"bufio"
|
||||
"net"
|
||||
)
|
||||
|
||||
// EraseFileRulePrefix Identifiant de cette règle
|
||||
@ -29,7 +29,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) (string, bool, func(r net.Conn) (string, bool)) {
|
||||
if rule.Match(data) {
|
||||
values := rule.matcher.Parse(data)
|
||||
println(values[1], " est le hash du fichier à supprimer")
|
||||
|
@ -2,8 +2,10 @@ package readers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
// SendFileRulePrefix Rule command prefix
|
||||
@ -31,29 +33,31 @@ func (rule SendFileRule) GetCmd() string {
|
||||
}
|
||||
|
||||
// Execute the Rule with a string command.
|
||||
func (rule SendFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) {
|
||||
func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) {
|
||||
if rule.Match(data) { // TODO : cloture this command.
|
||||
values := rule.matcher.Parse(data)
|
||||
|
||||
// Values
|
||||
fileName := values[1]
|
||||
// fileSize := 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")
|
||||
|
||||
// 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))
|
||||
}
|
||||
callback := func(con net.Conn) (string, bool) {
|
||||
file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName))
|
||||
|
||||
*/
|
||||
var fileContent []byte
|
||||
readedCount, err := io.ReadFull(r, fileContent)
|
||||
println("Un test ici : ", readedCount, err)
|
||||
_, err := io.Copy(file, con)
|
||||
println("HEY1")
|
||||
if err != nil {
|
||||
println("Can't copy file")
|
||||
return "SEND_ERROR\r", false
|
||||
}
|
||||
println("HEY2")
|
||||
return "SEND_OK\r", true
|
||||
}
|
||||
return "SEND_OK\r", true, callback
|
||||
|
Loading…
Reference in New Issue
Block a user