From f6c63b2be61fd352c6364aabe7458afd355b6d01 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 18 Jul 2015 04:35:55 +0200 Subject: removed /time with old protocol diff --git a/Makefile b/Makefile index 274531e..566972e 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,6 @@ getlibs: export GOPATH=$(curdir) getlibs: $(GOCMD) get "github.com/go-martini/martini" $(GOCMD) get "github.com/gorilla/websocket" - $(GOCMD) get "github.com/tuxychandru/pubsub" vet: export GOPATH=$(curdir) vet: 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) } -- cgit v0.10.2