2015-03-09 13:09:13 +01:00
|
|
|
package main
|
|
|
|
|
2015-03-09 14:01:30 +01:00
|
|
|
import (
|
2015-03-09 14:31:20 +01:00
|
|
|
"bufio"
|
2015-03-09 14:01:30 +01:00
|
|
|
"bytes"
|
2015-03-09 14:08:52 +01:00
|
|
|
"encoding/json"
|
2015-03-09 14:01:30 +01:00
|
|
|
"html/template"
|
2015-03-09 14:08:52 +01:00
|
|
|
"log"
|
2015-03-09 14:01:30 +01:00
|
|
|
"net/http"
|
2015-03-09 14:31:20 +01:00
|
|
|
"os"
|
2015-03-09 14:08:52 +01:00
|
|
|
"sync"
|
2015-03-09 14:31:20 +01:00
|
|
|
"time"
|
2015-03-09 14:01:30 +01:00
|
|
|
)
|
|
|
|
|
2015-03-09 14:31:20 +01:00
|
|
|
const sleepInterval = time.Second * 5
|
|
|
|
|
2015-03-09 14:01:30 +01:00
|
|
|
var geoJSONTmpl = template.Must(template.New("geojson").Parse(
|
|
|
|
`{ "type": "Feature",
|
|
|
|
"geometry": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [{{.X}}, {{.Z}}]
|
|
|
|
},
|
|
|
|
"properties": {
|
2015-03-09 15:01:47 +01:00
|
|
|
"name": "{{.Name | html }}"
|
2015-03-09 14:01:30 +01:00
|
|
|
}
|
|
|
|
}`))
|
|
|
|
|
|
|
|
type player struct {
|
2015-03-09 14:31:20 +01:00
|
|
|
X float64 `json:"x"`
|
|
|
|
Y float64 `json:"y"`
|
|
|
|
Z float64 `json:"z"`
|
|
|
|
Name string `json:"name"`
|
2015-03-09 14:01:30 +01:00
|
|
|
}
|
2015-03-09 13:09:13 +01:00
|
|
|
|
|
|
|
type players struct {
|
|
|
|
fifo string
|
2015-03-09 13:16:31 +01:00
|
|
|
wsf *websocketForwarder
|
2015-03-09 14:01:30 +01:00
|
|
|
pls []*player
|
2015-03-09 14:08:52 +01:00
|
|
|
mu sync.RWMutex
|
2015-03-09 13:09:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:16:31 +01:00
|
|
|
func newPlayers(fifo string, wsf *websocketForwarder) *players {
|
2015-03-09 14:32:14 +01:00
|
|
|
return &players{fifo: fifo, wsf: wsf, pls: []*player{}}
|
2015-03-09 13:09:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 14:01:30 +01:00
|
|
|
func (p *player) MarshalJSON() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := geoJSONTmpl.Execute(&buf, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-03-09 14:31:20 +01:00
|
|
|
func (ps *players) readFromFIFO() ([]*player, error) {
|
|
|
|
file, err := os.Open(ps.fifo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
reader := bufio.NewReader(file)
|
|
|
|
decoder := json.NewDecoder(reader)
|
|
|
|
var pls []*player
|
|
|
|
|
|
|
|
if err = decoder.Decode(&pls); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pls, nil
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:09:13 +01:00
|
|
|
func (ps *players) run() {
|
|
|
|
for {
|
2015-03-09 14:31:20 +01:00
|
|
|
pls, err := ps.readFromFIFO()
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(sleepInterval)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ps.mu.Lock()
|
|
|
|
ps.pls = pls
|
|
|
|
ps.mu.Unlock()
|
2015-03-09 15:37:57 +01:00
|
|
|
if ps.wsf != nil {
|
|
|
|
// TODO: Throttle this!
|
|
|
|
ps.wsf.BroadcastPlayers(pls)
|
|
|
|
}
|
2015-03-09 13:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *players) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2015-03-09 14:08:52 +01:00
|
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
|
|
ps.mu.RLock()
|
|
|
|
defer ps.mu.RUnlock()
|
|
|
|
encoder := json.NewEncoder(rw)
|
|
|
|
if err := encoder.Encode(ps.pls); err != nil {
|
|
|
|
log.Printf("error: sending JSON failed: %s\n", err)
|
|
|
|
}
|
2015-03-09 13:09:13 +01:00
|
|
|
}
|