Skip to content

Commit

Permalink
Merge branch 'feature/cm-ide-proxy' into feature/JRD-342-mc-aciona-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Aline Takechi committed Sep 22, 2015
2 parents ba14b22 + 3401deb commit 4a84fef
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 127 deletions.
67 changes: 20 additions & 47 deletions app/controllers/file.js
Original file line number Diff line number Diff line change
@@ -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;
};
118 changes: 44 additions & 74 deletions app/lib/code-machine-cli.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
};

Expand Down
9 changes: 3 additions & 6 deletions app/routes/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4a84fef

Please sign in to comment.