X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/c94fef11f43279165f39680fa0b0922c86702687..04483e823a42650816c2edbb276709145b9068db:/server/status.go?ds=inline diff --git a/server/status.go b/server/status.go index accbcc9..b2edd49 100644 --- a/server/status.go +++ b/server/status.go @@ -1,12 +1,14 @@ package main import ( - "code.octet-stream.net/broadcaster/protocol" + "code.octet-stream.net/broadcaster/internal/protocol" + "sync" ) type ServerStatus struct { - statuses map[int]protocol.StatusMessage - changeWait chan bool + statuses map[int]protocol.StatusMessage + statusesMutex sync.Mutex + changeWait chan bool } var status ServerStatus @@ -19,11 +21,15 @@ func InitServerStatus() { } func (s *ServerStatus) MergeStatus(radioId int, status protocol.StatusMessage) { + s.statusesMutex.Lock() + defer s.statusesMutex.Unlock() s.statuses[radioId] = status s.TriggerChange() } func (s *ServerStatus) RadioDisconnected(radioId int) { + s.statusesMutex.Lock() + defer s.statusesMutex.Unlock() delete(s.statuses, radioId) s.TriggerChange() } @@ -34,7 +40,13 @@ func (s *ServerStatus) TriggerChange() { } func (s *ServerStatus) Statuses() map[int]protocol.StatusMessage { - return s.statuses + s.statusesMutex.Lock() + defer s.statusesMutex.Unlock() + c := make(map[int]protocol.StatusMessage) + for k, v := range s.statuses { + c[k] = v + } + return c } func (s *ServerStatus) ChangeChannel() chan bool {