Skip to content

Commit

Permalink
changed the handling of config file in mdns
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Feb 6, 2024
1 parent 1744e99 commit 7c8f410
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 38 deletions.
37 changes: 22 additions & 15 deletions wap/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&currentIsConnected)
newState := int32(0)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
76 changes: 53 additions & 23 deletions wap/cmd/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7c8f410

Please sign in to comment.