Read players from fifo.

This commit is contained in:
Sascha L. Teichmann 2015-03-09 14:31:20 +01:00
parent ff08f2af94
commit 5d7b910a4d

View File

@ -1,14 +1,19 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"os"
"sync" "sync"
"time"
) )
const sleepInterval = time.Second * 5
var geoJSONTmpl = template.Must(template.New("geojson").Parse( var geoJSONTmpl = template.Must(template.New("geojson").Parse(
`{ "type": "Feature", `{ "type": "Feature",
"geometry": { "geometry": {
@ -21,10 +26,10 @@ var geoJSONTmpl = template.Must(template.New("geojson").Parse(
}`)) }`))
type player struct { type player struct {
X float64 X float64 `json:"x"`
Y float64 Y float64 `json:"y"`
Z float64 Z float64 `json:"z"`
Name string Name string `json:"name"`
} }
type players struct { type players struct {
@ -46,9 +51,33 @@ func (p *player) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
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
}
func (ps *players) run() { func (ps *players) run() {
for { for {
// TODO: Implement me! pls, err := ps.readFromFIFO()
if err != nil {
time.Sleep(sleepInterval)
continue
}
ps.mu.Lock()
ps.pls = pls
ps.mu.Unlock()
// TODO: Implement websocket broadcast.
} }
} }