2014-04-24 04:18:25 +08:00
|
|
|
config = require '../config'
|
2014-04-25 05:33:59 +08:00
|
|
|
log = require './bbblogger'
|
2014-04-14 22:58:20 +08:00
|
|
|
|
2014-04-24 06:27:43 +08:00
|
|
|
moduleDeps = ["MessageBus", "ClientProxy"]
|
2014-04-14 22:58:20 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
module.exports = class Controller
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
config.modules.wait moduleDeps, =>
|
|
|
|
@messageBus = config.modules.get("MessageBus")
|
2014-04-24 06:27:43 +08:00
|
|
|
@clientProxy = config.modules.get("ClientProxy")
|
2014-04-24 04:18:25 +08:00
|
|
|
|
2014-04-24 06:27:43 +08:00
|
|
|
@messageBus.receiveMessages (data) =>
|
|
|
|
@processReceivedMessage(data)
|
|
|
|
|
|
|
|
processReceivedMessage: (data, callback) ->
|
|
|
|
@clientProxy.sendToClients(data, callback)
|
2014-04-24 04:18:25 +08:00
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
# Processes a message requesting authentication
|
|
|
|
processAuthMessage: (data, callback) ->
|
|
|
|
log.info({ data: data }, "Sending an authentication request and waiting for reply")
|
|
|
|
@messageBus.sendAndWaitForReply data, (err, result) ->
|
2014-04-24 04:18:25 +08:00
|
|
|
if err?
|
2014-04-25 05:33:59 +08:00
|
|
|
log.error({ reason: err, result: result, original: data }, "Authentication failure")
|
2014-04-24 04:18:25 +08:00
|
|
|
callback(err, null)
|
2014-04-14 07:39:18 +08:00
|
|
|
else
|
2014-04-25 05:33:59 +08:00
|
|
|
if result.payload?.valid
|
|
|
|
log.info({ result: result }, "Authentication successful")
|
|
|
|
callback(null, result)
|
2014-04-24 04:18:25 +08:00
|
|
|
else
|
2014-04-25 05:33:59 +08:00
|
|
|
log.info({ result: result }, "Authentication failure")
|
|
|
|
callback(new Error("Authentication failure"), null)
|
2014-04-24 04:18:25 +08:00
|
|
|
|
|
|
|
# processEndMessage: (data, callback) ->
|
|
|
|
# @clientProxy.endMeeting()
|