Ajout système d'interface
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user