2014-04-14 07:39:18 +08:00
|
|
|
postal = require('postal')
|
|
|
|
crypto = require 'crypto'
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
config = require '../config'
|
|
|
|
|
|
|
|
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-24 05:37:19 +08:00
|
|
|
callback(msg.err, msg.data)
|
2014-04-24 04:18:25 +08:00
|
|
|
).once()
|
|
|
|
|
|
|
|
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
|