Merge branch 'dev' into maximilien
# Conflicts: # cmd/main.go # pkg/network/ClientMulticast.go
This commit is contained in:
commit
6cc3922347
19
cmd/main.go
19
cmd/main.go
@ -1,22 +1,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"StorBackEnd/pkg/config"
|
||||||
"StorBackEnd/pkg/network"
|
"StorBackEnd/pkg/network"
|
||||||
"StorBackEnd/pkg/protocol/managers"
|
"StorBackEnd/pkg/protocol/managers"
|
||||||
"StorBackEnd/pkg/protocol/repository"
|
"StorBackEnd/pkg/protocol/repository"
|
||||||
"StorBackEnd/pkg/protocol/rules/readers"
|
"StorBackEnd/pkg/protocol/rules/readers"
|
||||||
"StorBackEnd/pkg/protocol/rules/writers"
|
"StorBackEnd/pkg/protocol/rules/writers"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MULTICAST_ADDRESS = "226.66.66.1:42500"
|
FILE_PATH = "resources/AppConfig.json"
|
||||||
MULTICAST_SECOND = 10 // TODO : Changer en 30 secondes
|
|
||||||
UNICAST_ADDRESS = "0.0.0.0:58000"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
println("StorBackEnd started !")
|
println("StorBackEnd started !")
|
||||||
|
|
||||||
|
// Loading App config
|
||||||
|
appConfig, err := config.Read(FILE_PATH)
|
||||||
|
if err != nil {
|
||||||
|
println("Impossible de charger la configuration du server : " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Adresse multicast : " + appConfig.MulticastAddress)
|
||||||
|
|
||||||
protocolRepository := repository.CreateProtocolRepository()
|
protocolRepository := repository.CreateProtocolRepository()
|
||||||
|
|
||||||
// Création des Writers
|
// Création des Writers
|
||||||
@ -27,12 +36,12 @@ func main() {
|
|||||||
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$")
|
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$")
|
||||||
protocolRepository.AddReader(&eraseFileRule)
|
protocolRepository.AddReader(&eraseFileRule)
|
||||||
|
|
||||||
multicast := network.CreateClientMulticast(MULTICAST_ADDRESS, MULTICAST_SECOND, protocolRepository)
|
multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository)
|
||||||
go multicast.Run()
|
go multicast.Run()
|
||||||
|
|
||||||
requestManager := managers.RequestManager{Repository: protocolRepository}
|
requestManager := managers.RequestManager{Repository: protocolRepository}
|
||||||
|
|
||||||
server := network.ServerUnicast{Network: "tcp", Address: UNICAST_ADDRESS, ReqManager: &requestManager}
|
server := network.ServerUnicast{Network: "tcp", Port: appConfig.UnicastPort, ReqManager: &requestManager}
|
||||||
server.Run()
|
server.Run()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,27 +2,15 @@ package config
|
|||||||
|
|
||||||
// AppConfig Contient toute la configuration du server
|
// AppConfig Contient toute la configuration du server
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
// multicastAddress Contient l'adresse multicast du FileFrontEnd (adresse:port)
|
// multicastAddress Contient l'adresse multicast du FileFrontEnd
|
||||||
multicastAddress string
|
MulticastAddress string `json:"multicastAddress"`
|
||||||
|
|
||||||
// multicastSecond Contient le nombre de seconde entre chaque annonce
|
// multicastSecond Contient le nombre de seconde entre chaque annonce
|
||||||
multicastSecond int
|
MulticastSecond int `json:"multicastSecond"`
|
||||||
|
|
||||||
// unicastAddress Contient l'adresse unicast auquel le FileFrontEnd se connecte (adresse:port)
|
// domain Domain du StorBackEnd
|
||||||
unicastAddress string
|
Domain string `json:"domain"`
|
||||||
}
|
|
||||||
|
|
||||||
// MulticastAddress Accesseur pour multicastAddress
|
// unicastPort Contient le port unicast auquel le FileFrontEnd se connecte
|
||||||
func (a *AppConfig) MulticastAddress() string {
|
UnicastPort int `json:"unicastPort"`
|
||||||
return a.multicastAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
// MulticastSecond Accesseur pour multicastSecond
|
|
||||||
func (a *AppConfig) MulticastSecond() int {
|
|
||||||
return a.multicastSecond
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnicastAddress Accesseur pour unicastAddress
|
|
||||||
func (a *AppConfig) UnicastAddress() string {
|
|
||||||
return a.unicastAddress
|
|
||||||
}
|
}
|
||||||
|
21
pkg/config/JsonConfigReader.go
Normal file
21
pkg/config/JsonConfigReader.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Read(filePath string) (*AppConfig, error) {
|
||||||
|
config := AppConfig{}
|
||||||
|
file, err := ioutil.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
errJson := json.Unmarshal(file, &config)
|
||||||
|
if errJson != nil {
|
||||||
|
return nil, errJson
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config, nil
|
||||||
|
}
|
@ -3,14 +3,17 @@ package network
|
|||||||
import (
|
import (
|
||||||
"StorBackEnd/pkg/protocol/repository"
|
"StorBackEnd/pkg/protocol/repository"
|
||||||
"StorBackEnd/pkg/protocol/rules/writers"
|
"StorBackEnd/pkg/protocol/rules/writers"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast
|
// CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast
|
||||||
func CreateClientMulticast(address string, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
|
func CreateClientMulticast(address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
|
||||||
return ClientMulticast{
|
return ClientMulticast{
|
||||||
address: address,
|
address: address,
|
||||||
|
domain: domain,
|
||||||
|
port: port,
|
||||||
second: second,
|
second: second,
|
||||||
repository: repository,
|
repository: repository,
|
||||||
}
|
}
|
||||||
@ -19,9 +22,15 @@ func CreateClientMulticast(address string, second time.Duration, repository *rep
|
|||||||
// ClientMulticast Cette structure représente une communication en multicast.
|
// ClientMulticast Cette structure représente une communication en multicast.
|
||||||
// TODO : Prévoir une fermeture de la connection (con.Close())
|
// TODO : Prévoir une fermeture de la connection (con.Close())
|
||||||
type ClientMulticast struct {
|
type ClientMulticast struct {
|
||||||
// address Adresse de multicast (address:port)
|
// address Adresse de multicast
|
||||||
address string
|
address string
|
||||||
|
|
||||||
|
// address Domain de du StorBackEnd
|
||||||
|
domain string
|
||||||
|
|
||||||
|
// port Port de connexion en unicast
|
||||||
|
port int
|
||||||
|
|
||||||
// second Temps en seconde entre chaque ping
|
// second Temps en seconde entre chaque ping
|
||||||
second time.Duration
|
second time.Duration
|
||||||
|
|
||||||
@ -41,8 +50,7 @@ func (cMult ClientMulticast) Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fake test
|
cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, cMult.domain, fmt.Sprintf("%d", cMult.port))
|
||||||
cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, "benja", "5078")
|
|
||||||
if !correct {
|
if !correct {
|
||||||
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")")
|
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")")
|
||||||
return
|
return
|
||||||
|
@ -9,13 +9,13 @@ import (
|
|||||||
|
|
||||||
type ServerUnicast struct {
|
type ServerUnicast struct {
|
||||||
Network string
|
Network string
|
||||||
Address string
|
Port int
|
||||||
ReqManager *managers.RequestManager
|
ReqManager *managers.RequestManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server ServerUnicast) Run() {
|
func (server ServerUnicast) Run() {
|
||||||
|
|
||||||
listen, err := net.Listen(server.Network, server.Address) // "tcp", "0.0.0.0:58000"
|
listen, err := net.Listen(server.Network, fmt.Sprintf("0.0.0.0:%d", server.Port)) // "tcp", "0.0.0.0:58000"
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Can't start server : %s\n", err)
|
fmt.Printf("Can't start server : %s\n", err)
|
||||||
@ -36,7 +36,7 @@ func (server ServerUnicast) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := server.ReqManager.Execute(line)
|
result := server.ReqManager.Execute(line)
|
||||||
con.Write(append([]byte(result), '\n'))
|
con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
resources/AppConfig.json
Normal file
6
resources/AppConfig.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"multicastAddress" : "226.66.66.1:42500",
|
||||||
|
"multicastSecond" : 10,
|
||||||
|
"domain" : "benjamin",
|
||||||
|
"unicastPort" : 58000
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user