slice.Max has a bug, writing my own Max()

master
Uriel Fanelli 2023-08-14 14:18:51 +02:00
parent f1c46ed9f1
commit 636434a0cf
2 changed files with 16 additions and 25 deletions

View File

@ -165,19 +165,9 @@ func (b *AbstractBridge) IsAssigned() bool {
var ( var (
ief *net.Interface ief *net.Interface
addrs []net.Addr addrs []net.Addr
ipv4Addr net.IP
err error err error
bridgeip string
) )
brIp, _, err := net.ParseCIDR(b.BridgeIpCIDR)
if err != nil {
log.Println("IsActive : problem parsing the IP/CIDR: ", err.Error())
return false
} else {
bridgeip = brIp.String()
}
interfaceName := b.ExistingInterface interfaceName := b.ExistingInterface
if ief, err = net.InterfaceByName(interfaceName); err != nil { // get interface if ief, err = net.InterfaceByName(interfaceName); err != nil { // get interface
@ -196,20 +186,14 @@ func (b *AbstractBridge) IsAssigned() bool {
return false return false
} }
for _, addr := range addrs { // get ipv4 address
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
log.Printf("Error reading IPs for interface %s\n", interfaceName)
return false
}
}
for _, addr := range addrs { for _, addr := range addrs {
if addr.String() == bridgeip { log.Println("Address found is: ", addr.String())
log.Printf("Ip %s is assigned to interface %s", bridgeip, interfaceName) if addr.String() == b.BridgeIpCIDR {
log.Printf("Ip %s is assigned to interface %s", b.BridgeIpCIDR, interfaceName)
return true return true
} }
} }
log.Printf("Ip %s is NOT assigned to interface %s", bridgeip, interfaceName) log.Printf("Ip %s is NOT assigned to interface %s", b.BridgeIpCIDR, interfaceName)
return false return false
} }

View File

@ -22,9 +22,16 @@ func (mip *AbstractMulticast) AddUniqueAndSort(hier int64) {
func (mip *AbstractMulticast) IsAlpha(hier int64) bool { func (mip *AbstractMulticast) IsAlpha(hier int64) bool {
alpha := slices.Max(mip.HierarchyArray) slices.Sort(mip.HierarchyArray)
log.Println("Maximum element is :", alpha) indexMax := len(mip.HierarchyArray) - 1
if indexMax < 0 {
log.Println("Empty array , no elements")
return false
}
alpha := mip.HierarchyArray[indexMax]
log.Println("Maximum element is :", alpha)
log.Println("Array is :", mip.HierarchyArray)
return alpha == hier return alpha == hier
} }