Compare commits

..

2 Commits

Author SHA1 Message Date
Uriel Fanelli e6e6b90201 Adding possibility to disable logs + fixes. 2023-08-10 21:09:17 +02:00
Uriel Fanelli 08f139c8d2 Made it asynchronous. 2023-08-10 20:39:26 +02:00
6 changed files with 35 additions and 12 deletions

View File

@ -24,7 +24,6 @@ To compile it , you need:
Configuration contains: Configuration contains:
``` ```
{
"MulticastConfig": { "MulticastConfig": {
"MIpAddr": "239.0.0.19", "MIpAddr": "239.0.0.19",
"MPort": "9898", "MPort": "9898",
@ -33,8 +32,8 @@ Configuration contains:
"InterfaceConfig": { "InterfaceConfig": {
"ExistingInterface": "eth0", "ExistingInterface": "eth0",
"BridgeIpCIDR": "10.0.1.1/24" "BridgeIpCIDR": "10.0.1.1/24"
} },
} "debug": false
``` ```
Where: Where:
@ -45,6 +44,7 @@ Where:
- ExistingInterface: the name of your ingress interface (eth0, eno0 , enps18, whatever your system is using. This may be different node by node) - ExistingInterface: the name of your ingress interface (eth0, eno0 , enps18, whatever your system is using. This may be different node by node)
- BridgeIpCidr: the IP address and mask you want to share among servers. - BridgeIpCidr: the IP address and mask you want to share among servers.
- wether you want debug logs or not.
# FAQ # FAQ

View File

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io"
"log" "log"
"os" "os"
) )
@ -10,11 +11,13 @@ type AbstractConfig struct {
MulticastConfig struct { MulticastConfig struct {
MIPAddr string `json:"MIpAddr"` MIPAddr string `json:"MIpAddr"`
MPort string `json:"MPort"` MPort string `json:"MPort"`
MaxDatagramSize int `json:"MaxDatagramSize"`
} `json:"MulticastConfig"` } `json:"MulticastConfig"`
InterfaceConfig struct { InterfaceConfig struct {
ExistingInterface string `json:"ExistingInterface"` ExistingInterface string `json:"ExistingInterface"`
BridgeIPCIDR string `json:"BridgeIpCIDR"` BridgeIPCIDR string `json:"BridgeIpCIDR"`
} `json:"InterfaceConfig"` } `json:"InterfaceConfig"`
Debug bool `json:"debug"`
} }
var a AbstractConfig var a AbstractConfig
@ -44,6 +47,10 @@ func init() {
ZoreideBridge.ExistingInterface = a.InterfaceConfig.ExistingInterface ZoreideBridge.ExistingInterface = a.InterfaceConfig.ExistingInterface
ZoreideBridge.IsActive = false ZoreideBridge.IsActive = false
if !a.Debug {
log.SetOutput(io.Discard)
}
log.Println("Inizialized Generic Config: ", a) log.Println("Inizialized Generic Config: ", a)
log.Println("Inizialized Interface Config: ", ZoreideBridge) log.Println("Inizialized Interface Config: ", ZoreideBridge)
log.Println("Inizialized Multicast Config: ", MulticastEntity) log.Println("Inizialized Multicast Config: ", MulticastEntity)

View File

@ -71,6 +71,8 @@ func (b *AbstractBridge) configureIpAndBridgeUp() {
if err := br.SetLinkIp(brIp, brIpNet); err != nil { if err := br.SetLinkIp(brIp, brIpNet); err != nil {
log.Println("Error setting UP the IP: ", err.Error()) log.Println("Error setting UP the IP: ", err.Error())
} else {
log.Printf("%s configured with %s\n", b.ExistingInterface, brIp.String())
} }
} }
@ -105,18 +107,22 @@ func (b *AbstractBridge) removeIPandBridgeInt() {
func isActive(bridgeip string) bool { func isActive(bridgeip string) bool {
log.Println("Check for active IP: ", bridgeip)
pinger, err := ping.NewPinger(bridgeip) pinger, err := ping.NewPinger(bridgeip)
if err != nil { if err != nil {
log.Println("Ping error: " + err.Error()) log.Println("Ping error: " + err.Error())
} }
// just in case it doesn't stops alone // just in case it doesn't stops alone
defer pinger.Stop() defer pinger.Stop()
pinger.Count = 5 pinger.Count = 3
pinger.Interval = time.Duration(10 * time.Millisecond) pinger.Interval = time.Duration(100 * time.Millisecond)
pinger.Timeout = time.Duration(1 * time.Second) pinger.Timeout = time.Duration(1 * time.Second)
pinger.Run() // blocks until finished pinger.Run() // blocks until finished
stats := pinger.Statistics() stats := pinger.Statistics()
log.Println("Ping results for: ", bridgeip)
log.Printf("%d packet sent, %d packet recv", pinger.Count, stats.PacketsRecv)
return stats.PacketsRecv == 5 return stats.PacketsRecv == pinger.Count
} }

View File

@ -116,11 +116,20 @@ func (b *AbstractBridge) WaitAndClean(entity AbstractMulticast) {
log.Println("Inizializing Escalator") log.Println("Inizializing Escalator")
for {
entity.AddUniqueAndSort(b.hIerarchyNumber) entity.AddUniqueAndSort(b.hIerarchyNumber)
brIp, _, err := net.ParseCIDR(b.BridgeIpCIDR)
if err != nil {
log.Println(err.Error())
}
for {
pollTime := len(entity.HierarchyArray) + 1 pollTime := len(entity.HierarchyArray) + 1
time.Sleep(time.Duration(pollTime) * time.Second) time.Sleep(time.Duration(pollTime) * time.Second)
// svuotare l'array // svuotare l'array e rifare le elezioni
if !isActive(string(brIp.String())) {
entity.HierarchyArray = entity.HierarchyArray[:0] entity.HierarchyArray = entity.HierarchyArray[:0]
} }
}
} }

View File

@ -7,5 +7,6 @@
"InterfaceConfig": { "InterfaceConfig": {
"ExistingInterface": "eth0", "ExistingInterface": "eth0",
"BridgeIpCIDR": "10.0.1.1/24" "BridgeIpCIDR": "10.0.1.1/24"
} },
"debug": false
} }