Using Ping to explore the status

master 1.0.0b
Uriel Fanelli 2023-08-09 19:38:57 +02:00
parent 58c9527f19
commit 6c4f09f4cb
2 changed files with 20 additions and 17 deletions

View File

@ -60,21 +60,17 @@ func (b *AbstractBridge) configureIpAndBridgeUp() {
// other server
brIp, brIpNet, err := net.ParseCIDR(b.BridgeIpCIDR)
if err != nil {
log.Println(err.Error())
}
if doesIpExists(brIp.String()) {
log.Println("Cannot take this IP, it exists already: " + brIp.String())
return
log.Println("Error parsing CIDR: ", err.Error())
}
br, err := tenus.NewLinkFrom(b.ExistingInterface)
if err != nil {
log.Println(err.Error())
log.Println("Error creating Ethernet Alias: ", err.Error())
log.Println("Problematic interface: ", b.ExistingInterface)
}
if err := br.SetLinkIp(brIp, brIpNet); err != nil {
log.Println(err.Error())
log.Println("Error setting UP the IP: ", err.Error())
}
}
@ -92,20 +88,22 @@ func (b *AbstractBridge) removeIPandBridgeInt() {
br, err := tenus.NewLinkFrom(b.ExistingInterface)
if err != nil {
log.Println(err.Error())
log.Println("Error creating Ethernet Handle: ", err.Error())
log.Println("Problematic interface: ", b.ExistingInterface)
}
brIp, brIpNet, err := net.ParseCIDR(b.BridgeIpCIDR)
if err != nil {
log.Println(err.Error())
log.Println("Error parsing CIDR: ", err.Error())
}
if err := br.UnsetLinkIp(brIp, brIpNet); err != nil {
log.Println(err.Error())
log.Println("Error setting DOWN the IP: ", err.Error())
}
}
func doesIpExists(bridgeip string) bool {
func isActive(bridgeip string) bool {
pinger, err := ping.NewPinger(bridgeip)
if err != nil {

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"net"
"slices"
"sort"
"strconv"
@ -84,22 +85,26 @@ func (b *AbstractBridge) HierarchyReLocator(entity AbstractMulticast) {
entity.AddUniqueAndSort(brdNumber)
}
brIp, _, err := net.ParseCIDR(b.BridgeIpCIDR)
if err != nil {
log.Println(err.Error())
}
// finished feeding the new number
// if Alpha:
if entity.IsAlpha(b.hIerarchyNumber) {
if b.IsActive {
if isActive(brIp.String()) {
log.Println("Still ALPHA. This is ok.")
b.IsActive = true // you never know. Better to reiterate.
} else {
log.Println("I'm the new ALPHA! Get out my path, losers!")
b.configureIpAndBridgeUp()
b.IsActive = true
log.Println("Ip is active: ", isActive(brIp.String()))
}
} else {
log.Println("GULP! There is a bigger one, better descalating")
if b.IsActive {
if isActive(brIp.String()) {
b.removeIPandBridgeInt()
b.IsActive = false
}
}