128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
package network
|
|
|
|
import (
|
|
"StoreBackEnd/pkg/protocol/repository"
|
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// CreateClientMulticast Méthode de construction d'un instance de la stuct 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,
|
|
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 {
|
|
// 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 *repository.ProtocolRepository
|
|
}
|
|
|
|
// Run Cette méthode démarre une commmunication multicast
|
|
func (client ClientMulticast) Run() {
|
|
// Resolve multicast addr
|
|
rAddr, failedRA := client.ResolveAddr()
|
|
if failedRA {
|
|
return
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
cmd, correct := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port))
|
|
if !correct {
|
|
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")")
|
|
return
|
|
}
|
|
|
|
for {
|
|
_, _ = con.Write([]byte(cmd))
|
|
time.Sleep(time.Second * client.second)
|
|
}
|
|
}
|
|
|
|
// ResolveAddr Permet de résoude l'addresse multicast
|
|
func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
|
|
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) {
|
|
//i, err := net.InterfaceByName(inter)
|
|
//addrs, err := i.Addrs()
|
|
//println(addrs[0])
|
|
//addr, err := net.ResolveUDPAddr("udp", addrs[0].String())
|
|
//if err != nil {
|
|
// println(err.Error())
|
|
// return nil, true
|
|
//}
|
|
return nil, false
|
|
//var ipv4Addr net.IP
|
|
//ief, err := net.InterfaceByName(inter)
|
|
//if err != nil {
|
|
// println(err.Error())
|
|
// return nil, true
|
|
//}
|
|
//addrs, err := ief.Addrs()
|
|
//if err != nil {
|
|
// println(err.Error())
|
|
// return nil, true
|
|
//}
|
|
//for _, addr := range addrs {
|
|
// if ipv4Addr = addr.(*net.IPNet).IP; ipv4Addr != nil {
|
|
// break
|
|
// }
|
|
//}
|
|
//println(ipv4Addr.String())
|
|
//addr, err := net.ResolveUDPAddr("udp", ipv4Addr.String())
|
|
//if err != nil {
|
|
// println(err.Error())
|
|
// return nil, true
|
|
//}
|
|
//return addr, false
|
|
}
|
|
|
|
// DialUdp Ouvre une connection UDP
|
|
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
|
|
}
|
|
return con, false
|
|
}
|