summaryrefslogtreecommitdiff
path: root/src/helsinki.at/rhrdtime/rhrdtime.go
blob: 5fa72a12b621a3014144ca206e1d5b1a9f00bac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
//  rhrdtime
//
//  The Radio Helsinki Rivendell Time Websocket Server
//
//
//  Copyright (C) 2015 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of rhrdtime.
//
//  rhrdtime is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  rhrdtime is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with rhrdtime. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"net/http"
	"time"

	"github.com/go-martini/martini"
	"github.com/gorilla/websocket"
)

var (
	loc *time.Location
)

func init() {
	var err error
	if loc, err = time.LoadLocation("Europe/Vienna"); err != nil {
		fmt.Println("Error while loading loaction Europe/Vienna:", err)
	}
}

func getTZOffset(t time.Time) int {
	t = t.In(loc)
	_, offset := t.Zone()
	return offset
}

type ntpMessage struct {
	T1       int64 `json:"t1"`
	T2       int64 `json:"t2"`
	T3       int64 `json:"t3"`
	T4       int64 `json:"t4"`
	TZOffset int   `json:"tz_offset"`
}

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)
		return
	} else if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Client", ws.RemoteAddr(), "connected")

	for {
		msgtype, msg, err := ws.ReadMessage()
		if err != nil {
			fmt.Println("Client", ws.RemoteAddr(), "disconnected:", err)
			return
		}
		now_t2 := time.Now()
		if msgtype == websocket.TextMessage {

			var n ntpMessage
			if err := json.Unmarshal(msg, &n); err != nil {
				fmt.Println("Ignoring malformed (", err, ") NTP message from:", ws.RemoteAddr())
				continue
			}

			n.T2 = (now_t2.Unix() * 1000) + int64(now_t2.Nanosecond()/1000000)
			n.TZOffset = getTZOffset(now_t2)

			now_t3 := time.Now()
			n.T3 = (now_t3.Unix() * 1000) + int64(now_t3.Nanosecond()/1000000)

			ntp_json, err := json.Marshal(n)
			if err != nil {
				fmt.Println("Error while encoding to json:", err)
				continue
			}
			if err := ws.WriteMessage(websocket.TextMessage, ntp_json); err != nil {
				fmt.Println("Client", ws.RemoteAddr(), "write error:", err)
				return
			}
		}
	}
}

func RunMartini(addr string) {
	m := martini.Classic()
	m.Get("/ntp", func(w http.ResponseWriter, r *http.Request) {
		handleNTPClient(w, r)
	})

	m.RunOnAddr(addr)
}

func main() {
	addr_s := flag.String("addr", ":3000", "addr:port to listen on, default: ':3000'")
	help := flag.Bool("help", false, "show usage")

	flag.Parse()
	if *help {
		flag.Usage()
		return
	}

	RunMartini(*addr_s)
}