package main
import (
- "code.octet-stream.net/broadcaster/protocol"
+ "code.octet-stream.net/broadcaster/internal/protocol"
"encoding/json"
"golang.org/x/net/websocket"
"log"
)
func RadioSync(ws *websocket.Conn) {
- log.Println("A websocket connected, I think")
+ log.Println("Radio websocket connected, not yet authenticated")
buf := make([]byte, 16384)
badRead := false
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 {
}
}
-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) {
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{
func KeepFilesUpdated(ws *websocket.Conn) {
for {
- <-files.ChangeChannel()
- err := sendFilesMessageToRadio(ws)
+ f, ch := files.WatchForChanges()
+ err := sendFilesMessageToRadio(ws, f)
if err != nil {
return
}
+ <-ch
}
}