From 3b151ac2c0e09ec777f552515f0798451183e024 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Wed, 7 Feb 2024 12:20:34 -0400 Subject: [PATCH] periodic on and off for mdns server (#213) * periodic on and off for mdns server * changed the handling of config file in mdns * Update mdns.go --- wap/cmd/main.go | 75 +++++++++++++++++++++++++++++------- wap/cmd/mdns/mdns.go | 83 ++++++++++++++++++++++++++++------------ wap/pkg/server/server.go | 8 ++-- 3 files changed, 124 insertions(+), 42 deletions(-) diff --git a/wap/cmd/main.go b/wap/cmd/main.go index db5f30bf..7267efaf 100644 --- a/wap/cmd/main.go +++ b/wap/cmd/main.go @@ -175,9 +175,42 @@ func checkConfigExists() bool { } } +func handleServerLifecycle(ctx context.Context, serverControl chan bool) { + var server *mdns.MDNSServer + var serverMutex sync.Mutex + + for { + select { + case start := <-serverControl: + serverMutex.Lock() + if start { + if server == nil { + // Start the server + server = mdns.StartServer(ctx, 8080) // Adjust port as necessary + log.Info("mDNS server started") + } + } else { + if server != nil { + // Stop the server + server.Shutdown() + server = nil + log.Info("mDNS server stopped") + } + } + serverMutex.Unlock() + case <-ctx.Done(): + return + } + } +} + // handleAppState monitors the application state and starts/stops services as needed. -func handleAppState(ctx context.Context, isConnected bool, stopServer chan struct{}, mdnsServer **mdns.MDNSServer) { +func handleAppState(ctx context.Context, isConnected bool, stopServer chan struct{}) { log.Info("handleAppState is called") + // Load the config once at the start + if err := mdns.LoadConfig(); err != nil { + log.Fatal("Failed to load mdns configuration.") + } currentState := atomic.LoadInt32(¤tIsConnected) newState := int32(0) @@ -243,13 +276,13 @@ func handleAppState(ctx context.Context, isConnected bool, stopServer chan struc log.Info("Access point already enabled on startup") } } - if *mdnsServer != nil { + /*if *mdnsServer != nil { // Shutdown existing mDNS server before state change (*mdnsServer).Shutdown() *mdnsServer = nil } log.Info("starting mDNS server.") - *mdnsServer = mdns.StartServer(ctx, 8080) // start the mDNS server + *mdnsServer = mdns.StartServer(ctx, 8080) // start the mDNS server*/ atomic.StoreInt32(¤tIsConnected, int32(newState)) } else { log.Info("handleAppState is called but no action is needed") @@ -266,7 +299,6 @@ func main() { } else { log.Info("Successfully checked and set version info") } - var mdnsServer *mdns.MDNSServer = nil serverCloser := make(chan io.Closer, 1) stopServer := make(chan struct{}, 1) @@ -284,18 +316,35 @@ func main() { configExists := checkConfigExists() log.Info("Waiting for the system to connect to Wi-Fi") - handleAppState(ctx, isConnected, stopServer, &mdnsServer) + handleAppState(ctx, isConnected, stopServer) log.Infow("called handleAppState with ", isConnected) - // Start the server in a separate goroutine + //Start a periodic mdns server + serverControl := make(chan bool) + go handleServerLifecycle(ctx, serverControl) + + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + + go func() { + for range ticker.C { + // Toggle server state + serverControl <- false // Stop the server + time.Sleep(1 * time.Second) // Wait a bit before restarting + serverControl <- true // Start the server + } + }() + //end of mdns server handling + + // Start the http server in a separate goroutine go func() { - mdnsRestartCh := make(chan bool, 1) + connectedCh := make(chan bool, 1) serverMutex.Lock() if currentServer != nil { currentServer.Close() currentServer = nil } - closer := server.Serve(blox.BloxCommandInitOnly, "", "", mdnsRestartCh) + closer := server.Serve(blox.BloxCommandInitOnly, "", "", connectedCh) currentServer = closer serverMutex.Unlock() serverCloser <- closer @@ -311,9 +360,9 @@ func main() { isHotspotStarted = false serverMutex.Unlock() return // Exit the goroutine - case isConnected = <-mdnsRestartCh: + case isConnected = <-connectedCh: log.Infow("called handleAppState in go routine1 with ", isConnected) - handleAppState(ctx, isConnected, stopServer, &mdnsServer) + handleAppState(ctx, isConnected, stopServer) } } }() @@ -340,7 +389,7 @@ func main() { log.Info("Waiting for the system to connect to saved Wi-Fi periodic check") if wifi.CheckIfIsConnected(ctx, "") == nil { isConnected = true - handleAppState(ctx, isConnected, stopServer, &mdnsServer) + handleAppState(ctx, isConnected, stopServer) ticker2.Stop() break loop2 } @@ -358,7 +407,7 @@ func main() { } else { log.Info("Not connected to a wifi network") isConnected = false - handleAppState(ctx, isConnected, stopServer, &mdnsServer) + handleAppState(ctx, isConnected, stopServer) } log.Info("Access point enabled on startup") } @@ -376,7 +425,7 @@ func main() { signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) <-sig // Before shutting down, make sure to set the appropriate state - handleAppState(ctx, isConnected, stopServer, &mdnsServer) + handleAppState(ctx, isConnected, stopServer) log.Info("Shutting down wap") // Close the server diff --git a/wap/cmd/mdns/mdns.go b/wap/cmd/mdns/mdns.go index 34f4d35b..e27c3118 100644 --- a/wap/cmd/mdns/mdns.go +++ b/wap/cmd/mdns/mdns.go @@ -30,33 +30,44 @@ type Config struct { // include other fields as needed } -func createInfo() []string { - - bloxPeerIdString := "NA" - poolName := "NA" - authorizer := "NA" - hardwareID := "NA" +type Meta struct { + BloxPeerIdString string + PoolName string + Authorizer string + HardwareID string +} - hardwareID, err := wifi.GetHardwareID() - if err != nil { - log.Errorw("GetHardwareID failed", "err", err) +var globalConfig *Meta // To store the loaded config globally +// Load and parse the config file, then store it globally +func LoadConfig() error { + if _, err := os.Stat(config.FULA_CONFIG_PATH); os.IsNotExist(err) { + log.Errorf("Config file does not exist: %s", config.FULA_CONFIG_PATH) + return err } data, err := os.ReadFile(config.FULA_CONFIG_PATH) if err != nil { log.Errorw("ReadFile failed", "err", err) + return err } - // create a new Config - var config Config - - // unmarshal the YAML data into the config - if err := yaml.Unmarshal(data, &config); err != nil { + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { log.Errorw("Unmarshal failed", "err", err) + return err + } + bloxPeerIdString := "NA" + poolName := "NA" + authorizer := "NA" + hardwareID, err := wifi.GetHardwareID() + if err != nil { + log.Errorw("GetHardwareID failed", "err", err) + hardwareID = "NA" } - authorizer = config.Authorizer - km, err := base64.StdEncoding.DecodeString(config.Identity) + authorizer = cfg.Authorizer + + km, err := base64.StdEncoding.DecodeString(cfg.Identity) if err != nil { log.Errorw("DecodeString failed", "err", err) } else { @@ -73,15 +84,36 @@ func createInfo() []string { } } } - - poolName = config.PoolName + poolName = cfg.PoolName // Create a slice with the required information in key=value format + infoSlice := Meta{ + BloxPeerIdString: bloxPeerIdString, + PoolName: poolName, + Authorizer: authorizer, + HardwareID: hardwareID, + } + + log.Infow("mdns info loaded from config file", "infoSlice", infoSlice) + + globalConfig = &infoSlice // Store the config globally + return nil +} + +// Utilize the global config to create metadata info +func createInfo() []string { + if globalConfig == nil { + log.Error("Config not loaded") + return nil + } + + // Use the loaded globalConfig here to create your metadata + // Example: infoSlice := []string{ - "bloxPeerIdString=" + bloxPeerIdString, - "poolName=" + poolName, - "authorizer=" + authorizer, - "hardwareID=" + hardwareID, + "bloxPeerIdString=" + globalConfig.BloxPeerIdString, // Just an example, adjust according to actual data structure + "poolName=" + globalConfig.PoolName, + "authorizer=" + globalConfig.Authorizer, + "hardwareID=" + globalConfig.HardwareID, // Assuming you handle hardwareID differently } return infoSlice @@ -94,7 +126,7 @@ func StartServer(ctx context.Context, port int) *MDNSServer { log.Errorw("NewMDNSServer failed", "err", err) return nil } - log.Info("NewZeroConfService server started") + log.Debug("NewZeroConfService server started") // Listen for context done signal to close the server go func() { @@ -107,7 +139,7 @@ func StartServer(ctx context.Context, port int) *MDNSServer { func NewZeroConfService(port int) (*MDNSServer, error) { meta := createInfo() - log.Infow("mdns meta created", "meta", meta) + log.Debugw("mdns meta created", "meta", meta) service, err := zeroconf.Register( "fulatower", // service instance name @@ -122,7 +154,8 @@ func NewZeroConfService(port int) (*MDNSServer, error) { log.Errorw("zeroconf.Register failed", "err", err) return nil, err } - log.Info("NewZeroConfService registered") + log.Debug("NewZeroConfService registered") + service.TTL(2) return &MDNSServer{server: service}, nil } diff --git a/wap/pkg/server/server.go b/wap/pkg/server/server.go index 72ed80de..bf652796 100644 --- a/wap/pkg/server/server.go +++ b/wap/pkg/server/server.go @@ -277,7 +277,7 @@ func listWifiHandler(w http.ResponseWriter, r *http.Request) { } } -func connectWifiHandler(w http.ResponseWriter, r *http.Request, mdnsRestartCh chan bool) { +func connectWifiHandler(w http.ResponseWriter, r *http.Request, connectedCh chan bool) { if r.URL.Path != "/wifi/connect" { http.Error(w, "404 not found.", http.StatusNotFound) return @@ -313,7 +313,7 @@ func connectWifiHandler(w http.ResponseWriter, r *http.Request, mdnsRestartCh ch return } log.Info("wifi connected. Calling mdns restart") - mdnsRestartCh <- true + connectedCh <- true w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) @@ -506,7 +506,7 @@ func getNonLoopbackIP() (string, error) { // This function accepts an ip and port that it runs the webserver on. Default is 10.42.0.1:3500 and if it fails reverts to 0.0.0.0:3500 // - /wifi/list endpoint: shows the list of available wifis -func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip string, port string, mdnsRestartCh chan bool) io.Closer { +func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip string, port string, connectedCh chan bool) io.Closer { ctx := context.Background() peerFunction = peerFn mux := http.NewServeMux() @@ -514,7 +514,7 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip mux.HandleFunc("/wifi/list", listWifiHandler) mux.HandleFunc("/wifi/status", wifiStatusHandler) mux.HandleFunc("/wifi/connect", func(w http.ResponseWriter, r *http.Request) { - connectWifiHandler(w, r, mdnsRestartCh) + connectWifiHandler(w, r, connectedCh) }) mux.HandleFunc("/ap/enable", enableAccessPointHandler) mux.HandleFunc("/ap/disable", disableAccessPointHandler)