2022-02-15 11:07:16 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-02-23 13:07:48 +01:00
|
|
|
"StorBackEnd/pkg/protocol/repository"
|
|
|
|
"StorBackEnd/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-02-26 18:08:52 +01:00
|
|
|
func CreateClientMulticast(address string, domain string, port int, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast {
|
2022-02-15 11:07:16 +01:00
|
|
|
return ClientMulticast{
|
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-02-26 18:08:52 +01:00
|
|
|
// address Adresse de multicast
|
2022-02-15 11:07:16 +01:00
|
|
|
address string
|
|
|
|
|
2022-02-26 18:08:52 +01:00
|
|
|
// address Domain de du StorBackEnd
|
|
|
|
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-02-22 08:29:11 +01:00
|
|
|
|
|
|
|
// Repository de protocol permettant de
|
|
|
|
repository *repository.ProtocolRepository
|
2022-02-15 11:07:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run Cette méthode démarre une commmunication multicast
|
|
|
|
func (cMult ClientMulticast) Run() {
|
|
|
|
addr, done := cMult.ResolveAddr()
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
con, done2 := cMult.DialUdp(addr)
|
|
|
|
if done2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:08:52 +01:00
|
|
|
cmd, correct := cMult.repository.ExecuteWriter(writers.HelloRuleName, cMult.domain, fmt.Sprintf("%d", cMult.port))
|
2022-02-22 09:35:44 +01:00
|
|
|
if !correct {
|
2022-02-22 10:03:52 +01:00
|
|
|
println("[ClientMulticast] Hello rule isn't correct (" + cmd + ")")
|
2022-02-22 09:35:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:07:16 +01:00
|
|
|
for {
|
2022-02-22 09:35:44 +01:00
|
|
|
con.Write([]byte(cmd))
|
2022-02-15 11:07:16 +01:00
|
|
|
time.Sleep(time.Second * cMult.second)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveAddr Permet de résoude l'addresse
|
|
|
|
func (cMult ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
|
|
|
|
addr, errResUdp := net.ResolveUDPAddr("udp", cMult.address)
|
|
|
|
|
|
|
|
if errResUdp != nil {
|
|
|
|
println(errResUdp.Error())
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialUdp Ouvre une connection UDP
|
|
|
|
func (cMult ClientMulticast) DialUdp(addr *net.UDPAddr) (*net.UDPConn, bool) {
|
|
|
|
con, errDial := net.DialUDP("udp", nil, addr)
|
|
|
|
if errDial != nil {
|
|
|
|
println(errDial.Error())
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
return con, false
|
|
|
|
}
|