Ajout système d'interface
This commit is contained in:
parent
30e21ce042
commit
51defc39ac
11
cmd/main.go
11
cmd/main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"StoreBackEnd/pkg/protocol/repository"
|
"StoreBackEnd/pkg/protocol/repository"
|
||||||
"StoreBackEnd/pkg/protocol/rules/readers"
|
"StoreBackEnd/pkg/protocol/rules/readers"
|
||||||
"StoreBackEnd/pkg/protocol/rules/writers"
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||||
|
"StoreBackEnd/pkg/utils"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,6 +16,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
utils.NetworkLister() // TODO REMOVE
|
||||||
println("StoreBackEnd started !")
|
println("StoreBackEnd started !")
|
||||||
|
|
||||||
// Loading App config
|
// Loading App config
|
||||||
@ -24,8 +26,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Multicast Address : " + appConfig.MulticastAddress)
|
println(" - Multicast Network Interface : " + appConfig.MulticastNetworkInterface)
|
||||||
println("StoreBacked Domain : " + appConfig.Domain)
|
println(" - Multicast Address : " + appConfig.MulticastAddress)
|
||||||
|
println(" - StoreBacked Domain : " + appConfig.Domain)
|
||||||
|
|
||||||
protocolRepository := repository.CreateProtocolRepository()
|
protocolRepository := repository.CreateProtocolRepository()
|
||||||
|
|
||||||
@ -56,7 +59,9 @@ func main() {
|
|||||||
protocolRepository.AddReader(&sendFileRule)
|
protocolRepository.AddReader(&sendFileRule)
|
||||||
|
|
||||||
// Create a Multicast Client & run it
|
// Create a Multicast Client & run it
|
||||||
multicast := network.CreateClientMulticast(appConfig.MulticastAddress, appConfig.Domain, appConfig.UnicastPort, time.Duration(appConfig.MulticastSecond), protocolRepository)
|
multicast := network.CreateClientMulticast(
|
||||||
|
appConfig.MulticastNetworkInterface, 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}
|
||||||
|
@ -2,15 +2,18 @@ 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
|
// MulticastNetworkInterface
|
||||||
|
MulticastNetworkInterface string `json:"multicastNetworkInterface"`
|
||||||
|
|
||||||
|
// MulticastAddress Contient l'adresse multicast du FileFrontEnd
|
||||||
MulticastAddress string `json:"multicastAddress"`
|
MulticastAddress string `json:"multicastAddress"`
|
||||||
|
|
||||||
// multicastSecond Contient le nombre de seconde entre chaque annonce
|
// MulticastSecond Contient le nombre de seconde entre chaque annonce
|
||||||
MulticastSecond int `json:"multicastSecond"`
|
MulticastSecond int `json:"multicastSecond"`
|
||||||
|
|
||||||
// domain Domain du StoreBackEnd
|
// Domain du StoreBackEnd
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
|
|
||||||
// unicastPort Contient le port unicast auquel le FileFrontEnd se connecte
|
// UnicastPort Contient le port unicast auquel le FileFrontEnd se connecte
|
||||||
UnicastPort int `json:"unicastPort"`
|
UnicastPort int `json:"unicastPort"`
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 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, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
|
func CreateClientMulticast(netInterface string, address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
|
||||||
return ClientMulticast{
|
return ClientMulticast{
|
||||||
|
netInter: netInterface,
|
||||||
address: address,
|
address: address,
|
||||||
domain: domain,
|
domain: domain,
|
||||||
port: port,
|
port: port,
|
||||||
@ -22,30 +23,37 @@ func CreateClientMulticast(address string, domain string, port int, second time.
|
|||||||
// 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 {
|
||||||
|
// netInter Interface réseaux multicast
|
||||||
|
netInter string
|
||||||
// address Adresse de multicast
|
// address Adresse de multicast
|
||||||
address string
|
address string
|
||||||
|
|
||||||
// address Domain de du StoreBackEnd
|
// address Domain de du StoreBackEnd
|
||||||
domain string
|
domain string
|
||||||
|
|
||||||
// port Port de connexion en unicast
|
// port Port de connexion en unicast
|
||||||
port int
|
port int
|
||||||
|
|
||||||
// second Temps en seconde entre chaque ping
|
// second Temps en seconde entre chaque ping
|
||||||
second time.Duration
|
second time.Duration
|
||||||
|
// repository de protocol permettant de
|
||||||
// Repository de protocol permettant de
|
|
||||||
repository *repository.ProtocolRepository
|
repository *repository.ProtocolRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run Cette méthode démarre une commmunication multicast
|
// Run Cette méthode démarre une commmunication multicast
|
||||||
func (client ClientMulticast) Run() {
|
func (client ClientMulticast) Run() {
|
||||||
addr, failedRA := client.ResolveAddr()
|
// Resolve multicast addr
|
||||||
|
rAddr, failedRA := client.ResolveAddr()
|
||||||
if failedRA {
|
if failedRA {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
con, failedDU := client.DialUdp(addr)
|
// Resolve interface addr
|
||||||
|
lAddr, failedRIA := ResolveInterfaceAddr(client.netInter)
|
||||||
|
if failedRIA {
|
||||||
|
println("finish")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init UDP server flux
|
||||||
|
con, failedDU := client.DialUdp(lAddr, rAddr)
|
||||||
if failedDU {
|
if failedDU {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -60,23 +68,36 @@ func (client ClientMulticast) Run() {
|
|||||||
_, _ = con.Write([]byte(cmd))
|
_, _ = con.Write([]byte(cmd))
|
||||||
time.Sleep(time.Second * client.second)
|
time.Sleep(time.Second * client.second)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveAddr Permet de résoude l'addresse
|
// ResolveAddr Permet de résoude l'addresse multicast
|
||||||
func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
|
func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
|
||||||
addr, errResUdp := net.ResolveUDPAddr("udp", client.address)
|
addr, err := net.ResolveUDPAddr("udp", client.address)
|
||||||
|
if err != nil {
|
||||||
if errResUdp != nil {
|
println(err.Error())
|
||||||
println(errResUdp.Error())
|
|
||||||
return nil, true
|
return nil, true
|
||||||
}
|
}
|
||||||
return addr, false
|
return addr, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResolveInterfaceAddr Resolves the network interface address.
|
||||||
|
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { // TODO Work in progress ! do not touch
|
||||||
|
i, err := net.InterfaceByName(inter)
|
||||||
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
|
return nil, true
|
||||||
|
}
|
||||||
|
addrs, _ := i.Addrs()
|
||||||
|
println("INFO ABOUT THE SELECTED INTERFACE (TEMPORAIRE)")
|
||||||
|
for a, v := range addrs {
|
||||||
|
println(a, v.(*net.IPNet).String())
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
// DialUdp Ouvre une connection UDP
|
// DialUdp Ouvre une connection UDP
|
||||||
func (client ClientMulticast) DialUdp(addr *net.UDPAddr) (*net.UDPConn, bool) {
|
func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) {
|
||||||
con, errDial := net.DialUDP("udp", nil, addr)
|
con, errDial := net.DialUDP("udp", nil, rAddr)
|
||||||
if errDial != nil {
|
if errDial != nil {
|
||||||
println(errDial.Error())
|
println(errDial.Error())
|
||||||
return nil, true
|
return nil, true
|
||||||
|
24
pkg/utils/NetworkLister.go
Normal file
24
pkg/utils/NetworkLister.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NetworkLister() {
|
||||||
|
// Retrieve Interfaces
|
||||||
|
inter, err := net.Interfaces()
|
||||||
|
|
||||||
|
// Process errors
|
||||||
|
if err != nil {
|
||||||
|
println("[ERROR] An error occurred : " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display items
|
||||||
|
println("\n\nNetwork interface list :")
|
||||||
|
for i, val := range inter {
|
||||||
|
fmt.Printf("%d. %s\n", i, val.Name)
|
||||||
|
}
|
||||||
|
print("\n")
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"multicastNetworkInterface" : "Wi-Fi",
|
||||||
"multicastAddress" : "226.66.66.1:15502",
|
"multicastAddress" : "226.66.66.1:15502",
|
||||||
"multicastSecond" : 10,
|
"multicastSecond" : 10,
|
||||||
"domain" : "lightcontainerSB01",
|
"domain" : "lightcontainerSB01",
|
||||||
|
Loading…
Reference in New Issue
Block a user