-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
345 lines (278 loc) · 8.87 KB
/
hub.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package eventsocket
import (
"fmt"
log "github.com/dronemill/eventsocket/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
// hub maintains the set of active connections, and broadcasts messages
// for given events to where they need to go
type hub struct {
// Inbound messages from the connections.
recvClientMessage chan *ClientMessage
// Register a client
register chan *Client
// Unregister requests from connections.
unregister chan *wsConnection
// Registered connection map from connection to clientId
connections map[*wsConnection]string
// subscriptions is a list of the events that have atleast one subscriber
subscriptions hubSubscriptions
// clientSubscriptions is a map of clientId to a slice of events
// that the client has subscriped to
clientSubscriptions map[string]map[string]bool
}
// hubSubscription is a list of the clients who have subscribed to a given event
type hubSubscription map[string]bool
// hubSubscriptions is a list of the events that have atleast one subscriber
type hubSubscriptions map[string]hubSubscription
var h = hub{
recvClientMessage: make(chan *ClientMessage),
register: make(chan *Client),
unregister: make(chan *wsConnection),
connections: make(map[*wsConnection]string),
subscriptions: make(map[string]hubSubscription),
clientSubscriptions: make(map[string]map[string]bool),
}
func (h *hub) run() {
log.Info("Starting hub execution")
go h.recvRegistrations()
go h.recvUnregistrations()
for {
select {
case m := <-h.recvClientMessage:
h.ingest(m)
}
}
}
func (h *hub) recvRegistrations() {
log.Info("Ready to receive client registrations")
for {
h.registerClient(<-h.register)
}
}
func (h *hub) recvUnregistrations() {
log.Info("Ready to receive client unregistrations")
for {
h.unregisterConnection(<-h.unregister)
}
}
func (h *hub) registerClient(client *Client) {
log.WithField("clientID", client.Id).Info("Registring client")
h.connections[client.ws] = client.Id
h.clientSubscriptions[client.Id] = make(map[string]bool)
for s := range client.subscriptions {
h.storeSubscription(client.Id, s)
}
}
// unsuscribe from all events for this connection..? or set to false
// also remove the connection->client reference, and close the send chan
func (h *hub) unregisterConnection(ws *wsConnection) {
defer func() {
ws.Close()
}()
// if we dont know about this conneciton, then get out
if _, ok := h.connections[ws]; !ok {
log.Info("Aborting unregistration of unknown connection")
return
}
id := h.connections[ws]
delete(h.connections, ws)
log.WithField("clientID", id).Info("Unregistring connection")
// if we dont have any clientSubscriptions, then leave
if _, ok := h.clientSubscriptions[id]; !ok {
return
}
// remove all client subscriptions
for s := range h.clientSubscriptions[id] {
h.purgeSubscription(id, s)
}
delete(h.clientSubscriptions, id)
log.WithField("clientID", id).Info("Successfully unregistered connection")
}
// ingest a message and route it to the propper destination
func (h *hub) ingest(cm *ClientMessage) {
log.WithField("clientID", cm.ClientId).Info("Injesting message")
switch cm.Message.MessageType {
case MESSAGE_TYPE_BROADCAST:
h.handleBroadcast(cm)
case MESSAGE_TYPE_STANDARD:
h.handleStandard(cm)
case MESSAGE_TYPE_SUSCRIBE:
h.handleSuscribe(cm)
case MESSAGE_TYPE_UNSUSCRIBE:
h.handleUnsuscribe(cm)
case MESSAGE_TYPE_REQUEST:
h.handleRequest(cm)
case MESSAGE_TYPE_REPLY:
h.handleReply(cm)
default:
log.WithField("clientID", cm.ClientId).
WithField("type", cm.Message.MessageType).
Error("Unknown message type")
// FIXME: dont just silently drop message ..
}
}
// broadcast a message to all active clients
func (h *hub) handleBroadcast(cm *ClientMessage) {
log.WithField("clientID", cm.ClientId).Info("Handling broadcast message")
for ws, clientID := range h.connections {
log.WithField("clientID", clientID).Debug("Transmitting broadcast message")
h.send(ws, &cm.Message)
}
return
}
// handle a standard message
func (h *hub) handleStandard(cm *ClientMessage) {
log.WithField("clientID", cm.ClientId).Info("Handling standard message")
// ensure there is atleast one suscriber of this event
subscribers, ok := h.subscriptions[cm.Message.Event]
if !ok {
log.WithField("clientID", cm.ClientId).
WithField("event", cm.Message.Event).
Warn("No subscribers for event")
return
}
for s := range subscribers {
log.WithField("clientID", s).Debug("Transmitting standard message")
h.send(clients[s].ws, &cm.Message)
}
return
}
// suscribe a client to events
func (h *hub) handleSuscribe(cm *ClientMessage) {
log.WithField("clientID", cm.ClientId).Info("Handling subscription request")
// sanity check
events, ok := cm.Message.Payload["Events"]
if !ok {
log.WithField("clientID", cm.ClientId).Error("No events provided")
return
}
for _, event := range events.([]interface{}) {
e := event.(string)
h.storeSubscription(cm.ClientId, e)
}
return
}
// unsuscribe a client from events
func (h *hub) handleUnsuscribe(cm *ClientMessage) {
log.WithField("clientID", cm.ClientId).Info("Handling unsuscribe request")
// sanity check
events, ok := cm.Message.Payload["Events"]
if !ok {
log.WithField("clientID", cm.ClientId).Error("No events provided")
return
}
for _, event := range events.([]interface{}) {
e := event.(string)
h.purgeSubscription(cm.ClientId, e)
// remove the event from the client id subscriptions map
if _, ok := clients[cm.ClientId].subscriptions[e]; ok {
delete(clients[cm.ClientId].subscriptions, e)
}
}
return
}
// handle a request message. this is done by storing the requestee's
// ClientId as the ReplyTo, and then forwarding the message along
// to the receiving client
func (h *hub) handleRequest(cm *ClientMessage) {
log.WithField("fromClientID", cm.ClientId).
WithField("toClientID", cm.Message.RequestClientId).
Info("Handling request")
// store the clientID sending the message as the client to reply to
cm.Message.ReplyClientId = cm.ClientId
if clients[cm.Message.RequestClientId] == nil {
log.WithField("fromClientID", cm.ClientId).
WithField("toClientID", cm.Message.RequestClientId).
Error("Client does not exist")
h.requestError(cm,
"RequestClientID does not exist",
ErrorRequestClientNoExist,
)
return
}
err := h.send(clients[cm.Message.RequestClientId].ws, &cm.Message)
if err != nil {
log.WithField("fromClientID", cm.ClientId).
WithField("toClientID", cm.Message.RequestClientId).
Error("Client is not connected")
h.requestError(cm,
"RequestClientID is not connected",
ErrorRequestClientNotConnected,
)
}
}
// handle a reply. forward the message along to the requestee
func (h *hub) handleReply(cm *ClientMessage) {
log.WithField("fromClientID", cm.ClientId).
WithField("toClientID", cm.Message.ReplyClientId).
Info("Handling reply")
// FIXME need to have some form of error handling here..
h.send(clients[cm.Message.ReplyClientId].ws, &cm.Message)
}
// store the subscription for the given clientId and event
func (h *hub) storeSubscription(id, e string) {
log.WithField("clientID", id).WithField("event", e).
Info("Storing subscription")
// make sure we have a subscription key
if _, ok := h.subscriptions[e]; !ok {
h.subscriptions[e] = make(hubSubscription)
}
// store the clientid in the subscription map
h.subscriptions[e][id] = true
h.clientSubscriptions[id][e] = true
// store the event in the client id subscriptions map
clients[id].subscriptions[e] = true
}
// purge the subscription
func (h *hub) purgeSubscription(id, e string) {
log.WithField("clientID", id).WithField("event", e).
Info("Purgin subscription")
// remove the clientSubscriptions map, if it exists
if _, ok := h.clientSubscriptions[id][e]; ok {
delete(h.clientSubscriptions[id], e)
}
// make sure we are actually suscribing to this event
if _, ok := h.subscriptions[e][id]; !ok {
return
}
// remove the suscription
delete(h.subscriptions[e], id)
// if no one else if suscribing, then remove the suscription key
if len(h.subscriptions[e]) == 0 {
log.WithField("event", e).
Debug("No suscribers remain")
delete(h.subscriptions, e)
}
}
func (h *hub) send(ws *wsConnection, m *Message) error {
var err error
err = nil
defer func() {
if r := recover(); r != nil {
h.unregister <- ws
err = fmt.Errorf("Send failed: %s", r)
}
}()
if ws.closed {
h.unregister <- ws
err = fmt.Errorf("WS is closed")
} else {
ws.send <- *m
}
return err
}
func (h *hub) requestError(cm *ClientMessage, message, etype string) {
e := map[string]interface{}{
"Message": message,
"Type": etype,
}
m := Message{
MessageType: MESSAGE_TYPE_REPLY,
RequestId: cm.Message.RequestId,
Error: e,
}
// we dont need to check for errors here, because if the request fails,
// then it doesnt matter, as the client needing to know that it failed
// is the one who cant be sent to..
h.send(clients[cm.Message.ReplyClientId].ws, &m)
}