- trying to setup message handlers

This commit is contained in:
Richard Alam 2014-04-13 16:39:18 -07:00
parent bee1320463
commit 8d50ea3e7f
7 changed files with 266 additions and 2 deletions

View File

@ -0,0 +1,14 @@
postal = require('postal')
{SocketIoMessageHandler} = require './lib/clientproxy'
crypto = require 'crypto'
io = require('socket.io').listen(3019)
sio = new SocketIoMessageHandler(io)

View File

@ -0,0 +1,17 @@
bunyan = require 'bunyan'
logger = bunyan.createLogger({
name: 'bbbnode',
streams: [
{
level: 'debug',
stream: process.stdout,
},
{
level: 'info',
path: '/var/log/bigbluebutton/bbbnode.log'
}
]
});
module.exports = logger;

View File

@ -0,0 +1,63 @@
{Controller} = require './controller'
log = require './bbblogger'
MESSAGE = "message"
class SocketIoMessageHandler
constructor: (io) ->
@io = io
@controller = new Controller(@)
@initialize()
initialize: () ->
@io.sockets.on('connection', (socket) ->
console.log("Client has connected.")
log.debug("LOG: Client has connected.")
socket.on('message', (jsonMsg) ->
log.debug("Received message #{jsonMsg}")
handleMessage(socket, @controller, jsonMsg)
)
socket.on('disconnect', () ->
handleClientDisconnected socket
)
)
exports.SocketIoMessageHandler = SocketIoMessageHandler
handleClientDisconnected = (socket) ->
log.debug("LOG: Client has disconnected.")
handleMessage = (socket, controller, message) ->
if message.name?
handleValidMessage(socket, controller, message)
else
log.error({error: "Invalid message", message: JSON.stringify(message)})
handleValidMessage = (socket, controller, message) ->
switch message.name
when 'authenticateMessage'
handleLoginMessage socket, controller, message
when 'Sandra'
connectJackNumber 22
when 'Toby'
connectJackNumber 9
else
console.log('I am sorry, your call cannot be connected')
handleLoginMessage = (socket, controller, data) ->
controller.processLoginMessage(data, (err, result) ->
if (err)
message = {name: "authenticationReply", error: err}
sendMessageToClient message
else
message = {name: "authenticationReply", data: result}
sendMessageToClient message
)
sendMessageToClient = (socket, message) ->
socket.emit(MESSAGE, JSON.stringify(message))

View File

@ -0,0 +1,22 @@
bus = require './messagebus'
logger = require './bbblogger'
class Controller
constructor: (clientProxy) ->
@proxy = clientProxy
processLoginMessage = (data, callback) ->
bus.sendMessage(data, (err, result) ->
if (err)
errLog = {message: "Authentication Failure", reason: err, data: data}
log.error(JSON.stringify(errLog))
callback(err, null)
else
console.log("SUCCESS: #{result}")
if result.error?
callback(result.error, null)
else
callback(null, result.data)
)
exports.Controller = Controller

View File

@ -0,0 +1,50 @@
postal = require('postal')
redisrpc = require './redispubsub'
crypto = require 'crypto'
io = require('socket.io').listen(3009)
io.sockets.on('connection', (socket) ->
console.log("Connected")
socket.on('login', (data) ->
console.log("User login #{data}")
sendMessage(data, (err, result) ->
if (err)
console.log("ERROR: #{err}")
else
console.log("SUCCESS: #{result}")
socket.emit('authenticated', JSON.stringify(result))
)
)
socket.on('event', (data) ->
console.log("Server received event.")
)
socket.on('disconnect', () ->
console.log("Server disconnect event.")
)
socket.emit('ready')
)
sendMessage = (data, callback) ->
replyTo = {
channel: 'model.data',
topic: 'get.' + crypto.randomBytes(16).toString('hex')
};
postal.subscribe({
channel: replyTo.channel,
topic: replyTo.topic,
callback: (msg, envelope) ->
callback( msg.err, msg.data )
}).once()
postal.publish({
channel: 'publishChannel',
topic: 'broadcast',
replyTo: replyTo,
data: data
})

View File

@ -0,0 +1,95 @@
redis = require 'redis'
crypto = require 'crypto'
postal = require 'postal'
# default timeout to wait for response
TIMEOUT = 5000;
pubClient = redis.createClient()
subClient = redis.createClient()
# hash to store requests waiting for response
pendingRequests = {};
initialize = () ->
postal.subscribe({
channel: 'publishChannel',
topic: 'broadcast',
callback: ( msg, envelope ) ->
if (envelope.replyTo?)
sendAndWaitForReply(msg, envelope)
else
sendMessage(msg, envelope)
})
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
error = {code: "503", message: "Waiting for reply timeout.", description: "Waiting for reply timeout."}
response.err = error
postal.publish({
channel: envelope.replyTo.channel,
topic: envelope.replyTo.topic,
data: response
})
# delete the entry from hash
delete pendingRequests[correlationId];
, TIMEOUT, correlationId)
# 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;
console.log("Publishing #{message}")
message.correlationId = correlationId
pubClient.publish("bigbluebuttonAppChannel", JSON.stringify(message))
subClient.on("subscribe", (channel, count) ->
console.log("Subscribed to #{channel}")
)
subClient.on("message", (channel, jsonMsg) ->
console.log("Received message on [channel] = #{channel} [message] = #{jsonMsg}")
message = JSON.parse(jsonMsg)
if (message.correlationId?)
#retreive the request entry
entry = pendingRequests[message.correlationId];
#make sure we don't timeout by clearing it
clearTimeout(entry.timeout);
#delete the entry from hash
delete pendingRequests[message.correlationId];
response = {}
response.data = message
postal.publish({
channel: entry.replyTo.channel,
topic: entry.replyTo.topic,
data: response
})
else
if message.meetingId?
postal.publish({
channel: "bigbluebuttonAppChannel"
topic: message.meetingId,
data: message
})
else
console.log("Invalid message: #{jsonMsg}")
)
initialize()
console.log("RPC: Subscribing message on channel [responseChannel]")
subClient.subscribe("responseChannel")

7
client/bbb-html5-client/package.json Normal file → Executable file
View File

@ -9,8 +9,11 @@
"connect-redis": "1.4.5",
"connect": "2.8.5",
"socket.io": "0.9.16",
"redis": "0.9.0",
"hiredis": "0.1.15",
"postal" : "0.9.0-rc1",
"redis": "0.10.1",
"hiredis": "0.1.16",
"bunyan": "0.22.2",
"crypto-js": "3.1.2-3",
"sanitizer": "0.0.15",
"hat": "0.0.3",
"imagemagick": "0.1.3",