2014-04-14 07:39:18 +08:00
|
|
|
redis = require 'redis'
|
|
|
|
crypto = require 'crypto'
|
|
|
|
postal = require 'postal'
|
|
|
|
|
2014-04-24 05:37:19 +08:00
|
|
|
config = require '../config'
|
2014-04-24 04:18:25 +08:00
|
|
|
log = require './bbblogger'
|
|
|
|
|
|
|
|
module.exports = class RedisPubSub
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
@pubClient = redis.createClient()
|
|
|
|
@subClient = redis.createClient()
|
|
|
|
|
|
|
|
# hash to store requests waiting for response
|
|
|
|
@pendingRequests = {}
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
postal.subscribe
|
2014-04-24 05:37:19 +08:00
|
|
|
channel: config.redis.internalChannels.publish
|
2014-04-24 04:18:25 +08:00
|
|
|
topic: 'broadcast'
|
2014-04-25 05:33:59 +08:00
|
|
|
callback: (msg, envelope) =>
|
2014-04-24 04:18:25 +08:00
|
|
|
if envelope.replyTo?
|
2014-04-25 05:33:59 +08:00
|
|
|
@sendAndWaitForReply(msg, envelope)
|
2014-04-24 04:18:25 +08:00
|
|
|
else
|
2014-04-25 05:33:59 +08:00
|
|
|
@send(msg, envelope)
|
|
|
|
|
2014-05-09 05:32:09 +08:00
|
|
|
@subClient.on "psubscribe", @_onSubscribe
|
|
|
|
@subClient.on "pmessage", @_onMessage
|
2014-04-25 05:33:59 +08:00
|
|
|
|
|
|
|
log.info("RPC: Subscribing message on channel: #{config.redis.channels.fromBBBApps}")
|
2014-05-09 05:32:09 +08:00
|
|
|
@subClient.psubscribe(config.redis.channels.fromBBBApps)
|
2014-04-24 04:18:25 +08:00
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
# Sends a message and waits for a reply
|
2014-04-24 04:18:25 +08:00
|
|
|
sendAndWaitForReply: (message, envelope) ->
|
|
|
|
# generate a unique correlation id for this call
|
|
|
|
correlationId = crypto.randomBytes(16).toString('hex')
|
|
|
|
|
|
|
|
# create a timeout for what should happen if we don't get a response
|
|
|
|
timeoutId = setTimeout( (correlationId) =>
|
|
|
|
response = {}
|
|
|
|
# if this ever gets called we didn't get a response in a timely fashion
|
|
|
|
response.err =
|
|
|
|
code: "503"
|
|
|
|
message: "Waiting for reply timeout."
|
|
|
|
description: "Waiting for reply timeout."
|
|
|
|
postal.publish
|
|
|
|
channel: envelope.replyTo.channel
|
|
|
|
topic: envelope.replyTo.topic
|
|
|
|
data: response
|
|
|
|
# delete the entry from hash
|
|
|
|
delete @pendingRequests[correlationId]
|
2014-04-25 05:33:59 +08:00
|
|
|
, config.redis.timeout, correlationId)
|
2014-04-24 04:18:25 +08:00
|
|
|
|
|
|
|
# create a request entry to store in a hash
|
|
|
|
entry =
|
|
|
|
replyTo: envelope.replyTo
|
|
|
|
timeout: timeoutId #the id for the timeout so we can clear it
|
|
|
|
|
|
|
|
# put the entry in the hash so we can match the response later
|
|
|
|
@pendingRequests[correlationId] = entry
|
2014-05-16 04:42:54 +08:00
|
|
|
message.header.reply_to = correlationId
|
2014-05-22 23:22:13 +08:00
|
|
|
console.log("\n\nmessage=" + JSON.stringify(message) + "\n\n")
|
2014-05-16 04:42:54 +08:00
|
|
|
log.info({ message: message, channel: config.redis.channels.toBBBApps.meeting}, "Publishing a message")
|
|
|
|
@pubClient.publish(config.redis.channels.toBBBApps.meeting, JSON.stringify(message))
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
# Send a message without waiting for a reply
|
|
|
|
send: (message, envelope) ->
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
_onSubscribe: (channel, count) =>
|
|
|
|
log.info("Subscribed to #{channel}")
|
|
|
|
|
2014-05-09 05:32:09 +08:00
|
|
|
_onMessage: (pattern, channel, jsonMsg) =>
|
2014-04-25 05:33:59 +08:00
|
|
|
# TODO: this has to be in a try/catch block, otherwise the server will
|
|
|
|
# crash if the message has a bad format
|
|
|
|
message = JSON.parse(jsonMsg)
|
2014-05-23 03:25:00 +08:00
|
|
|
|
2014-05-21 02:54:20 +08:00
|
|
|
unless message.header?.name is "keep_alive_reply" #temporarily stop logging the keep_alive_reply message
|
|
|
|
log.debug({ pattern: pattern, channel: channel, message: message}, "Received a message from redis")
|
2014-05-27 01:19:14 +08:00
|
|
|
#console.log "=="+JSON.stringify message
|
2014-04-25 05:33:59 +08:00
|
|
|
|
|
|
|
# retrieve the request entry
|
2014-05-21 02:54:20 +08:00
|
|
|
|
|
|
|
#correlationId = message.header?.reply_to
|
|
|
|
correlationId = message.payload?.reply_to or message.header?.reply_to
|
|
|
|
console.log "\ncorrelation_id=" + correlationId
|
2014-04-25 05:33:59 +08:00
|
|
|
if correlationId? and @pendingRequests?[correlationId]?
|
|
|
|
entry = @pendingRequests[correlationId]
|
|
|
|
# make sure the message in the timeout isn't triggered by clearing it
|
|
|
|
clearTimeout(entry.timeout)
|
|
|
|
|
|
|
|
delete @pendingRequests[correlationId]
|
|
|
|
postal.publish
|
|
|
|
channel: entry.replyTo.channel
|
|
|
|
topic: entry.replyTo.topic
|
|
|
|
data: message
|
|
|
|
else
|
2014-05-23 03:25:00 +08:00
|
|
|
#sendToController(message)
|
2014-04-25 05:33:59 +08:00
|
|
|
|
2014-05-22 23:22:13 +08:00
|
|
|
if message.header?.name is 'validate_auth_token_reply'
|
|
|
|
if message.payload?.valid is "true"
|
|
|
|
|
2014-05-23 02:20:21 +08:00
|
|
|
#TODO use the message library for these messages. Perhaps put it in Modules?!
|
|
|
|
|
|
|
|
joinMeetingMessage = {
|
|
|
|
"payload": {
|
2014-05-23 05:52:36 +08:00
|
|
|
"meeting_id": message.payload.meeting_id
|
|
|
|
"user_id": message.payload.userid
|
2014-05-23 02:20:21 +08:00
|
|
|
},
|
|
|
|
"header": {
|
2014-05-23 05:52:36 +08:00
|
|
|
"timestamp": new Date().getTime()
|
|
|
|
"reply_to": message.payload.meeting_id + "/" + message.payload.userid
|
|
|
|
"name": "user_joined_event"
|
2014-05-23 02:20:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# the user joins the meeting
|
|
|
|
|
|
|
|
@pubClient.publish(config.redis.channels.toBBBApps.users, JSON.stringify(joinMeetingMessage))
|
|
|
|
console.log "just published the joinMeetingMessage in RedisPubSub"
|
|
|
|
|
2014-05-23 05:52:36 +08:00
|
|
|
#get the list of users in the meeting
|
2014-05-22 23:22:13 +08:00
|
|
|
getUsersMessage = {
|
|
|
|
"payload": {
|
2014-05-23 05:52:36 +08:00
|
|
|
"meeting_id": message.payload.meeting_id
|
|
|
|
"requester_id": message.payload.userid
|
2014-05-22 23:22:13 +08:00
|
|
|
},
|
|
|
|
"header": {
|
2014-05-23 05:52:36 +08:00
|
|
|
"timestamp": new Date().getTime()
|
|
|
|
"reply_to": message.payload.meeting_id + "/" + message.payload.userid
|
|
|
|
"name": "get_users_request"
|
2014-05-22 23:22:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@pubClient.publish(config.redis.channels.toBBBApps.users, JSON.stringify(getUsersMessage))
|
2014-05-23 02:20:21 +08:00
|
|
|
console.log "just published the getUsersMessage in RedisPubSub"
|
2014-05-22 23:22:13 +08:00
|
|
|
|
2014-05-23 05:52:36 +08:00
|
|
|
#get the chat history
|
|
|
|
getChatHistory = {
|
|
|
|
"payload": {
|
|
|
|
"meeting_id": message.payload.meeting_id
|
|
|
|
"requester_id": message.payload.userid
|
|
|
|
},
|
|
|
|
"header": {
|
|
|
|
"timestamp": new Date().getTime()
|
|
|
|
"reply_to": message.payload.meeting_id + "/" + message.payload.userid
|
|
|
|
"name": "get_chat_history"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@pubClient.publish(config.redis.channels.toBBBApps.chat, JSON.stringify(getChatHistory))
|
|
|
|
console.log "just published the getChatHistory in RedisPubSub"
|
|
|
|
|
|
|
|
|
2014-05-23 03:25:00 +08:00
|
|
|
else if message.header?.name is 'get_users_reply'
|
2014-05-23 05:52:36 +08:00
|
|
|
console.log 'got a reply from bbb-apps for get users'
|
|
|
|
sendToController(message)
|
|
|
|
|
|
|
|
else if message.header?.name is 'get_chat_history_reply'
|
|
|
|
console.log 'got a reply from bbb-apps for chat history'
|
2014-05-22 23:22:13 +08:00
|
|
|
sendToController(message)
|
|
|
|
|
2014-05-26 22:24:56 +08:00
|
|
|
else if message.header?.name is 'send_public_chat_message'
|
|
|
|
console.log "just got a public chat message :" + JSON.stringify message
|
|
|
|
sendToController (message)
|
|
|
|
|
|
|
|
publishing: (channel, message) =>
|
2014-05-27 01:19:14 +08:00
|
|
|
console.log '\n Publishing\n'
|
2014-05-26 22:24:56 +08:00
|
|
|
@pubClient.publish(channel, JSON.stringify(message))
|
|
|
|
|
2014-04-14 22:58:20 +08:00
|
|
|
sendToController = (message) ->
|
2014-04-24 04:18:25 +08:00
|
|
|
postal.publish
|
2014-04-24 05:37:19 +08:00
|
|
|
channel: config.redis.internalChannels.receive
|
2014-04-24 04:18:25 +08:00
|
|
|
topic: "broadcast"
|
2014-04-14 22:58:20 +08:00
|
|
|
data: message
|
2014-05-26 22:24:56 +08:00
|
|
|
|