X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/c94fef11f43279165f39680fa0b0922c86702687..6f8503e889dc1f45eddd987ee44e6338712de4fe:/server/radio_sync.go?ds=inline diff --git a/server/radio_sync.go b/server/radio_sync.go index 3dfd4ed..66521f8 100644 --- a/server/radio_sync.go +++ b/server/radio_sync.go @@ -1,7 +1,7 @@ package main import ( - "code.octet-stream.net/broadcaster/protocol" + "code.octet-stream.net/broadcaster/internal/protocol" "encoding/json" "golang.org/x/net/websocket" "log" @@ -53,22 +53,11 @@ func RadioSync(ws *websocket.Conn) { radio = r log.Println("Radio authenticated:", radio.Name) isAuthenticated = true + commandRouter.AddWebsocket(r.Id, ws) + defer commandRouter.RemoveWebsocket(ws) go KeepFilesUpdated(ws) - - // send initial file message - err = sendFilesMessageToRadio(ws) - if err != nil { - return - } - go KeepPlaylistsUpdated(ws) - - // send initial playlists message - err = sendPlaylistsMessageToRadio(ws) - if err != nil { - return - } } if t == protocol.StatusType { @@ -79,9 +68,9 @@ func RadioSync(ws *websocket.Conn) { } } -func sendPlaylistsMessageToRadio(ws *websocket.Conn) error { +func sendPlaylistsMessageToRadio(ws *websocket.Conn, p []Playlist) error { playlistSpecs := make([]protocol.PlaylistSpec, 0) - for _, v := range db.GetPlaylists() { + for _, v := range p { if v.Enabled { entrySpecs := make([]protocol.EntrySpec, 0) for _, e := range db.GetEntriesForPlaylist(v.Id) { @@ -101,17 +90,18 @@ func sendPlaylistsMessageToRadio(ws *websocket.Conn) error { func KeepPlaylistsUpdated(ws *websocket.Conn) { for { - <-playlistChangeWait - err := sendPlaylistsMessageToRadio(ws) + p, ch := playlists.WatchForChanges() + err := sendPlaylistsMessageToRadio(ws, p) if err != nil { return } + <-ch } } -func sendFilesMessageToRadio(ws *websocket.Conn) error { +func sendFilesMessageToRadio(ws *websocket.Conn, f []FileSpec) error { specs := make([]protocol.FileSpec, 0) - for _, v := range files.Files() { + for _, v := range f { specs = append(specs, protocol.FileSpec{Name: v.Name, Hash: v.Hash}) } files := protocol.FilesMessage{ @@ -125,10 +115,11 @@ func sendFilesMessageToRadio(ws *websocket.Conn) error { func KeepFilesUpdated(ws *websocket.Conn) { for { - <-files.ChangeChannel() - err := sendFilesMessageToRadio(ws) + f, ch := files.WatchForChanges() + err := sendFilesMessageToRadio(ws, f) if err != nil { return } + <-ch } }