]> code.octet-stream.net Git - broadcaster/blob - broadcaster-server/status.go
Add licence, etc.
[broadcaster] / broadcaster-server / status.go
1 package main
2
3 import (
4 "code.octet-stream.net/broadcaster/internal/protocol"
5 "sync"
6 )
7
8 type ServerStatus struct {
9 statuses map[int]protocol.StatusMessage
10 statusesMutex sync.Mutex
11 changeWait chan bool
12 }
13
14 var status ServerStatus
15
16 func InitServerStatus() {
17 status = ServerStatus{
18 statuses: make(map[int]protocol.StatusMessage),
19 changeWait: make(chan bool),
20 }
21 }
22
23 func (s *ServerStatus) MergeStatus(radioId int, status protocol.StatusMessage) {
24 s.statusesMutex.Lock()
25 defer s.statusesMutex.Unlock()
26 s.statuses[radioId] = status
27 s.TriggerChange()
28 }
29
30 func (s *ServerStatus) RadioDisconnected(radioId int) {
31 s.statusesMutex.Lock()
32 defer s.statusesMutex.Unlock()
33 delete(s.statuses, radioId)
34 s.TriggerChange()
35 }
36
37 func (s *ServerStatus) TriggerChange() {
38 close(s.changeWait)
39 s.changeWait = make(chan bool)
40 }
41
42 func (s *ServerStatus) Statuses() map[int]protocol.StatusMessage {
43 s.statusesMutex.Lock()
44 defer s.statusesMutex.Unlock()
45 c := make(map[int]protocol.StatusMessage)
46 for k, v := range s.statuses {
47 c[k] = v
48 }
49 return c
50 }
51
52 func (s *ServerStatus) ChangeChannel() chan bool {
53 return s.changeWait
54 }