StoreBackEnd/pkg/network/ClientMulticast.go

104 lines
2.7 KiB
Go
Raw Normal View History

package network
import (
"StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils"
2022-02-26 18:08:52 +01:00
"fmt"
"net"
"time"
)
// CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast
2022-03-08 14:58:41 +01:00
func CreateClientMulticast(netInterface string, address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
return ClientMulticast{
2022-03-08 14:58:41 +01:00
netInter: netInterface,
2022-02-22 08:29:11 +01:00
address: address,
2022-02-26 18:08:52 +01:00
domain: domain,
port: port,
2022-02-22 08:29:11 +01:00
second: second,
repository: repository,
}
}
// ClientMulticast Cette structure représente une communication en multicast.
// TODO : Prévoir une fermeture de la connection (con.Close())
type ClientMulticast struct {
2022-03-08 14:58:41 +01:00
// netInter Interface réseaux multicast
netInter string
2022-02-26 18:08:52 +01:00
// address Adresse de multicast
address string
// address Domain de du StoreBackEnd
2022-02-26 18:08:52 +01:00
domain string
// port Port de connexion en unicast
port int
// second Temps en seconde entre chaque ping
second time.Duration
2022-03-08 14:58:41 +01:00
// repository de protocol permettant de
2022-02-22 08:29:11 +01:00
repository *repository.ProtocolRepository
}
// Run Cette méthode démarre une commmunication multicast
func (client ClientMulticast) Run() {
2022-03-08 14:58:41 +01:00
// Resolve multicast addr
rAddr, failedRA := client.ResolveAddr()
if failedRA {
return
}
2022-03-08 14:58:41 +01:00
// Resolve interface addr
lAddr, failedRIA := ResolveInterfaceAddr(client.netInter)
if failedRIA {
println("Error : No IPv4 found in interface")
2022-03-08 14:58:41 +01:00
return
}
// Init UDP server flux
con, failedDU := client.DialUdp(lAddr, rAddr)
if failedDU {
return
}
writerResult := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port))
if writerResult == nil {
println("[ClientMulticast] Hello rule isn't correct")
return
}
for {
_, _ = con.Write([]byte(writerResult.Cmd))
time.Sleep(time.Second * client.second)
}
}
2022-03-08 14:58:41 +01:00
// ResolveAddr Permet de résoude l'addresse multicast
func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
2022-03-08 14:58:41 +01:00
addr, err := net.ResolveUDPAddr("udp", client.address)
if err != nil {
println(err.Error())
return nil, true
}
return addr, false
}
2022-03-08 14:58:41 +01:00
// ResolveInterfaceAddr Resolves the network interface address.
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) {
ip, err := utils.RetrieveIPv4FromInterface(inter)
if err != nil {
println("Error : " + err.Error())
return nil, true
}
println("Selected IP : " + ip.String())
return &net.UDPAddr{IP: ip}, false
2022-03-08 14:58:41 +01:00
}
// DialUdp Ouvre une connection UDP
2022-03-08 14:58:41 +01:00
func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) {
con, errDial := net.DialUDP("udp", lAddr, rAddr)
if errDial != nil {
println(errDial.Error())
return nil, true
}
return con, false
}