Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Petr Janda committed Dec 4, 2011
2 parents 1857091 + 692f690 commit ac3f1eb
Showing 2 changed files with 107 additions and 80 deletions.
85 changes: 41 additions & 44 deletions lib/ant.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/*
* ANTHILL.JS - distributed asynchronous library with publich / subscribe support
*/

/*
* Package dependencies.
*/
var dnode = require('dnode'),
util = require('util'),
EventEmitter = require('eventemitter2').EventEmitter2;

/*
@@ -18,45 +23,56 @@ var dnode = require('dnode'),
var Ant = module.exports = function(options) {
var self = this;

self.options = options || {};
options = options || {};

self.debug = true;
self.name = self.options['name'] || 'ant-' + Math.round(Math.random() * 10000);
self.port = self.options['port'] || 4000;
self._connections = {};

self.emitter = new EventEmitter({
wildcard: self.options['use-wildcard'] || true,
delimiter: self.options['delimiter'] || '.',
maxListeners: self.options['max-listeners'] || 100,
EventEmitter.call(this, {
wildcard: options['use-wildcard'] || true,
delimiter: options['delimiter'] || '.',
maxListeners: options['max-listeners'] || 100,
});

self.debug = options['debug'] || false;
self.name = options['name'] || 'ant-' + Math.round(Math.random() * 10000);
self.port = options['port'] || 4000;

self.server = dnode(function (client, conn) {
conn.on('end', function () {
self.emit("client.end", client.id);
});

this.on = function(name, cb) {
self.emitter.on(name, cb);
self.on(name, cb);
}

this.off = function(name, cb) {
self.off(name, cb);
}
});

self.server.on('connection', function (connection) {
self.emit('connection.open', connection);
});

self.server.on('ready', function () {
self.emit('ant.listening', self.post);
self.emit('ant.started', self);
self.server.on( 'ready', function () {
self.emit( 'ant.listening', self.post );
self.emit( 'ant.started', self );
});

self.init();
};

util.inherits( Ant, EventEmitter );

Ant.prototype.init = function() {
var self = this;

// Simple debugging.
if(self.debug) {
self.emitter.onAny(function(event) {
console.log(self.name + ' -> ' + event.name);
if( self.debug ) {
self.onAny( function(event) {
console.log( self.name + ' -> ' + event.name );
})
}
};
}

/*
* Emit an event with specified attributes for all subscribed listeners.
@@ -65,48 +81,29 @@ var Ant = module.exports = function(options) {
* @param {Object} Event content data.
*/
Ant.prototype.emit = function(name, data) {
var self = this;
self.emitter.emit(name, {
EventEmitter.prototype.emit.call(this, name, {
name: name,
data: data
});
};

/*
* Subscribe to the given ant for a given event. Other events emited by
* ant will not be delivered.
*
* @param {String} Ant name.
* @param {String} Event name.
*/
Ant.prototype.on = function(antPort, name, cb) {
var self = this;

self._connections[antPort].client.on(name, cb);
};

/*
* Connect to another ant.
*
* @param {String} Port, where the ant is running.
* @param {Function} Callback to be called after successful connect.
*/
Ant.prototype.connect = function(antPort, cb) {
Ant.prototype.connect = function(params, cb) {
var self = this;

dnode.connect(antPort, function (client, connection) {
dnode.connect(params, function (remote, connection) {
connection.on('end', function () {
self.emit('end', antPort);
self.emit('end', params);
});

self.emit('connect', antPort);

self._connections[antPort] = {
client: client,
connection: connection
};

cb(client);
self.emit('connect', params);

cb(remote);
});
}

102 changes: 66 additions & 36 deletions spec/antSpec.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
var Ant = require('../lib/ant');
var dnode = require('dnode'),
Ant = require('../lib/ant'),
EventEmitter = require('eventemitter2').EventEmitter2;

describe('Ant', function() {

var ant = null;

beforeEach(function() {
ant = new Ant();
ant.start();
describe('constructor', function() {
it( 'should be instance of EventEmitter', function() {
expect( new Ant() instanceof EventEmitter ).toBeTruthy();
})

it( 'should pass name', function() {
expect( new Ant({name: 'foo'}).name ).toEqual('foo');
})

it( 'should pass port', function() {
expect( new Ant({port: 9000}).port ).toEqual(9000);
})

it( 'should pass debug', function() {
expect( new Ant({debug: true}).debug ).toEqual(true);
})

it( 'should have debug disabled by default', function() {
expect( new Ant().debug ).toEqual(false);
})
})

afterEach(function() {
try {
ant.stop();
} catch(err) {}
})
describe( '', function() {
var ant = null;

it('server should be defined', function() {
expect(ant.server.server).toBeDefined();
})
beforeEach(function() {
ant = new Ant();
ant.start();
})

describe('.emit', function() {
it('server emit an event', function() {
var s = spyOn(ant.emitter, 'emit')
ant.emit('foo', {bar: 'baz'});

expect(s).toHaveBeenCalledWith('foo', {
name : 'foo',
data : { bar : 'baz' }
});
afterEach(function() {
try {
ant.stop();
} catch(err) {}
})
})

describe('.on', function() {
it('should call proper client on method', function() {
client = {
on: function() {}
}
it('server should be defined', function() {
expect(ant.server.server).toBeDefined();
})

describe('.emit', function() {
it('server emit an event', function() {
var s = spyOn(EventEmitter.prototype, 'emit')
ant.emit('foo', {bar: 'baz'});

ant._connections['ant-name'] = {
client: client
}
expect(s).toHaveBeenCalledWith('foo', {
name : 'foo',
data : { bar : 'baz' }
});
})
})

describe('.connect', function() {
it('should connect to dnode server', function() {
var s = spyOn( dnode, 'connect' )
ant.connect( 1000, function() {} )
expect(s).toHaveBeenCalled();
})

var s = spyOn(client, 'on')
it('should connect to dnode server with given port', function() {
var s = spyOn( dnode, 'connect' )
ant.connect( '4000', function() {} )
expect(s).toHaveBeenCalledWith('4000', jasmine.any(Function));
})

var f = function() {}
ant.on('ant-name', 'foo', f);
expect(s).toHaveBeenCalledWith('foo', f)
it('should connect to dnode server with given host and port', function() {
var params = { host: '192.168.0.1', port: 3000 },
s = spyOn( dnode, 'connect' );

ant.connect( params, function() {} )
expect(s).toHaveBeenCalledWith( params, jasmine.any(Function) );
})
})
})
})

0 comments on commit ac3f1eb

Please sign in to comment.