Skip to content

Commit

Permalink
Work on the protocol parser
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Feb 19, 2012
1 parent 5140534 commit 30ce0e7
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 7 deletions.
94 changes: 92 additions & 2 deletions lib/protocol/1.js
Expand Up @@ -4,7 +4,52 @@
* MIT Licensed
*/

var EventEmitter = require('events').EventEmitter;
var EventEmitter = this.EventEmitter || require('events').EventEmitter
, keys = Object.keys || (function polyfill () {
var has = Object.prototype.hasOwnProperty
, hasDontEnumBug = !{ toString: null }.propertyIsEnumerable('toString')
, DontEnums = [
'toString'
, 'toLocaleString'
, 'valueOf'
, 'hasOwnProperty'
, 'isPrototypeOf'
, 'propertyIsEnumerable'
, 'constructor'
]
, DontEnumsLength = DontEnums.length;

/**
* Return all the keys of the given object.
*
* @param {Object} o
* @returns {Array}
* @api public
*/

return function keys (o) {
if (typeof o !== 'object' && typeof o !== 'function' || o === null) {
throw new TypeError('Object.keys called on a non-object');
}

var result = [];
for (var name in o) {
if (has.call(o, name)) {
result.push(name);
}
}

if (hasDontEnumBug) {
for (var i = 0; i < DontEnumsLength; i++) {
if (has.call(o, DontEnums[i])) {
result.push(DontEnums[i]);
}
}
}

return result;
};
})();

/**
* The protocol.
Expand All @@ -20,9 +65,38 @@ function Protocol (encode, decode) {

this.encode = encode || JSON.stringify;
this.decode = decode || JSON.parse;

EventEmitter.call(this);
}

Protocol.prototype.__proto__ = EventEmitter.prototype;
Protocol.prototype = new EventEmitter;
Protocol.prototype.constructor = Protocol;

/**
* A object of all protocol types with a mapping to it's index.
*
* @type {Object}
* @api private
*/

Protocol.types = {
'disconnect': 0 // disconnect initiated by server
, 'connect': 1 // successfull connection
, 'heartbeat': 2 // heartbeat signal
, 'codec': 3 // JavaScript Object encoded with a codec
, 'event': 4 // event for EventEmitter
, 'reboot': 5 // the server is rebooting
, 'error': 6 // error
};

/**
* A list of protocol types.
*
* @type {Array}
* @api private
*/

Protocol.typelist = keys(Protocol.types);

/**
* Encode the message.
Expand Down Expand Up @@ -54,8 +128,24 @@ Protocol.prototype.decode = function decode (message) {
* @api private
*/

Protocol.regexp = /(\d+)#(\d+)?#(\d+)?#/;
Protocol.prototype.parse = function parse () {
var data
, type
, id
, length
, message;

while (this.queue.length) {
data = this.queue.match(Protocol.regexp);

// we don't have enough data yet to parse out the wire format
// or we don't have enough data yet to splice out all the information
if (!data || this.queue.length < +data[2]) break;

}

this.emit('message');
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/red.js
Expand Up @@ -358,6 +358,7 @@ RED.prototype.transport = function transport (data, request, response, head) {
sessionid: data.session
, connectionid: data.id
, specification: data.query.callback || +data.query.specification || 0
, protocol: new Protocol[data.protocol]
});

// initialize the request
Expand Down
13 changes: 10 additions & 3 deletions tests/common.js
Expand Up @@ -8,9 +8,16 @@
* Expose globals that we would be using in most of the test suites.
*/

RED = require('../');
Engine = require('../lib/engine');
request = require('request');
global.RED = RED = require('../');
global.Engine = Engine = require('../lib/engine');
global.Protocols = Protocols = require('../lib/protocol');

/**
* Global dependencies
*/

global.should = should = require('should');
global.require = request = require('request');

/**
* Helper tools
Expand Down
6 changes: 4 additions & 2 deletions tests/engine.test.js
Expand Up @@ -66,13 +66,15 @@ describe('engine.js', function () {
hostname: 'wtftrololol'
});

engine.connect();

engine.on('error', function () {});
engine.once('connect failed', function (err) {
err.message.should.equal('Failed to establish a connection');

engine.close();
next();
});

engine.connect();
});
});

Expand Down
31 changes: 31 additions & 0 deletions tests/protocol.1.test.js
@@ -0,0 +1,31 @@
describe('Protocol.1', function () {
var Protocol = Protocols['1'];

it('should be exported as a function', function () {
Protocol.should.be.a('function');
});

it('should initialize without any issues', function () {
var parser = new Protocol();
});

it('should be a instance of EventEmitter', function () {
var parser = new Protocol();

if (!(parser instanceof process.EventEmitter)) {
should.fail('should be an EventEmitter');
}
});

describe('#encode', function () {

});

describe('#decode', function () {

});

describe('#stream', function () {

});
});
11 changes: 11 additions & 0 deletions tests/ssl/red-csr.pem
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEh
MB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB
AQUAA4GNADCBiQKBgQDDPFU8twQqH545fN4pdFQXGog/LvfrVpLdv+UBOuJkB6bP
evhR/WUn31uovmHMD5cKWvtc2zPH1ERYn/OLf6CnDrQQYmrbtiVB/Tzk/0RDrFei
DAHfxvtq9phuWjyqEOm1UzRAy/HZJjdrXgTF1xwJPBl+6nMTMQ+ddUAKICj+aQID
AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAfsCljBwOpPI7fhyWtpOW/MXo2VRBd1av
wj+jr1fzCskshEHJeOM1CS2s2JjaN+I8ygSZBsE8gIIxT5Eu2S+fEY3FjDXlJk25
hMZP03yKmNav1ULKbVwx05H0wGD1ht3xcDoY+OHfuLVYX/iQ8tCAXBEmmd0lX12X
o/Vc7IoVAuI=
-----END CERTIFICATE REQUEST-----
3 changes: 3 additions & 0 deletions transports/transport.js
Expand Up @@ -35,6 +35,9 @@ function Transport (engine, response, options) {
// restrictions
this.maxiumBuffer = 38400;

// protocol parser
this.protocol = null;

_.extend(this, options || {});

this.engine = engine;
Expand Down

0 comments on commit 30ce0e7

Please sign in to comment.