From 3401debadb0b4396554dc76dbb1f55df9b774318 Mon Sep 17 00:00:00 2001 From: Guilherme Marques Date: Mon, 21 Sep 2015 12:12:52 -0300 Subject: [PATCH] [JRD-480] Modified CM client class, created proxy functionality for CM websocket. --- app/controllers/file.js | 67 ++++++-------------- app/lib/code-machine-cli.js | 118 ++++++++++++++---------------------- app/routes/file.js | 9 +-- 3 files changed, 67 insertions(+), 127 deletions(-) diff --git a/app/controllers/file.js b/app/controllers/file.js index 4cc5ab3..c5293d3 100644 --- a/app/controllers/file.js +++ b/app/controllers/file.js @@ -1,68 +1,41 @@ 'use strict'; -/** - * @todo Get cm from user context - */ module.exports = function (app) { - this.create = function (socket) { + /** + * On connection, register listeners to commands that the client (IDE) may + * emit to the code machine, and re-emits them to the CM connection. + */ + this.registerCommands = function (socket) { if (!app.cm) { + console.log('No instance of Code Machine found!'); return; } - socket.on('create', function () { - var file = app.cm.create(); - socket.emit('created', file); - }); - }; - this.list = function (socket) { - if (!app.cm) { - return; - } - socket.on('list', function () { - var list = app.cm.list(); - socket.emit('listed', list); + app.cm.commands.forEach(function (cmd) { + socket.on(cmd, app.cm.emit.bind(app.cm, cmd)); }); }; - this.delete = function (socket) { + /** + * On connection, register listeners to events that the code machine may + * emit, and re-emits them to the socket with client (IDE). + */ + this.registerEvents = function (socket) { if (!app.cm) { + console.log('No instance of Code Machine found!'); return; } - socket.on('delete', function () { - var res = app.cm.del(); - socket.emit('deleted', res); - }); - }; - this.apNode = function (socket) { - if (!app.cm) { - return; - } - socket.on('apNode', function () { - var node = app.cm.apNode(); - socket.emit('added', node); - }); - }; + app.cm.events.forEach(function (ev) { + var listener = socket.emit.bind(socket, ev); - this.rmNode = function (socket) { - if (!app.cm) { - return; - } - socket.on('rmNode', function () { - var res = app.cm.rmNode(); - socket.emit('removed', res); + app.cm.on(ev, listener); + socket.on('disconnect', function () { + app.cm.removeListener(ev, listener); + }); }); }; - this.edNodeAtt = function (socket) { - if (!app.cm) { - return; - } - socket.on('edNodeAtt', function () { - var res = app.cm.edNodeAtt(); - socket.emit('changed', res); - }); - }; return this; }; diff --git a/app/lib/code-machine-cli.js b/app/lib/code-machine-cli.js index f11a00b..09e127a 100644 --- a/app/lib/code-machine-cli.js +++ b/app/lib/code-machine-cli.js @@ -1,13 +1,22 @@ 'use strict'; var request = require('request'); +var io = require('socket.io-client'); +var EventEmitter = require('events').EventEmitter; +var util = require('util'); /** - * This class represents the Code Machine in the interactions + * Creates a connection to a code machine instance. The connection's instance + * proxies websocket events through an event emitter. CM.prototype.commands + * lists all events the code machine listens to, and which can be emitted + * through the connection instance. CM.prototype.events lists all events the + * code machine may emit (and to which the mission control may add listeners to + * the event emitter). * * @class CM - * @param {Object} req - Request object - * @param {string} req.url - Path for the file - * @param {Object} res - Response containing the requested file + * @param {Object} container - represents the code machine instance. + * @param {string} container.markedURL - URL where the CM serves marked files. + * @param {string} container.cleanURL - URL where the CM serves clean files. + * @param {string} container.wsURL - URL for websocket connection to the CM. */ var CM = function (container) { this.container = container; @@ -19,83 +28,42 @@ var CM = function (container) { if (!this.container.cleanURL) { throw 'Container must contain clean url.'; } - return this; -}; -/** - * Creates reference for CM - * - * @todo Implement... - * @return {Object} obj.uuid Containing the Id of the CM - */ -CM.prototype.create = function () { - return { - uuid: 'bla', - }; -}; + this.ws = io.connect(container.wsURL); -/** - * Lists reference - * - * @todo Implement... - * @return {Array} views Containing a list of references - */ -CM.prototype.list = function () { - return { - views: [ - {uuid: 1}, - {uuid: 2}, - ], - }; -}; + this.ws.on('connect', function () { + for (var i in this.events) { + var ev = this.events[i]; + this.ws.on(ev, this.emit.bind(this, ev)); + } + }.bind(this)); -/** - * Deletes reference - * - * @todo Implement... - * @return {Object} obj - */ -CM.prototype.del = function () { - return { - good: 'bye', - }; -}; + for (var i in this.commands) { + var cmd = this.commands[i]; + this.on(cmd, this.ws.emit.bind(this.ws, cmd)); + } -/** - * Appends a Node - * - * @todo Implement... - * @return {Object} obj - */ -CM.prototype.apNode = function () { - return { - uuid: '8hRSD7uyDm8302', - }; + EventEmitter.call(this); + return this; }; -/** - * Removes a Node - * - * @todo Implement... - * @return {Object} obj - */ -CM.prototype.rmNode = function () { - return { - good: 'bye', - }; -}; +util.inherits(CM, EventEmitter); + +// Events which the code machine is listening to. May be emitted in the instance +CM.prototype.commands = [ + 'command:insertElementAtXPath', + 'command:insertElement', +]; + +// Events which the code machine may emit. Will be re-emitted in the instance +CM.prototype.events = [ + 'control:contentUpdate', + 'command:insertElementAtXPath/error', + 'command:insertElementAtXPath/success', + 'command:insertElement/error', + 'command:insertElement/success', +]; -/** - * Edits a Node - * - * @todo Implement... - * @return {Object} obj - */ -CM.prototype.edNodeAtt = function () { - return { - it: 'is done', - }; -}; /** * Handles request for a Marked file @@ -117,7 +85,9 @@ CM.prototype.marked = function (req, res) { * @param {Object} res - Response containing the requested file */ CM.prototype.clean = function (req, res) { + console.log(req.url); var cmURL = this.container.cleanURL + req.url; + console.log(cmURL); req.pipe(request(cmURL)).pipe(res); }; diff --git a/app/routes/file.js b/app/routes/file.js index 8439460..a176449 100644 --- a/app/routes/file.js +++ b/app/routes/file.js @@ -3,15 +3,12 @@ module.exports = function (app) { var file = app.controllers.file; + // @todo refactor route to a better name. app.ws .of('/file') .on('connection', function (socket) { - file.create(socket); - file.list(socket); - file.delete(socket); - file.apNode(socket); - file.rmNode(socket); - file.edNodeAtt(socket); + file.registerEvents(socket); + file.registerCommands(socket); }); return this;