-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchristmasd.go
199 lines (171 loc) · 4.99 KB
/
christmasd.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package christmasd
import (
"context"
"fmt"
"image"
"log/slog"
"net/http"
"dev.acmcsuf.com/christmas/lib/xcolor"
"dev.acmcsuf.com/christmasd/christmaspb"
"github.com/gobwas/ws"
"golang.org/x/sync/errgroup"
"gopkg.in/typ.v4/sync2"
)
//go:generate mkdir -p christmaspb
//go:generate protoc -I=. --go_out=paths=source_relative:./christmaspb christmas.proto
// ServerOpts are options for a server.
type ServerOpts struct {
// LEDController is the LED controller to use for the server.
LEDController LEDController
// Logger is the logger to use for the server.
Logger *slog.Logger
// HTTPUpgrader is the HTTP-to-Websocket upgrader to use for the server.
HTTPUpgrader ws.HTTPUpgrader
}
// Server handles all HTTP requests for the server.
type Server struct {
opts ServerOpts
connections sync2.Map[*Session, sessionControl]
}
type sessionControl struct {
cancel context.CancelCauseFunc
}
// NewServer creates a new server.
func NewServer(opts ServerOpts) *Server {
return &Server{
opts: opts,
}
}
// KickAllConnections kicks all connections from the server.
// Optionally, a reason can be provided.
func (s *Server) KickAllConnections(reason string) {
var err error
if reason != "" {
err = fmt.Errorf("kicked: %s", reason)
} else {
err = fmt.Errorf("kicked")
}
s.connections.Range(func(s *Session, ctrl sessionControl) bool {
ctrl.cancel(err)
return true
})
}
// ServeHTTP implements http.Handler.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, err := SessionUpgrade(w, r, s.opts)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithCancelCause(r.Context())
s.connections.Store(session, sessionControl{cancel: cancel})
if err := session.Start(ctx); err != nil {
s.connections.Delete(session)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Session is a websocket session. It implements handling of messages from a
// single client.
type Session struct {
ws *websocketServer
logger *slog.Logger
opts ServerOpts
}
// SessionUpgrade upgrades an HTTP request to a websocket session.
func SessionUpgrade(w http.ResponseWriter, r *http.Request, opts ServerOpts) (*Session, error) {
wsconn, _, _, err := opts.HTTPUpgrader.Upgrade(r, w)
if err != nil {
return nil, fmt.Errorf("failed to upgrade HTTP: %w", err)
}
logger := opts.Logger.With("addr", wsconn.RemoteAddr())
return &Session{
ws: newWebsocketServer(wsconn, logger),
logger: logger,
opts: opts,
}, nil
}
// Start starts the server.
func (s *Session) Start(ctx context.Context) error {
errg, ctx := errgroup.WithContext(ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errg.Go(func() error {
return s.ws.Start(ctx)
})
errg.Go(func() error {
// Treat main loop errors as fatal and kill the connection,
// but don't return it because it's not the caller's fault.
if err := s.mainLoop(ctx); err != nil {
return s.ws.SendError(ctx, err)
}
return nil
})
return errg.Wait()
}
var errInternalServer = fmt.Errorf("internal server error")
func (s *Session) mainLoop(ctx context.Context) error {
bufPbLED := make([]uint32, len(s.opts.LEDController.LEDs()))
bufCtLED := make([]xcolor.RGB, len(s.opts.LEDController.LEDs()))
for {
select {
case <-ctx.Done():
return nil
case msg := <-s.ws.Messages:
switch msg := msg.GetMessage().(type) {
case *christmaspb.LEDClientMessage_GetLeds:
ctLEDs := s.opts.LEDController.LEDs()
for i, led := range ctLEDs {
bufPbLED[i] = led.ToUint()
}
s.ws.Send(ctx, &christmaspb.LEDServerMessage{
Message: &christmaspb.LEDServerMessage_GetLeds{
GetLeds: &christmaspb.GetLEDsResponse{
Leds: bufPbLED,
},
},
})
case *christmaspb.LEDClientMessage_SetLeds:
pbLEDs := msg.SetLeds.GetLeds()
if len(pbLEDs) != len(bufCtLED) {
return fmt.Errorf("invalid number of LEDs: %d", len(pbLEDs))
}
for i, led := range pbLEDs {
bufCtLED[i] = xcolor.RGBFromUint(led)
}
if err := s.opts.LEDController.SetLEDs(bufCtLED); err != nil {
s.logger.Error(
"failed to set LEDs",
"err", err)
return errInternalServer
}
case *christmaspb.LEDClientMessage_GetLedCanvasInfo:
w, h := s.opts.LEDController.ImageSize()
s.ws.Send(ctx, &christmaspb.LEDServerMessage{
Message: &christmaspb.LEDServerMessage_GetLedCanvasInfo{
GetLedCanvasInfo: &christmaspb.GetLEDCanvasInfoResponse{
Width: uint32(w),
Height: uint32(h),
},
},
})
case *christmaspb.LEDClientMessage_SetLedCanvas:
w, h := s.opts.LEDController.ImageSize()
img := image.RGBA{
Rect: image.Rect(0, 0, w, h),
Stride: w * 4,
Pix: msg.SetLedCanvas.GetPixels().GetPixels(),
}
if len(img.Pix) != w*h*4 {
return fmt.Errorf("invalid image size")
}
if err := s.opts.LEDController.DrawImage(&img); err != nil {
s.logger.Error(
"failed to draw image",
"err", err)
return errInternalServer
}
}
}
}
}