-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
158 lines (140 loc) · 3.43 KB
/
server.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"log"
"net"
"net/http"
"time"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/vipnode/ethstats/stats"
)
func renderJSON(w http.ResponseWriter, body interface{}) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(body); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
type Server struct {
collector
Name stats.ID
}
func (srv *Server) APIHandler(w http.ResponseWriter, r *http.Request) {
nodeID := r.FormValue("node")
if nodeID == "" {
response := struct {
Nodes []stats.ID `json:"nodes"`
}{
Nodes: srv.List(),
}
renderJSON(w, response)
return
}
node, ok := srv.Get(stats.ID(nodeID))
if !ok {
http.NotFound(w, r)
return
}
renderJSON(w, node)
}
func (srv *Server) WebsocketHandler(w http.ResponseWriter, r *http.Request) {
log.Print("connected, upgrading", r)
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
go func() {
err := srv.Join(conn)
// TODO: Detect normal disconnects and ignore them?
if err != nil {
log.Printf("closing connection after error: %s", err)
}
}()
}
// Join runs the event loop for a connection, should be run in a goroutine.
func (srv *Server) Join(conn net.Conn) error {
defer conn.Close()
var err error
var emit EmitMessage
r := wsutil.NewReader(conn, ws.StateServerSide)
w := wsutil.NewWriter(conn, ws.StateServerSide, ws.OpText)
decoder := json.NewDecoder(r)
encoder := json.NewEncoder(w)
for {
// Prepare for the next message
if _, err = r.NextFrame(); err != nil {
return err
}
// Decode next message
if err = decoder.Decode(&emit); err != nil {
return err
}
log.Printf("%s: received topic: %s", conn.RemoteAddr(), emit.Topic)
// TODO: Support relaying by trusting and mapping ID?
switch topic := emit.Topic; topic {
case "hello":
report := stats.AuthReport{}
if err = json.Unmarshal(emit.Payload, &report); err != nil {
break
}
if err = srv.Collect(report); err != nil {
break
}
defer func() {
srv.Collect(stats.DisconnectReport{report.NodeID()})
}()
err = encoder.Encode(&EmitMessage{
Topic: "ready",
})
case "node-ping":
// Every ethstats implementation ignores the clientTime in
// the response here, and there is no standard format (eg.
// geth sends a monotonic offset) so we'll ignore it too.
sendPayload, err := json.Marshal(&stats.PingReport{srv.Name, time.Now()})
if err != nil {
break
}
// TODO: We could reuse a sendPayload buffer above
err = encoder.Encode(&EmitMessage{
Topic: "node-pong",
Payload: sendPayload,
})
case "latency":
report := stats.LatencyReport{}
if err = json.Unmarshal(emit.Payload, &report); err != nil {
break
}
err = srv.Collect(report)
case "block":
report := stats.BlockReport{}
if err = json.Unmarshal(emit.Payload, &report); err != nil {
break
}
err = srv.Collect(report)
case "pending":
report := stats.PendingReport{}
if err = json.Unmarshal(emit.Payload, &report); err != nil {
break
}
err = srv.Collect(report)
case "stats":
report := stats.StatusReport{}
if err = json.Unmarshal(emit.Payload, &report); err != nil {
break
}
err = srv.Collect(report)
default:
continue
}
if err != nil {
return err
}
// Write buffer
if err = w.Flush(); err != nil {
return err
}
}
}