Browse Source

add ship refresh loop

reid 2 years ago
parent
commit
522d6c8e4e
1 changed files with 28 additions and 3 deletions
  1. 28 3
      broadcast/broadcast.go

+ 28 - 3
broadcast/broadcast.go

@@ -24,6 +24,7 @@ var (
 	logger           = slog.New(slog.NewJSONHandler(os.Stdout, nil))
 	clients          = make(map[*websocket.Conn]bool)
 	hostInfoInterval = 3 * time.Second // how often we refresh system info
+	shipInfoInterval = 3 * time.Second // how often we refresh ship info
 	broadcastState   structs.AuthBroadcast
 	unauthState      structs.UnauthBroadcast
 	mu               sync.RWMutex // synchronize access to broadcastState
@@ -110,8 +111,9 @@ func bootstrapBroadcastState(config structs.SysConfig) (structs.AuthBroadcast, e
 		errmsg := fmt.Sprintf("Error updating broadcast state:", err)
 		logger.Error(errmsg)
 	}
-	// start looping host info
-	go HostStatusLoop()
+	// start looping info refreshes
+	go hostStatusLoop()
+	go shipStatusLoop()
 	// return the boostrapped result
 	res = GetState()
 	return res, nil
@@ -349,7 +351,7 @@ func BroadcastToClients() error {
 }
 
 // refresh loop for host info
-func HostStatusLoop() {
+func hostStatusLoop() {
 	ticker := time.NewTicker(hostInfoInterval)
 	for {
 		select {
@@ -362,3 +364,26 @@ func HostStatusLoop() {
 		}
 	}
 }
+
+// refresh loop for ship info
+func shipStatusLoop() {
+	ticker := time.NewTicker(hostInfoInterval)
+	for {
+		select {
+		case <-ticker.C:
+			updates, err := constructPierInfo(piers)
+			if err != nil {
+				return res, err
+			}
+			// update broadcastState
+			err = UpdateBroadcastState(map[string]interface{}{
+				"Urbits": updates,
+			})
+			if err != nil {
+				errmsg := fmt.Sprintf("Unable to update ship state: %v", err)
+				logger.Error(errmsg)
+				return res, err
+			}
+		}
+	}
+}