diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-07-18 02:35:55 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-07-18 02:35:55 (GMT) |
commit | f6c63b2be61fd352c6364aabe7458afd355b6d01 (patch) | |
tree | a36065a3276b0c48dd7b4e6a29d600184fa4a6cd /src | |
parent | 82c24e04f5b41f32cde625b645a7ae48ee561928 (diff) |
removed /time with old protocol
Diffstat (limited to 'src')
-rw-r--r-- | src/helsinki.at/rhrdtime/rhrdtime.go | 82 |
1 files changed, 4 insertions, 78 deletions
diff --git a/src/helsinki.at/rhrdtime/rhrdtime.go b/src/helsinki.at/rhrdtime/rhrdtime.go index af51902..e426c42 100644 --- a/src/helsinki.at/rhrdtime/rhrdtime.go +++ b/src/helsinki.at/rhrdtime/rhrdtime.go @@ -34,7 +34,6 @@ import ( "github.com/go-martini/martini" "github.com/gorilla/websocket" - "github.com/tuxychandru/pubsub" ) func getRDWeekAndOffset(t time.Time) (uint8, int) { @@ -68,60 +67,6 @@ func getRDWeekAndOffset(t time.Time) (uint8, int) { return uint8(math.Floor(math.Mod((((float64(sEpoch)+259200)/604800)+2), 4)) + 1), offset } -type timeUpdate struct { - Timestamp int64 `json:"timestamp"` - TZOffset int `json:"tz_offset"` - Week uint8 `json:"week"` -} - -func getTimeUpdate() []byte { - now := time.Now() - week, offset := getRDWeekAndOffset(now) - update := timeUpdate{(now.Unix() * 1000) + int64(now.Nanosecond()/1000000), offset, week} - update_json, err := json.Marshal(update) - if err != nil { - fmt.Println(err) - return []byte{'{', '}'} - } - return update_json -} - -func handleTimeUpdateClient(w http.ResponseWriter, r *http.Request, ps *pubsub.PubSub) { - ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) - if _, ok := err.(websocket.HandshakeError); ok { - http.Error(w, "Not a websocket handshake", 400) - return - } else if err != nil { - fmt.Println(err) - return - } - fmt.Println("Client", ws.RemoteAddr(), "connected") - - go func() { - for { - if _, _, err := ws.NextReader(); err != nil { - fmt.Println("Client", ws.RemoteAddr(), "disconnected:", err) - ws.Close() - break - } - } - }() - - timeupdate_chan := ps.Sub("timeupdate") - defer ps.Unsub(timeupdate_chan, "timeupdate") - - update_json := getTimeUpdate() - if err := ws.WriteMessage(websocket.TextMessage, update_json); err != nil { - return - } - - for jsonupdate := range timeupdate_chan { - if err := ws.WriteMessage(websocket.TextMessage, jsonupdate.([]byte)); err != nil { - return - } - } -} - type ntpMessage struct { T1 int64 `json:"t1"` T2 int64 `json:"t2"` @@ -131,7 +76,7 @@ type ntpMessage struct { Week uint8 `json:"week"` } -func handleNTPClient(w http.ResponseWriter, r *http.Request, ps *pubsub.PubSub) { +func handleNTPClient(w http.ResponseWriter, r *http.Request) { ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) if _, ok := err.(websocket.HandshakeError); ok { http.Error(w, "Not a websocket handshake", 400) @@ -175,20 +120,16 @@ func handleNTPClient(w http.ResponseWriter, r *http.Request, ps *pubsub.PubSub) } } -func RunMartini(ps *pubsub.PubSub, addr string) { +func RunMartini(addr string) { m := martini.Classic() - m.Get("/time", func(w http.ResponseWriter, r *http.Request) { - handleTimeUpdateClient(w, r, ps) - }) m.Get("/ntp", func(w http.ResponseWriter, r *http.Request) { - handleNTPClient(w, r, ps) + handleNTPClient(w, r) }) m.RunOnAddr(addr) } func main() { - interval_s := flag.String("interval", "15s", "the interval between updates, default: '15s'") addr_s := flag.String("addr", ":3000", "addr:port to listen on, default: ':3000'") help := flag.Bool("help", false, "show usage") @@ -198,20 +139,5 @@ func main() { return } - interval, err := time.ParseDuration(*interval_s) - if err != nil { - fmt.Println(err) - return - } - - ps := pubsub.New(1) - - ticker := time.NewTicker(interval) - go func() { - for _ = range ticker.C { - ps.Pub(getTimeUpdate(), "timeupdate") - } - }() - - RunMartini(ps, *addr_s) + RunMartini(*addr_s) } |