2019-01-02 16:11:22 +01:00
|
|
|
package www
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
2019-01-02 19:06:45 +01:00
|
|
|
"strconv"
|
2019-01-04 09:59:01 +01:00
|
|
|
"sync"
|
2019-01-02 16:11:22 +01:00
|
|
|
"html/template"
|
2019-01-02 19:06:45 +01:00
|
|
|
"net/http"
|
2019-01-02 16:11:22 +01:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
"../playlist"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// todo: hub - https://stackoverflow.com/questions/31532652/go-websocket-send-all-clients-a-message
|
2019-01-03 14:21:24 +01:00
|
|
|
var wsc *websocket.Conn
|
2019-01-02 16:11:22 +01:00
|
|
|
|
2019-01-02 19:06:45 +01:00
|
|
|
var p *playlist.Playlist
|
2019-01-03 14:21:24 +01:00
|
|
|
var pp *playlist.PrettyPlaylist
|
|
|
|
|
var mp *playlist.MinimalPlaylist
|
2019-01-02 16:11:22 +01:00
|
|
|
|
2019-01-04 09:59:01 +01:00
|
|
|
var bundle *Bundle
|
|
|
|
|
var mutex = &sync.Mutex{}
|
|
|
|
|
|
2019-01-02 16:11:22 +01:00
|
|
|
var pln = log.Println
|
|
|
|
|
|
|
|
|
|
type op_t struct {
|
|
|
|
|
OP string `json:"op"`
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
INDEX int `json:"index"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
|
ReadBufferSize: 512,
|
2019-01-04 09:59:01 +01:00
|
|
|
WriteBufferSize: 1024,
|
2019-01-02 16:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-02 19:06:45 +01:00
|
|
|
func Init(playlist *playlist.Playlist) {
|
|
|
|
|
p = playlist
|
2019-01-03 14:21:24 +01:00
|
|
|
p.CALLB = pop_callback
|
2019-01-02 16:11:22 +01:00
|
|
|
pp = p.Pretty()
|
2019-01-03 14:21:24 +01:00
|
|
|
mp = p.Minimal()
|
2019-01-04 09:59:01 +01:00
|
|
|
bundle = makeBundle()
|
|
|
|
|
go bundle.process()
|
|
|
|
|
|
2019-01-02 16:11:22 +01:00
|
|
|
http.HandleFunc("/pp", pp_handler)
|
|
|
|
|
http.HandleFunc("/ppop", pp_operations)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pp_handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
t, err := template.ParseFiles("./www/tmpl/playlist.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
pln(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
pp.Print()
|
|
|
|
|
t.Execute(w, pp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pp_operations(w http.ResponseWriter, r *http.Request) {
|
2019-01-04 09:59:01 +01:00
|
|
|
pln("new connection")
|
2019-01-02 16:11:22 +01:00
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
pln(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-01-04 09:59:01 +01:00
|
|
|
|
|
|
|
|
// pln("GGGG")
|
|
|
|
|
// s := &Socket{wsc: c, send: make(chan []byte, 1024), bundle: bundle}
|
|
|
|
|
// s.bundle.add <- s
|
|
|
|
|
|
|
|
|
|
s := makeSocket(c, bundle)
|
|
|
|
|
|
|
|
|
|
go s.read()
|
|
|
|
|
go s.write()
|
|
|
|
|
|
|
|
|
|
// // this should be hub
|
|
|
|
|
// go readop(c)
|
2019-01-02 16:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-04 09:59:01 +01:00
|
|
|
func (s *Socket) read() {
|
|
|
|
|
defer s.close()
|
2019-01-02 16:11:22 +01:00
|
|
|
for {
|
|
|
|
|
opdata := &op_t{}
|
2019-01-04 09:59:01 +01:00
|
|
|
if err := s.wsc.ReadJSON(&opdata); err != nil {
|
|
|
|
|
pln("0")
|
|
|
|
|
// if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
|
|
|
pln("connection closed - read")
|
|
|
|
|
break
|
|
|
|
|
// }
|
2019-01-02 16:11:22 +01:00
|
|
|
}
|
2019-01-02 19:06:45 +01:00
|
|
|
if opdata.OP == "move" {
|
|
|
|
|
if id, err := strconv.ParseUint(opdata.ID, 10, 32); err == nil {
|
2019-01-04 09:59:01 +01:00
|
|
|
mutex.Lock()
|
2019-01-02 19:06:45 +01:00
|
|
|
p.Move(uint32(id), opdata.INDEX)
|
|
|
|
|
pp = p.Pretty()
|
2019-01-03 14:21:24 +01:00
|
|
|
mp = p.Minimal()
|
2019-01-04 09:59:01 +01:00
|
|
|
mutex.Unlock()
|
|
|
|
|
s.bundle.broadcast <- mp.Encode() /// crap this nees to be []byte
|
2019-01-02 19:06:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-04 09:59:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-02 19:06:45 +01:00
|
|
|
|
2019-01-04 09:59:01 +01:00
|
|
|
func (s *Socket) write() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case mp, _ := <-s.send:
|
|
|
|
|
w, err := s.wsc.NextWriter(websocket.TextMessage);
|
|
|
|
|
if err != nil {
|
|
|
|
|
// if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
|
|
|
pln("connection closed - write")
|
|
|
|
|
return
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
w.Write(mp)
|
|
|
|
|
// is there queued messages?
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
2019-01-02 16:11:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 14:21:24 +01:00
|
|
|
func pop_callback(list *playlist.Playlist) {
|
|
|
|
|
if list == p {
|
|
|
|
|
pp = p.Pretty()
|
|
|
|
|
mp = p.Minimal()
|
2019-01-04 09:59:01 +01:00
|
|
|
bundle.broadcast <- mp.Encode()
|
2019-01-03 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-04 09:59:01 +01:00
|
|
|
|
|
|
|
|
// func readop(c *websocket.Conn) {
|
|
|
|
|
// for {
|
|
|
|
|
// opdata := &op_t{}
|
|
|
|
|
// if err := c.ReadJSON(&opdata); err != nil {
|
|
|
|
|
// pln(err)
|
|
|
|
|
// return //connection lost?
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if opdata.OP == "move" {
|
|
|
|
|
// if id, err := strconv.ParseUint(opdata.ID, 10, 32); err == nil {
|
|
|
|
|
// p.Move(uint32(id), opdata.INDEX)
|
|
|
|
|
// pp = p.Pretty()
|
|
|
|
|
// mp = p.Minimal()
|
|
|
|
|
// // pp.Print()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if err := c.WriteJSON(mp); err != nil {
|
|
|
|
|
// pln(err)
|
|
|
|
|
// return //connection lost?
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|