From c650a92a1d0b2815b280e338328fd46881a865da Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 16 Jul 2013 13:51:44 +0200 Subject: [PATCH] Various updates. --- src/collection.js | 13 ----- src/main.js | 132 +++++++++++++++++++++++++--------------------- src/xo.js | 76 +++++++++++++++++++++++--- 3 files changed, 141 insertions(+), 80 deletions(-) diff --git a/src/collection.js b/src/collection.js index b02380c..c62fd51 100644 --- a/src/collection.js +++ b/src/collection.js @@ -3,19 +3,6 @@ var Q = require('q'); ////////////////////////////////////////////////////////////////////// -// @todo Maybe we should generalize the getter methods (get, -// findWhere, where) to two methods: get([properties]) & -// first([properties]). -// -// Each of these methods accept optionnal properties to filter its -// results. -// -// get() returns any models that match while first() returns only the -// one. -// -// These method should also accept a scalar value as a matching value -// for the “id” property. - // @todo Add events. function Collection(models) { diff --git a/src/main.js b/src/main.js index 181bbf4..7c74386 100644 --- a/src/main.js +++ b/src/main.js @@ -79,22 +79,26 @@ function json_api_call(session, message) // JSON-RPC over WebSocket. ////////////////////////////////////////////////////////////////////// - // @todo Port should be configurable. -new (require('ws').Server)({'port': 8080}).on('connection', function (socket) { - var session = new Session(xo); - session.once('close', function () { - socket.close(); - }); +xo.on('started', function () { + // @todo Port should be configurable. + var server = new (require('ws').Server)({'port': 8080}); - socket.on('message', function (request) { - json_api_call(session, request).then(function (response) { - socket.send(response); - }).done(); - }); + server.on('connection', function (socket) { + var session = new Session(xo); + session.once('close', function () { + socket.close(); + }); - // @todo Ugly inter dependency. - socket.once('close', function () { - session.close(); + socket.on('message', function (request) { + json_api_call(session, request).then(function (response) { + socket.send(response); + }).done(); + }); + + // @todo Ugly inter dependency. + socket.once('close', function () { + session.close(); + }); }); }); @@ -102,61 +106,67 @@ new (require('ws').Server)({'port': 8080}).on('connection', function (socket) { // JSON-RPC over TCP. ////////////////////////////////////////////////////////////////////// -require('net').createServer(function (socket) { - var session = new Session(xo); - session.on('close', function () { - socket.end(); // @todo Check it is enough. - }); +xo.on('started', function () { + require('net').createServer(function (socket) { + var session = new Session(xo); + session.on('close', function () { + socket.end(); // @todo Check it is enough. + }); - var length = null; // Expected message length. - var buffer = new Buffer(1024); // @todo I hate hardcoded values! - socket.on('data', function (data) { - data.copy(buffer); + var length = null; // Expected message length. + var buffer = new Buffer(1024); // @todo I hate hardcoded values! + socket.on('data', function (data) { + data.copy(buffer); - // Read the message length. - if (!length) - { - var i = _.indexOf(buffer, 10); - if (-1 === i) + // Read the message length. + if (!length) + { + var i = _.indexOf(buffer, 10); + if (-1 === i) + { + return; + } + + length = +(buffer.toString('ascii', 0, i)); + + // If the length is NaN, we cannot do anything except + // closing the connection. + if (length !== length) + { + session.close(); + return; + } + + buffer = buffer.slice(i + 1); + } + + // We do not have received everything. + if (buffer.length < length) { return; } - length = +(buffer.toString('ascii', 0, i)); + json_api_call( + session, + buffer.slice(0, length).toString() + ).then(function (response) { + // @todo Handle long messages. + socket.write(response.length +'\n'+ response); + }).done(); - // If the length is NaN, we cannot do anything except - // closing the connection. - if (length !== length) - { - session.close(); - return; - } + // @todo Check it frees the memory. + buffer = buffer.slice(length); - buffer = buffer.slice(i + 1); - } + length = null; + }); - // We do not have received everything. - if (buffer.length < length) - { - return; - } + // @todo Ugly inter dependency. + socket.once('close', function () { + session.close(); + }); + }).listen(1024); // @todo Should be configurable. +}); - json_api_call( - session, - buffer.slice(0, length).toString() - ).then(function (response) { - // @todo Handle long messages. - socket.write(response.length +'\n'+ response); - }).done(); +////////////////////////////////////////////////////////////////////// - // @todo Check it frees the memory. - buffer = buffer.slice(length); - - length = null; - }); - - // @todo Ugly inter dependency. - socket.once('close', function () { - session.close(); - }); -}).listen(1024); // @todo Should be configurable. +xo.start(); diff --git a/src/xo.js b/src/xo.js index dfc0cfc..34c7de9 100644 --- a/src/xo.js +++ b/src/xo.js @@ -33,6 +33,11 @@ var check = function () { // Models ////////////////////////////////////////////////////////////////////// +var Server = Model.extend({ + 'validate': function () { + }, +}); + // @todo We could also give a permission level to tokens (<= // user.permission). var Token = Model.extend({ @@ -108,15 +113,26 @@ var User = Model.extend({ }, }); -var Server = Model.extend({ - 'validate': function () { - }, -}); +var Pool = Model.extend({}); + +var Host = Model.extend({}); + +var VM = Model.extend({}); + +var Network = Model.extend({}); + +var SR = Model.extend({}); + +var VDI = Model.extend({}); ////////////////////////////////////////////////////////////////////// // Collections ////////////////////////////////////////////////////////////////////// +var Servers = Collection.extend({ + 'model': Server, +}); + var Tokens = Collection.extend({ 'model': Token, @@ -148,8 +164,28 @@ var Users = Collection.extend({ } }); -var Servers = Collection.extend({ - 'model': Server, +var Pools = Collection.extend({ + 'model': Pool, +}); + +var Hosts = Collection.extend({ + 'model': Host, +}); + +var VMs = Collection.extend({ + 'model': VM, +}); + +var Networks = Collection.extend({ + 'model': Network, +}); + +var SRs = Collection.extend({ + 'model': SR, +}); + +var VDIs = Collection.extend({ + 'model': VDI, }); ////////////////////////////////////////////////////////////////////// @@ -161,10 +197,15 @@ function Xo() return new Xo(); } + //-------------------------------------- + // Main objects (@todo should be persistent). + this.servers = new Servers(); this.tokens = new Tokens(); this.users = new Users(); + // Temporary user, used for tests while the collection are not + // persistent. this.users.add({ 'email': 'bob@gmail.com', 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', @@ -184,9 +225,32 @@ function Xo() self.emit('user.revoked:'+ user_id); }); }); + + //-------------------------------------- + // Xen objects. + + this.pools = new Pools(); + this.hosts = new Hosts(); + this.vms = new VMs(); + + this.networks = new Networks(); + this.srs = new SRs(); + this.vdis = new VDIs(); + + // Connecting classes: VIF & PIF, VBD & SR. } require('util').inherits(Xo, require('events').EventEmitter); +Xo.prototype.start = function () { + // @todo Connect to persistent collection. + + // @todo Connect to Xen servers & fetch data. + + // ------------------------------------- + + this.emit('started'); +}; + ////////////////////////////////////////////////////////////////////// module.exports = Xo;