2022-02-15 11:07:16 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-03-05 17:21:46 +01:00
|
|
|
"StoreBackEnd/pkg/protocol/repository"
|
|
|
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
2022-02-26 18:08:52 +01:00
|
|
|
"fmt"
|
2022-02-15 11:07:16 +01:00
|
|
|
"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 {
|
2022-02-15 11:07:16 +01:00
|
|
|
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,
|
2022-02-15 11:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-02-15 11:07:16 +01:00
|
|
|
address string
|
2022-03-05 17:21:46 +01:00
|
|
|
// address Domain de du StoreBackEnd
|
2022-02-26 18:08:52 +01:00
|
|
|
domain string
|
|
|
|
// port Port de connexion en unicast
|
|
|
|
port int
|
2022-02-15 11:07:16 +01:00
|
|
|
// 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
|
2022-02-15 11:07:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run Cette méthode démarre une commmunication multicast
|
2022-03-05 18:32:27 +01:00
|
|
|
func (client ClientMulticast) Run() {
|
2022-03-08 14:58:41 +01:00
|
|
|
// Resolve multicast addr
|
|
|
|
rAddr, failedRA := client.ResolveAddr()
|
2022-03-08 10:25:00 +01:00
|
|
|
if failedRA {
|
2022-02-15 11:07:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:58:41 +01:00
|
|
|
// Resolve interface addr
|
|
|
|
lAddr, failedRIA := ResolveInterfaceAddr(client.netInter)
|
|
|
|
if failedRIA {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init UDP server flux
|
|
|
|
con, failedDU := client.DialUdp(lAddr, rAddr)
|
2022-03-08 10:25:00 +01:00
|
|
|
if failedDU {
|
2022-02-15 11:07:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-12 15:57:27 +01:00
|
|
|
writerResult := client.repository.ExecuteWriter(writers.HelloRulePrefix, client.domain, fmt.Sprintf("%d", client.port))
|
|
|
|
if writerResult == nil {
|
|
|
|
println("[ClientMulticast] Hello rule isn't correct")
|
2022-02-22 09:35:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:07:16 +01:00
|
|
|
for {
|
2022-03-12 15:57:27 +01:00
|
|
|
_, _ = con.Write([]byte(writerResult.Cmd))
|
2022-03-05 18:32:27 +01:00
|
|
|
time.Sleep(time.Second * client.second)
|
2022-02-15 11:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:58:41 +01:00
|
|
|
// ResolveAddr Permet de résoude l'addresse multicast
|
2022-03-05 18:32:27 +01:00
|
|
|
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())
|
2022-02-15 11:07:16 +01:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
return addr, false
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:58:41 +01:00
|
|
|
// ResolveInterfaceAddr Resolves the network interface address.
|
2022-03-08 19:41:54 +01:00
|
|
|
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) {
|
2022-03-12 19:12:19 +01:00
|
|
|
//ief, _ := net.InterfaceByName(inter)
|
|
|
|
//addrs, _ := ief.Addrs()
|
2022-03-08 19:41:54 +01:00
|
|
|
//for _, addr := range addrs {
|
2022-03-12 19:12:19 +01:00
|
|
|
// if utils.IsIPv4(addr.String()) {
|
|
|
|
// println("Selected IP", addr.String())
|
2022-03-12 20:36:12 +01:00
|
|
|
// lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:15502")
|
2022-03-12 19:12:19 +01:00
|
|
|
// return lAddr, false
|
2022-03-08 19:41:54 +01:00
|
|
|
// }
|
|
|
|
//}
|
2022-03-13 11:12:40 +01:00
|
|
|
return nil, false
|
2022-03-08 14:58:41 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:07:16 +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) {
|
2022-03-12 20:36:12 +01:00
|
|
|
con, errDial := net.DialUDP("udp", nil, rAddr)
|
2022-02-15 11:07:16 +01:00
|
|
|
if errDial != nil {
|
|
|
|
println(errDial.Error())
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
return con, false
|
|
|
|
}
|