cleaning up

This commit is contained in:
Anton Georgiev 2014-07-02 17:57:07 +00:00
parent 22785c12bc
commit c40bcece8f
5 changed files with 0 additions and 281 deletions

View File

@ -1,30 +0,0 @@
###
if Meteor.isServer
console.log " I am in the server"
Meteor.startup ->
console.log "On startup in the server"
console.log Meteor.config.appName
#a = new Meteor.ClientProxy()
#b = new Meteor.RedisPubSub()
# Module to store the modules registered in the application
Meteor.config.modules = modules = new Meteor.Modules()
# Router
#config.modules.register "MainRouter", new MainRouter()
# Application modules
Meteor.config.modules.register "RedisPubSub", new Meteor.RedisPubSub()
#Meteor.config.modules.register "MessageBus", new Meteor.MessageBus()
#Meteor.config.modules.register "Controller", new Controller()
clientProxy = new Meteor.ClientProxy()
Meteor.config.modules.register "ClientProxy", clientProxy
###############clientProxy.listen(app)
###

View File

@ -1,93 +0,0 @@
socketio = Meteor.require('socket.io')
MESSAGE = "message"
moduleDeps = ["Controller"]
class Meteor.ClientProxy
constructor: ->
Meteor.config.modules.wait moduleDeps, =>
@controller = Meteor.config.modules.get("Controller")
# Listens for events on the websocket and does something when they are triggered.
listen: (app) ->
@io = socketio.listen(app)
@io.set('log level', 1)
@io.sockets.on 'connection', (socket) =>
log.debug({ client: socket.id }, "Client has connected.")
socket.on 'message', (jsonMsg) =>
log.debug({ message: jsonMsg }, "Received message")
@_handleMessage(socket, jsonMsg)
socket.on 'disconnect', =>
@_handleClientDisconnected socket
# Sends a message in `data` to all the clients that should receive it.
sendToClients: (data, callback) ->
#log.debug({ data: data }, "Sending to client")
# the channel can be the user_id (send to one user only) or the meeting_id
# (send to everyone in the meeting)
channel = data?.payload?.user_id or data?.payload?.meeting_id
# if the data has "header":{"name":"some_event_name"} use that name
# otherwise look for "name":"some_event_name" in the top level of the data
eventName = data?.header?.name or data?.name
# clients = @io.sockets.clients(channel)
# console.log "Found", clients?.length, "clients for the channel", channel
#log.debug({ channel: channel, eventName: eventName, message: data, clientCount: clients?.length },
# "Sending message to websocket clients")
# TODO: if `channel` is undefined, it should not send the message,
# instead if is sending to all users
@io.sockets.in(channel).emit(eventName, data)
callback?()
_handleClientDisconnected: (socket) ->
console.log "\ntrying to disconnect"
#if socket.userId?
# log.info("User [#{socket.userId}] has disconnected.")
_handleMessage: (socket, message) ->
if message.header?.name?
@_handleValidMessage(socket, message)
else
log.error({ message: message }, "Invalid message.")
_handleValidMessage: (socket, message) =>
switch message.header.name
when 'validate_auth_token'
@_handleLoginMessage socket, message
when 'send_public_chat_message'
@controller.sendingChat message
when 'user_leaving_request'
@controller.sendingUsersMessage message
else
log.error({ message: message }, 'Unknown message name.')
_handleLoginMessage: (socket, data) ->
@controller.processAuthMessage(data, (err, result) ->
if err?
log.debug({ message: result }, "Sending authentication not OK to user and disconnecting socket")
sendMessageToClient(socket, result)
socket.disconnect()
else
log.debug({ userChannel: result.payload.user_id, meetingChannel: result.payload.meeting_id },
"Subscribing a user to his channels")
socket.join(result.payload.user_id)
socket.join(result.payload.meeting_id)
# assign the userId to this socket. This way we can
# locate this socket using the userId.
socket.userId = result?.payload?.user_id
log.debug({ message: result }, "Sending authentication OK reply to user")
sendMessageToClient(socket, result)
)
sendMessageToClient = (socket, message) ->
socket.emit(MESSAGE, message)

View File

