Skip to content

Commit

Permalink
periodic on and off for mdns server (#213)
Browse files Browse the repository at this point in the history
* periodic on and off for mdns server

* changed the handling of config file in mdns

* Update mdns.go
  • Loading branch information
ehsan6sha committed Feb 7, 2024
1 parent 5d49c67 commit 3b151ac
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 42 deletions.
75 changes: 62 additions & 13 deletions wap/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&currentIsConnected)
newState := int32(0)
Expand Down Expand Up @@ -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(&currentIsConnected, int32(newState))
} else {
log.Info("handleAppState is called but no action is needed")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
}
}()
Expand All @@ -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
}
Expand All @@ -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")
}
Expand All @@ -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
Expand Down
83 changes: 58 additions & 25 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
}
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 {
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions wap/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -506,15 +506,15 @@ 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()
mux.HandleFunc("/readiness", readinessHandler)
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)
Expand Down

0 comments on commit 3b151ac

Please sign in to comment.