-
Notifications
You must be signed in to change notification settings - Fork 3
/
shape3dserver.go
156 lines (144 loc) · 3.78 KB
/
shape3dserver.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
package main
import (
"context"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/soypat/sdf3ui/model"
"gonum.org/v1/gonum/spatial/r3"
"github.com/soypat/sdf/render"
"golang.org/x/time/rate"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
type shape3DServer struct {
mu sync.Mutex
shape model.Shape3D
stat model.ServerStatus
// staleFunc is a function that is called when shape
// becomes stale. It cancels the Shape3D's context.
staleFunc func()
shapeNotify chan struct{}
}
// ServeHTTP is a basic websocket implementation for reading/writing a TODO list
// from a websocket.
func (s *shape3DServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
Subprotocols: []string{model.WSSubprotocol},
InsecureSkipVerify: true,
})
if err != nil {
log.Println(err)
return
}
c.SetReadLimit(model.WSReadLimit)
if c.Subprotocol() != model.WSSubprotocol {
c.Close(websocket.StatusPolicyViolation, "client must speak the "+model.WSSubprotocol+" subprotocol")
return
}
l := rate.NewLimiter(rate.Every(500*time.Millisecond), 2)
log.Println("websocket connection established")
if s.shapeNotify == nil {
s.shapeNotify = make(chan struct{})
}
for range s.shapeNotify {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
err = s.sendStatus(ctx, c, l)
cancel()
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
c.Close(websocket.StatusInternalError, "normal closure")
return
}
if err != nil {
c.Close(websocket.StatusInternalError, err.Error())
log.Printf("failed to send status to %v: %v\n", r.RemoteAddr, err)
return
}
time.Sleep(500 * time.Millisecond) // burst rate limiting
}
}
// sendStatus sends 3d model data over websocket connection. It fails to send
// if shape becomes stale.
func (t *shape3DServer) sendStatus(ctx context.Context, c *websocket.Conn, l *rate.Limiter) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.shape.Context().Err() != nil {
t.shape.SetContext(ctx) // temp workaround
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
// This goroutine cancels sendShape if shape becomes stale.
select {
case <-ctx.Done():
case <-t.shape.Context().Done():
log.Println("shape became stale during status send")
}
cancel()
}()
err := l.Wait(ctx)
if err != nil {
return err
}
return wsjson.Write(ctx, c, t.stat)
}
// SetShape sets the render data and handles Shape3D context
// and sequence number.
func (t *shape3DServer) SetShape(name string, data []r3.Triangle) {
if t.staleFunc != nil {
t.staleFunc()
}
t.mu.Lock()
defer t.mu.Unlock()
ctx, cancel := context.WithCancel(context.Background())
t.staleFunc = cancel
seq := t.shape.Seq + 1
t.stat = model.ServerStatus{
ShapeSeq: seq,
ShapeName: name,
}
t.shape = model.Shape3D{
Triangles: data,
Seq: seq,
}
t.shape.SetContext(ctx)
busysend:
for {
select {
case t.shapeNotify <- struct{}{}:
// send until no more requests waiting.
default:
break busysend
}
}
}
func (t *shape3DServer) serveShapeHTTP(w http.ResponseWriter, r *http.Request) {
defer log.Printf("request to encode %s ended\n", t.shape.String())
w.Header().Add("Content-Type", "application/octet-stream")
w.WriteHeader(200)
t.mu.Lock()
defer t.mu.Unlock()
err := t.shape.Encode(w)
if err != nil {
log.Printf("[ERR] encoding shape %s: %s", t.shape.String(), err)
return
}
}
func (t *shape3DServer) createSTLHandler(w http.ResponseWriter, req *http.Request) {
t.mu.Lock()
defer t.mu.Unlock()
filename := req.URL.Query().Get("name")
fp, err := os.Create(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
err = render.WriteSTL(fp, t.shape.Triangles)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(200)
}