From 51defc39ac7fd932972c4a5f35e858e3c85ff446 Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 8 Mar 2022 14:58:41 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20syst=C3=A8me=20d'interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 11 +++++-- pkg/config/AppConfig.go | 11 ++++--- pkg/network/ClientMulticast.go | 53 ++++++++++++++++++++++++---------- pkg/utils/NetworkLister.go | 24 +++++++++++++++ resources/AppConfig.json | 1 + 5 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 pkg/utils/NetworkLister.go diff --git a/cmd/main.go b/cmd/main.go index 9e76eee..7d68c63 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,6 +7,7 @@ import ( "StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/rules/readers" "StoreBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/utils" "time" ) @@ -15,6 +16,7 @@ const ( ) func main() { + utils.NetworkLister() // TODO REMOVE println("StoreBackEnd started !") // Loading App config @@ -24,8 +26,9 @@ func main() { return } - println("Multicast Address : " + appConfig.MulticastAddress) - println("StoreBacked Domain : " + appConfig.Domain) + println(" - Multicast Network Interface : " + appConfig.MulticastNetworkInterface) + println(" - Multicast Address : " + appConfig.MulticastAddress) + println(" - StoreBacked Domain : " + appConfig.Domain) protocolRepository := repository.CreateProtocolRepository() @@ -56,7 +59,9 @@ func main() { protocolRepository.AddReader(&sendFileRule) // 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() requestManager := managers.RequestManager{Repository: protocolRepository} diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 018a884..3777a54 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -2,15 +2,18 @@ package config // AppConfig Contient toute la configuration du server 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"` - // multicastSecond Contient le nombre de seconde entre chaque annonce + // MulticastSecond Contient le nombre de seconde entre chaque annonce MulticastSecond int `json:"multicastSecond"` - // domain Domain du StoreBackEnd + // Domain du StoreBackEnd 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"` } diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 2a39dd7..4ff5c0f 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -9,8 +9,9 @@ import ( ) // 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{ + netInter: netInterface, address: address, domain: domain, 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. // TODO : Prévoir une fermeture de la connection (con.Close()) type ClientMulticast struct { + // netInter Interface réseaux multicast + netInter string // address Adresse de multicast address string - // address Domain de du StoreBackEnd domain string - // port Port de connexion en unicast port int - // second Temps en seconde entre chaque ping second time.Duration - - // Repository de protocol permettant de + // repository de protocol permettant de repository *repository.ProtocolRepository } // Run Cette méthode démarre une commmunication multicast func (client ClientMulticast) Run() { - addr, failedRA := client.ResolveAddr() + // Resolve multicast addr + rAddr, failedRA := client.ResolveAddr() if failedRA { 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 { return } @@ -60,23 +68,36 @@ func (client ClientMulticast) Run() { _, _ = con.Write([]byte(cmd)) 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) { - addr, errResUdp := net.ResolveUDPAddr("udp", client.address) - - if errResUdp != nil { - println(errResUdp.Error()) + addr, err := net.ResolveUDPAddr("udp", client.address) + if err != nil { + println(err.Error()) return nil, true } 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 -func (client ClientMulticast) DialUdp(addr *net.UDPAddr) (*net.UDPConn, bool) { - con, errDial := net.DialUDP("udp", nil, addr) +func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { + con, errDial := net.DialUDP("udp", nil, rAddr) if errDial != nil { println(errDial.Error()) return nil, true diff --git a/pkg/utils/NetworkLister.go b/pkg/utils/NetworkLister.go new file mode 100644 index 0000000..3e0c0de --- /dev/null +++ b/pkg/utils/NetworkLister.go @@ -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") +} diff --git a/resources/AppConfig.json b/resources/AppConfig.json index 6908b10..16552c9 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -1,4 +1,5 @@ { + "multicastNetworkInterface" : "Wi-Fi", "multicastAddress" : "226.66.66.1:15502", "multicastSecond" : 10, "domain" : "lightcontainerSB01",