@ -1,39 +0,0 @@
moduleDeps = ["MessageBus", "ClientProxy"]
class Meteor.Controller
constructor: ->
config.modules.wait moduleDeps, =>
@messageBus = config.modules.get("MessageBus")
@clientProxy = config.modules.get("ClientProxy")
@messageBus.receiveMessages (data) =>
@processReceivedMessage(data)
processReceivedMessage: (data, callback) ->
@clientProxy.sendToClients(data, callback)
# 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) ->
if err?
log.error({ reason: err, result: result, original: data }, "Authentication failure")
callback(err, null)
else
if result.payload?.valid
log.info({ result: result }, "Authentication successful")
callback(null, result)
else
log.info({ result: result }, "Authentication failure")
callback(new Error("Authentication failure"), null)
# processEndMessage: (data, callback) ->
# @clientProxy.endMeeting()
sendingChat: (data) =>
@messageBus.sendingToRedis(config.redis.channels.toBBBApps.chat, data)
sendingUsersMessage: (data) =>
@messageBus.sendingToRedis(config.redis.channels.toBBBApps.users, data)

View File

@ -1,40 +0,0 @@
crypto = Meteor.require 'crypto'
postal = Meteor.require 'postal'
moduleDeps = ["RedisPubSub"]
class Meteor.MessageBus
constructor: ->
Meteor.config.modules.wait moduleDeps, =>
@pubSub = Meteor.config.modules.get("RedisPubSub")
receiveMessages: (callback) ->
postal.subscribe
channel: Meteor.config.redis.internalChannels.receive
topic: "broadcast"
callback: (msg, envelope) ->
callback(msg)
sendAndWaitForReply: (data, callback) ->
replyTo =
channel: Meteor.config.redis.internalChannels.reply
topic: 'get.' + crypto.randomBytes(16).toString('hex')
postal.subscribe(
channel: replyTo.channel
topic: replyTo.topic
callback: (msg, envelope) ->
callback(null, msg)
).once()
log.info({ message: data, replyTo: replyTo }, "Sending a message and waiting for reply")
postal.publish
channel: Meteor.config.redis.internalChannels.publish
topic: 'broadcast'
replyTo: replyTo
data: data
sendingToRedis: (channel, message) =>
@pubSub.publishing(channel, message)

View File

@ -1,79 +0,0 @@
_ = Meteor.require('lodash')
EventEmitter = Meteor.require('events').EventEmitter
# Helper class to register and store modules.
# It stores a list of objects in an object literal indexed by the name of the module.
# Includes methods for a class to ask for a module and wait until it is ready.
#
# This class is used to prevent errors when modules are required in an order that
# would generate a circular dependency. In these cases, the objects might not be loaded
# entirely.
# @see http://nodejs.org/api/modules.html#modules_cycles
#
# With this class, the class requiring a module can wait until the module is properly
# loaded. More than that, it will return an instanced object, not only a reference to
# a class (as usually done when using `require`).
#
# @example How to use it
# modules = new Modules()
# modules.wait ["db"], ->
# # this callback will only be called when the module "db" is registered
# db = config.modules.get("db")
# ...
# # calls to register a module can be made anywhere in the application
# modules.register "db", new Database()
#
class Meteor.Modules extends EventEmitter
constructor: ->
# the list of modules registered:
@modules = {}
# list of callbacks waiting for a module to be registered:
@callbacks = []
# Registers a new module with the name in `name` and the content in `object`.
# @param name [string] the name of the module
# @param object [string] the instance of the module
# @return [object] the same object in the parameter `object`
register: (name, object) ->
@modules[name] = object
@_checkCallbacks()
object
# Blocks until a list of modules is registered.
# @param names [Array] an array of strings with the names of all modules that should be waited for
# @param callback [Function] will be called when *all* modules in `names` are registered
wait: (names, callback) ->
names = [names] unless _.isArray(names)
@callbacks.push {modules: names, fn: callback}
@_checkCallbacks()
# Returns the module with the name in `name`.
# @param name [string] the name of the module to be returned
# @return [object] the module asked for
get: (name) ->
@modules[name]
# Returns the list of all modules registered.
# @return [Array] all modules registered
all: ->
@modules
# Run through the list of registered callbacks in `@callbacks` to see if any of
# them are ready to be called (if all modules waited for are available).
# @private
_checkCallbacks: () ->
toRemove = []
for cb in @callbacks
done = true
for name in cb.modules
unless @modules[name]?
done = false
break
if done
cb.fn()
toRemove.push cb
@callbacks = _.filter(@callbacks, (i) -> i not in toRemove)