StoreBackEnd/cmd/main.go

103 lines
3.6 KiB
Go

package main
import (
"StoreBackEnd/pkg/config"
"StoreBackEnd/pkg/network"
"StoreBackEnd/pkg/protocol/managers"
"StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/readers"
"StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils"
"log"
"time"
)
const (
FilePath = "resources/AppConfig.json"
)
func main() {
hash, err := utils.HashFileCompare("D:\\HELMoCrypted.png", "bacf89bd47be38ac445d5519c4e945bbea79c4a1f250cb31579e331e67941939")
if err != nil {
log.Fatal(err)
}
println("Hash : ", hash)
utils.NetworkLister() // TODO REMOVE
println("StoreBackEnd started !")
// Loading App config
appConfig, err := config.Read(FilePath)
if err != nil {
println("Impossible de charger la configuration du server : " + err.Error())
return
}
println(" - Multicast Network Interface : " + appConfig.MulticastNetworkInterface)
println(" - Multicast Address : " + appConfig.MulticastAddress)
println(" - StoreBacked Domain : " + appConfig.Domain)
protocolRepository := repository.CreateProtocolRepository()
/**
===== Init all Write here =====
*/
// Creation of the HelloRule
helloRule := writers.CreateHelloRule("^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$")
protocolRepository.AddWriter(&helloRule)
// Creation of the SendOkRule
sendOkRule := writers.CreateSendOkRule("^SEND_OK\r\n$")
protocolRepository.AddWriter(&sendOkRule)
// Creation of the SendErrorRule
sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\r\n$")
protocolRepository.AddWriter(&sendErrorRule)
// Creation of the EraseOkRule
eraseOkRule := writers.CreateEraseOkRule("^ERASE_OK\r\n$")
protocolRepository.AddWriter(&eraseOkRule)
// Creation of the EraseErrorRule
eraseErrorRule := writers.CreateEraseErrorRule("^ERASE_ERROR\r\n$")
protocolRepository.AddWriter(&eraseErrorRule)
// Creation of the EraseOkRule
retrieveOkRule := writers.CreateRetrieveOkRule("^RETRIEVE_OK ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n$", appConfig.StoragePath)
protocolRepository.AddWriter(&retrieveOkRule)
// Creation of the EraseErrorRule
retrieveErrorRule := writers.CreateRetrieveErrorRule("^RETRIEVE_ERROR\r\n$")
protocolRepository.AddWriter(&retrieveErrorRule)
/**
===== Init all Reader here =====
*/
// Creation of the EraseFileRule
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&eraseFileRule)
// Creation of the SendFileRule
sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{50,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&sendFileRule)
// Create of the RetrieveFileRule
retrieveFileRule := readers.CreateRetrieveFileRule("^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&retrieveFileRule)
// Create a Multicast Client & run it
multicast := network.CreateClientMulticast(
appConfig.MulticastNetworkInterface, appConfig.MulticastAddress, appConfig.Domain,
appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository)
go multicast.Run()
requestManager := managers.RequestManager{Repository: protocolRepository}
server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager}
server.Run() // TODO : -> pourquoi ne pas partir dans un thread ici.
//reader := bufio.NewReader(os.Stdin) TODO ne pas oublier ici de mettre en place un point de sortie pour le programme.
//fmt.Print("Type Enter to quite: ")
//cmd, _ := reader.ReadString('\n')
//println(cmd)
}