forked from joeledwards/msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.coffee
executable file
·381 lines (305 loc) · 10.4 KB
/
msg.coffee
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env coffee
require 'log-a-log'
_ = require 'lodash'
P = require 'bluebird'
fs = require 'fs'
ws = require 'ws'
mqtt = require 'mqtt'
uuid = require 'uuid'
Redis = require 'ioredis'
EventEmitter = require 'events'
# A key-value store for all client info.
class ClientChannelRegistry
constructor: ->
@clients = {}
@channels = {}
markChannelSend: (channelKey) ->
channel = @channels[channelKey]
if channel?
channel.send += 1
markChannelRecv: (channelKey) ->
channel = @channels[channelKey]
if channel?
channel.recv += 1
markClientSend: (clientId) ->
client = @clients[clientId]
if client?
client.send += 1
markClientRecv: (clientId) ->
client = @clients[clientId]
if client?
client.recv += 1
# Fetch all channels associated with a client
getClientChannels: (clientId) ->
client = @clients[clientId] ? {}
client.channels
# Fetch all clients associated with a channel
getChannelClients: (channelKey) ->
channel = @channels[channelKey] ? {}
channel.clients
# Add a client
addClient: (clientId, socket) ->
client = @clients[clientId]
if client?
client.socket = socket
else
@clients[clientId] =
socket: socket
send: 0
recv: 0
channels: {}
# Remove a client
removeClient: (clientId) ->
emptyChannels = []
client = @clients[clientId]
if client?
channels = client.channels
if channels?
_(channels)
.keys()
.each (channelKey) =>
if @unsubscribe(clientId, channelKey) < 1
emptyChannels.push channelKey
console.log "Removing client #{clientId}; sent=#{client.send} received=#{client.recv}"
delete @clients[clientId]
emptyChannels
# Subscribe a client to a channel
subscribe: (clientId, channelKey) ->
client = @clients[clientId]
client.channels[channelKey] = 1
channel = @channels[channelKey]
if not channel?
channel =
send: 0
recv: 0
clients: {}
@channels[channelKey] = channel
channel.clients[clientId] = client.socket
_(channel.clients).size()
# Unsubscribe a client from a channel
unsubscribe: (clientId, channelKey) ->
client = @clients[clientId]
if client?
delete client.channels[channelKey]
channel = @channels[channelKey]
if channel?
delete channel.clients[clientId]
clientCount = _(channel.clients).size()
if clientCount < 1
if channel.send > 0 and channel.recv > 0 and channel.send != channel.recv
console.log "============================================================================="
console.log "-----------------------------------------------------------------------------"
console.log "Removing channel #{channelKey}; sent=#{channel.send} received=#{channel.recv}"
console.log "-----------------------------------------------------------------------------"
console.log "============================================================================="
else
console.log "Removing channel #{channelKey}; sent=#{channel.send} received=#{channel.recv}"
delete @channels[channelKey]
clientCount
else
0
plur = (stem, count) ->
if count == 1 then "#{count} #{stem}" else "#{count} #{stem}s"
class BaseCore extends EventEmitter
constructor: ->
@paused = false
pause: ->
@paused = true
resume: ->
@paused = false
isPaused: ->
@paused
# Core based on broker which speaks MQTT.
class MqttCore extends BaseCore
constructor: (servers) ->
super()
console.log "Establishing MQTT connection"
@client = mqtt.connect({servers: servers})
@client.on 'message', (topic, message) =>
if not @paused
@emit 'message', topic, JSON.parse(message)
@client.on 'connect', =>
console.log "MQTT client connected"
@emit 'connected'
@client.on 'reconnect', =>
console.log "MQTT client reconnected"
@emit 'reconnected'
@client.on 'error', (error) =>
console.error "MQTT client error:", error
@emit 'error', error
@client.on 'close', =>
console.log "MQTT connection closed"
@emit 'close'
subscribe: (channel) ->
new P (resolve, reject) =>
console.log "subscribing to channel '#{channel}'"
@client.subscribe channel, (error, granted) ->
if error?
console.error "Error subscribing to channel '#{channel}':", error
reject error
else
count = _(granted).size()
console.log "#{plur 'subscription', count} for this client"
resolve()
unsubscribe: (channel) ->
new P (resolve, reject) =>
console.log "un-subscribing from channel '#{channel}'"
@client.unsubscribe channel, (error) ->
if error?
console.error "Error unsubscribing from channel '#{channel}':", error
reject error
else
console.log "Successfully unsubscribed client from channel #{channel}"
resolve()
publish: (channel, message) ->
@client.publish channel, JSON.stringify(message)
close: ->
console.log "Closing MqttCore"
@client.end()
# Core based on Redis, designed for a distributed cluster of msg servers.
class RedisCore extends BaseCore
constructor: ({cluster, server, servers}) ->
super()
console.log "Establishing Redis connections"
@sub = if cluster then new Redis.Cluster(servers) else new Redis(server)
@pub = if cluster then new Redis.Cluster(servers) else new Redis(server)
@sub.on 'message', (channel, message) =>
if not @paused
@emit 'message', channel, message
setImmediate => @emit 'connected'
subscribe: (channel) ->
new P (resolve, reject) =>
console.log "subscribing to channel '#{channel}'"
@sub.subscribe channel, (error, count) ->
if error?
console.error "Error subscribing to channel '#{channel}':", error
reject error
else
console.log "#{plur 'subscription', count} in Redis"
resolve()
unsubscribe: (channel) ->
new P (resolve, reject) =>
console.log "un-subscribing from channel '#{channel}'"
@sub.unsubscribe channel, (error, count) ->
if error?
console.error "Error unsubscribing from channel '#{channel}':", error
reject error
else
console.log "#{plur 'subscription', count} in Redis"
resolve()
publish: (channel, message) ->
@pub.publish channel, message
close: ->
console.log "Closing RedisCore"
@sub.close()
@pub.close()
# Core based on memory, designed for a stand-alone msg server.
class MemoryCore extends BaseCore
constructor: ->
super()
console.log "Nothing to setup in MemoryCore"
setImmediate => @emit 'connected'
subscribe: (channel) ->
console.log "Pretending to subscribe to channel #{channel}"
unsubscribe: (channel) ->
console.log "Pretending to unsubscribe from channel #{channel}"
P.resolve()
publish: (channel, message) ->
if not @paused
setImmediate =>
@emit 'message', channel, message
close: ->
console.log "Closing MemoryCore"
config = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
core = new RedisCore(config.redis)
#core = new MqttCore(config.mqtt.servers)
#core = new MemoryCore()
context = new ClientChannelRegistry()
server = new ws.Server({port: 8888})
core.on 'message', (channel, message) ->
context.markChannelRecv channel
_(context.getChannelClients(channel))
.toPairs()
.each ([clientId, socket]) ->
record =
action: 'message'
channel: channel
message: message
json = JSON.stringify record
try
socket.send json
context.markClientSend clientId
catch error
console.error """Error forwarding message:
channel: #{channel}
message: #{JSON.stringify(message)}
""", error
# Server connection handler
server.on 'connection', (sock) ->
clientId = uuid.v1()
console.log "New connection, client #{clientId}"
context.addClient clientId, sock
sock.on 'error', (error) ->
console.error "Connection error: #{error}\n#{error.stack}"
sock.on 'close', ->
console.log "Client #{clientId} disconnected."
_(context.removeClient(clientId))
.each (channel) ->
core.unsubscribe channel
sock.on 'message', (json) ->
try
record = JSON.parse json
{action, channel, message} = record
switch action
when 'publish'
if channel? and message?
context.markClientRecv clientId
core.publish channel, message
context.markChannelSend channel
else
logger.error "Publish record missing channel or message!"
when 'subscribe'
if channel?
core.subscribe channel
.then ->
count = context.subscribe clientId, channel
console.log "#{plur 'subscriber', count} on channel #{channel}"
sock.send JSON.stringify({
action: 'subscribe'
channel: channel
result: 'success'
})
.catch (error) ->
console.error "Error subscribing client #{clientId} to channel #{channel}:", error
sock.send JSON.stringify({
action: 'subscribe'
channel: channel
result: 'failure'
})
else
logger.error "Subscribe record missing channel!"
when 'unsubscribe'
if channel?
unsub = P.resolve()
if _(context.getChannelClients(channel)).size() < 1
unsub = core.unsubscribe channel
unsub
.then ->
count = context.unsubscribe clientId, channel
console.log "#{plur 'subscriber', count} on channel #{channel}"
sock.send JSON.stringify({
action: 'unsubscribe'
channel: channel
result: 'success'
})
.catch (error) ->
console.error "Error unsubscribing client #{clientId} from channel #{channel}:", error
sock.send JSON.stringify({
action: 'unsubscribe'
channel: channel
result: 'failure'
})
else
logger.error "Unsubscribe record missing channel!"
catch error
console.error "Invalid JSON: #{error}\n#{error.stack}\n#{json}"