diff --git a/wap/cmd/main.go b/wap/cmd/main.go index 14ae89a..7267efa 100644 --- a/wap/cmd/main.go +++ b/wap/cmd/main.go @@ -207,6 +207,10 @@ func handleServerLifecycle(ctx context.Context, serverControl chan bool) { // handleAppState monitors the application state and starts/stops services as needed. 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) @@ -295,20 +299,6 @@ func main() { } else { log.Info("Successfully checked and set version info") } - 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 - } - }() serverCloser := make(chan io.Closer, 1) stopServer := make(chan struct{}, 1) @@ -329,7 +319,24 @@ func main() { 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() { connectedCh := make(chan bool, 1) serverMutex.Lock() diff --git a/wap/cmd/mdns/mdns.go b/wap/cmd/mdns/mdns.go index d42235e..65e576f 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 } - authorizer = config.Authorizer + bloxPeerIdString := "NA" + poolName := "NA" + authorizer := "NA" + hardwareID, err := wifi.GetHardwareID() + if err != nil { + log.Errorw("GetHardwareID failed", "err", err) + hardwareID = "NA" + } + + authorizer = cfg.Authorizer - km, err := base64.StdEncoding.DecodeString(config.Identity) + km, err := base64.StdEncoding.DecodeString(cfg.Identity) if err != nil { log.Errorw("DecodeString failed", "err", err) } else { @@ -73,15 +84,34 @@ 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, + } + + 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 @@ -123,7 +153,7 @@ func NewZeroConfService(port int) (*MDNSServer, error) { return nil, err } log.Info("NewZeroConfService registered") - service.TTL(5) + service.TTL(2) return &MDNSServer{server: service}, nil }