2014-04-14 07:39:18 +08:00
|
|
|
crypto = require 'crypto'
|
2014-05-30 03:55:32 +08:00
|
|
|
postal = require 'postal'
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
config = require '../config'
|
2014-05-30 03:55:32 +08:00
|
|
|
log = require './bbblogger'
|
2014-04-24 04:18:25 +08:00
|
|
|
|
|
|
|
moduleDeps = ["RedisPubSub"]
|
|
|
|
|
|
|
|
module.exports = class MessageBus
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
config.modules.wait moduleDeps, =>
|
|
|
|
@pubSub = config.modules.get("RedisPubSub")
|
|
|
|
|
|
|
|
receiveMessages: (callback) ->
|
|
|
|
postal.subscribe
|
2014-04-24 05:37:19 +08:00
|
|
|
channel: config.redis.internalChannels.receive
|
2014-04-24 04:18:25 +08:00
|
|
|
topic: "broadcast"
|
|
|
|
callback: (msg, envelope) ->
|
2014-04-24 06:27:43 +08:00
|
|
|
callback(msg)
|
2014-04-24 04:18:25 +08:00
|
|
|
|
|
|
|
sendAndWaitForReply: (data, callback) ->
|
|
|
|
replyTo =
|
2014-04-24 05:37:19 +08:00
|
|
|
channel: config.redis.internalChannels.reply
|
2014-04-24 04:18:25 +08:00
|
|
|
topic: 'get.' + crypto.randomBytes(16).toString('hex')
|
|
|
|
|
|
|
|
postal.subscribe(
|
|
|
|
channel: replyTo.channel
|
|
|
|
topic: replyTo.topic
|
|
|
|
callback: (msg, envelope) ->
|
2014-04-25 05:33:59 +08:00
|
|
|
callback(null, msg)
|
2014-04-24 04:18:25 +08:00
|
|
|
).once()
|
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
log.info({ message: data, replyTo: replyTo }, "Sending a message and waiting for reply")
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
postal.publish
|
2014-04-24 05:37:19 +08:00
|
|
|
channel: config.redis.internalChannels.publish
|
2014-04-24 04:18:25 +08:00
|
|
|
topic: 'broadcast'
|
|
|
|
replyTo: replyTo
|
|
|
|
data: data
|
2014-05-26 22:24:56 +08:00
|
|
|
|
|
|
|
sendingToRedis: (channel, message) =>
|
2014-05-30 03:55:32 +08:00
|
|
|
@pubSub.publishing(channel, message)
|