Réception d'un fichier et sauvegarde de celui-ci terminée
This commit is contained in:
parent
a987a7eb64
commit
6fa5f6c1c4
@ -30,12 +30,13 @@ func (server ServerUnicast) Run() {
|
||||
return
|
||||
} else {
|
||||
for { // TODO : Extraire cette partie de code
|
||||
line, err := bufio.NewReader(con).ReadString('\n')
|
||||
reader := bufio.NewReader(con)
|
||||
line, err := reader.ReadString('\n')
|
||||
println("[REQUEST] " + line)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result := server.ReqManager.Execute(line, con)
|
||||
result := server.ReqManager.Execute(line, reader)
|
||||
println("[RESPONSE] : ", result)
|
||||
_, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"net"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
// IProtocolReader Représentation abstraite d'un protocol
|
||||
@ -10,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 net.Conn) (string, bool))
|
||||
Execute(data string) (string, bool, func(reader *bufio.Reader) (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,14 +2,14 @@ package managers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol/repository"
|
||||
"net"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
type RequestManager struct {
|
||||
Repository *repository.ProtocolRepository
|
||||
}
|
||||
|
||||
func (receiver RequestManager) Execute(request string, reader net.Conn) string {
|
||||
func (receiver RequestManager) Execute(request string, reader *bufio.Reader) string {
|
||||
// On lis ce que l'on reçoit
|
||||
result, executed, readCb := receiver.Repository.ExecuteReader(request)
|
||||
|
||||
|
@ -2,7 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"net"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
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 net.Conn) (string, bool)) {
|
||||
func (repo ProtocolRepository) ExecuteReader(data string) (string, bool, func(reader *bufio.Reader) (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"
|
||||
"net"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
// 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 net.Conn) (string, bool)) {
|
||||
func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) {
|
||||
if rule.Match(data) {
|
||||
values := rule.matcher.Parse(data)
|
||||
println(values[1], " est le hash du fichier à supprimer")
|
||||
|
@ -2,10 +2,9 @@ package readers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"StoreBackEnd/pkg/utils"
|
||||
"bufio"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// SendFileRulePrefix Rule command prefix
|
||||
@ -33,13 +32,13 @@ func (rule SendFileRule) GetCmd() string {
|
||||
}
|
||||
|
||||
// Execute the Rule with a string command.
|
||||
func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) {
|
||||
func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) {
|
||||
if rule.Match(data) { // TODO : cloture this command.
|
||||
values := rule.matcher.Parse(data)
|
||||
|
||||
// Values
|
||||
fileName := values[1]
|
||||
// fileSize := values[2]
|
||||
fileSize, _ := strconv.Atoi(values[2])
|
||||
// fileContentHash := values[3]
|
||||
|
||||
// print received data
|
||||
@ -48,13 +47,20 @@ func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (s
|
||||
println(values[3], " File Content Hash")
|
||||
|
||||
// function callback
|
||||
callback := func(con net.Conn) (string, bool) {
|
||||
file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName))
|
||||
|
||||
_, err := io.Copy(file, con)
|
||||
callback := func(reader *bufio.Reader) (string, bool) {
|
||||
hasReceive := utils.ReceiveFile(fileName, fileSize, reader)
|
||||
println("HEY1")
|
||||
if err != nil {
|
||||
println("Can't copy file")
|
||||
/*
|
||||
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")
|
||||
|
@ -1 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ReceiveFile Permet de récupérer un fichier sur un reader
|
||||
func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool {
|
||||
file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
|
||||
if fileErr != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
println(1)
|
||||
defer file.Close()
|
||||
_, err := io.CopyN(file, reader, int64(fileSize))
|
||||
println(2)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user