-
Notifications
You must be signed in to change notification settings - Fork 6
/
collab.go
237 lines (209 loc) · 5.48 KB
/
collab.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package db
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/uclaacm/teach-la-go-backend/httpext"
"golang.org/x/net/websocket"
)
type Message struct {
Author string `json:"author"`
Type string `json:"type"`
Target string `json:"target"`
Body string `json:"body"`
}
type stringSet map[string]bool
// Session describes a collaborative coding environment.
type Session struct {
*sync.Mutex
// Map UIDs to their websocket.Conn
Conns map[string]*Connection
Teacher string
}
type Connection struct {
*websocket.Conn
UID string
// Set of UIDs which are notified when this connection has changes
Subscriptions stringSet
}
// Maps session IDs to Session object
var (
sessions sync.Map
)
func (s *Session) AddConn(uid string, conn *websocket.Conn) error {
s.Lock()
defer s.Unlock()
if s.Conns[uid] != nil {
return errors.New("User is already connected")
}
connection := &Connection{Conn: conn, UID: uid, Subscriptions: make(stringSet)}
s.Conns[uid] = connection
return nil
}
func (s *Session) RemoveConn(uid string) error {
s.Lock()
defer s.Unlock()
if s.Conns[uid] != nil {
return errors.New("Could not remove unconnected user")
}
delete(s.Conns, uid)
// Stop other connections from sending to removed connection
for _, conn := range s.Conns {
delete(conn.Subscriptions, uid)
}
return nil
}
// BroadcastAll sends a message to all active connections
func (s *Session) BroadcastAll(msg Message) (lastErr error) {
s.Lock()
defer s.Unlock()
for _, conn := range s.Conns {
if err := websocket.JSON.Send(conn.Conn, msg); err != nil {
lastErr = err
}
}
return
}
// BroadcastError creates and sends an Error message given a string err
// to a given uid
func (s *Session) BroadcastError(uid string, err string) error {
errorMsg := Message{
Author: uid,
Type: msgTypeError,
Body: err,
}
if broadcastErr := s.BroadcastTo(errorMsg, uid); broadcastErr != nil {
return broadcastErr
}
return nil
}
// BroadcastTo sends a Message msg to the connections associated with the
// provided uids
func (s *Session) BroadcastTo(msg Message, uids ...string) (lastErr error) {
s.Lock()
defer s.Unlock()
for _, uid := range uids {
if err := websocket.JSON.Send(s.Conns[uid].Conn, msg); err != nil {
lastErr = err
}
}
return
}
// BroadcastToSet sends a Message msg to the connections associated with the
// provided uids in a stringSet
func (s *Session) BroadcastToSet(msg Message, uids stringSet) (lastErr error) {
s.Lock()
defer s.Unlock()
for uid := range uids {
if err := websocket.JSON.Send(s.Conns[uid].Conn, msg); err != nil {
lastErr = err
}
}
return
}
func (s *Session) RequestAccess(uid string, msg Message) error {
s.Lock()
defer s.Unlock()
if msg.Author == s.Teacher {
if conn, ok := s.Conns[msg.Target]; ok {
conn.Subscriptions[uid] = true
} else {
return s.BroadcastError(uid, "Student does not exist")
}
} else {
return s.BroadcastError(uid, "Teacher permission required to request access to student")
}
return nil
}
// CreateCollab creates a collaborative session, setting up the session's websocket.
// Request Body:
// {
// uid: UID for the user the program belongs to
// name: optional name identifier for the session, defaults to random UUID.
// }
//
// Returns status 201 created on success.
func (d *DB) CreateCollab(c echo.Context) error {
var body struct {
Name string `json:"name"`
UID string `json:"uid"`
}
if err := httpext.RequestBodyTo(c.Request(), &body); err != nil {
return c.String(http.StatusInternalServerError, "failed to read request body")
}
sessionID := uuid.New().String()
if body.Name != "" {
sessionID = body.Name
}
if _, ok := sessions.Load(sessionID); ok {
return c.String(http.StatusBadRequest, "session with same name already exists")
}
sessions.Store(sessionID, Session{
Conns: make(map[string]*Connection),
})
// Kill session if no connections every minute
go func() {
ticker := time.NewTicker(time.Minute)
for range ticker.C {
if sessionIFace, ok := sessions.Load(sessionID); ok && len(sessionIFace.(Session).Conns) == 0 {
fmt.Printf("Deleting session")
sessions.Delete(sessionID)
ticker.Stop()
return
}
}
}()
return c.String(http.StatusCreated, sessionID)
}
func (d *DB) JoinCollab(c echo.Context) error {
sessionID := c.Param("id")
uid := uuid.New().String() // What will we be using as identifiers?
sessionIFace, ok := sessions.Load(sessionID)
session := sessionIFace.(Session)
if !ok {
return c.String(http.StatusNotFound, "Session does not exist.")
}
websocket.Handler(func(ws *websocket.Conn) {
if err := session.AddConn(uid, ws); err != nil {
return
}
if len(session.Conns) == 1 {
session.Teacher = uid
defer func() {
if len(session.Conns) >= 1 {
// If teacher leaves, choose arbitrary member to be teacher
for k := range session.Conns {
session.Teacher = k
}
}
}()
}
for {
var msg Message
if err := websocket.JSON.Receive(ws, &msg); err != nil {
if err := session.BroadcastError(uid, err.Error()); err != nil {
break
}
if removeErr := session.RemoveConn(uid); removeErr != nil {
break
}
break
}
switch msg.Type {
case msgTypeRead:
if err := session.RequestAccess(uid, msg); err != nil {
break
}
default:
if err := session.BroadcastToSet(msg, session.Conns[uid].Subscriptions); err != nil {
break
}
}
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}