Skip to content

Commit

Permalink
Cleanup, bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
fed135 committed Mar 29, 2016
1 parent f1118db commit f10f758
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 43 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

A library to simplify and optimize your Socket communications.

- Packet bundling
- Packet minification
- Easy-to-use single syntax for all protocols
- Multiplexing for everyone!


## Adapters

Allow you to easily use different socket types, hassle-free
Expand Down Expand Up @@ -41,7 +47,7 @@ Perform batch operation of payloads.

---

The framework is flexible enough so that you can load your own custom adapters, encoders or middlewares - say you wanted support for protocols like zmq or yaml encoding.
The framework is flexible enough so that you can load your own custom adapters, encoders or middlewares - say you wanted support for protocols like zmq, WebSockets or have yaml encoding.


## Usage
Expand Down Expand Up @@ -71,7 +77,6 @@ The framework is flexible enough so that you can load your own custom adapters,
});

server.on('connection', function(client) {} // Handler, where client is an instance of Kalm.Client

server.broadcast('someOtherEvent', 'hello!');


Expand Down
20 changes: 15 additions & 5 deletions src/client.js → src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

/* Requires ------------------------------------------------------------------*/

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var debug = require('debug')('kalm');

var adapters = require('./adapters');
Expand All @@ -22,6 +25,8 @@ var middleware = require('./middleware');
* @param {object} options The configuration options for the client
*/
function Client(options) {
EventEmitter.call(this);

options = options || {};

this.options = {
Expand Down Expand Up @@ -49,9 +54,6 @@ function Client(options) {

// Data packets - transient state - by channel
this._packets = {};

// Init
this._updateSocket();
}

/**
Expand All @@ -61,11 +63,17 @@ function Client(options) {
* @param {string|null} name The name of the channel.
* @returns {Channel} The recovered or created channel
*/
Client.prototype.on = function(name, handler) {
Client.prototype.channel = function(name, handler) {
name = name || '/';

if (name[0] !== '/') name = '/' + name;

if (!(name in this._channels)) {
debug('log: New Channel ' + name);
debug(
'log: new channel ' +
this.options.adapter + '://' + this.options.hostname + ':' +
this.options.port + name
);
this._channels[name] = [];
}

Expand Down Expand Up @@ -105,6 +113,8 @@ Client.prototype._handleRequest = function(evt, data) {
console.log(encoders[this.options.encoder].decode(evt || data));
};

util.inherits(Client, EventEmitter);

/* Exports -------------------------------------------------------------------*/

module.exports = Client;
40 changes: 24 additions & 16 deletions src/server.js → src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

/* Requires ------------------------------------------------------------------*/

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var debug = require('debug')('kalm');
var Signal = require('signals');

var Client = require('./Client');
var adapters = require('./adapters');
var encoders = require('./encoders');

Expand All @@ -22,6 +25,8 @@ var encoders = require('./encoders');
* @param {object} options The configuration options for the server
*/
function Server(options) {
EventEmitter.call(this);

options = options || {};

this.status = 'off';
Expand All @@ -31,15 +36,7 @@ function Server(options) {
port: options.port || 80
};

this.onReady = new Signal();

if (this.options.adapter in adapters) {
this._server = new adapters[this.options.adapter](this.options, this._handleRequest.bind(this));
}
else {
// Custom adapter
this._server = new this.options.adapter(this.options, this._handleRequest);
}
this.listen();
}

/**
Expand All @@ -48,20 +45,31 @@ function Server(options) {
* @memberof Server
*/
Server.prototype.listen = function(callback) {
var _self = this;
if (this._server) {
this._server.listen(callback);
var adapter = adapters.resolve(this.options.adapter);
if (adapter) {
debug('log: listening ' + this.options.adapter + '://0.0.0.0:' + this.options.port);
adapter.listen(this, this._handleLift.bind(this));
}
else {
debug('error: no adapter found "' + this.options.adapter + '"');
}
};

Server.prototype._handleLift = function() {
this.emit('ready');
};

Server.prototype.stop = function() {
if (this._server) this._server.stop();
if (this.listener) this.listener.stop();
};

Server.prototype._handleRequest = function(evt, data) {
console.log(encoders[this.options.encoder].decode(evt || data));
Server.prototype._handleRequest = function(socket) {
console.log(socket);
this.emit('connection', new Client(socket));
};

util.inherits(Server, EventEmitter);

/* Exports -------------------------------------------------------------------*/

module.exports = Server;
4 changes: 1 addition & 3 deletions src/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
var ipc = require('./ipc.adapter');
var tcp = require('./tcp.adapter');
var udp = require('./udp.adapter');
var ws = require('./ws.adapter');

var debug = require('debug')('kalm');

Expand All @@ -19,8 +18,7 @@ var debug = require('debug')('kalm');
var list = {
ipc: ipc,
tcp: tcp,
udp: udp,
ws: ws
udp: udp
};

/* Methods -------------------------------------------------------------------*/
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/ipc.adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var defaultPath = '/tmp/app.socket-';
* @param {function} callback The callback for the operation
*/
function listen(server, callback) {
fs.unlink(this.path, function _bindSocket() {
fs.unlink(defaultPath + server.options.port, function _bindSocket() {
server.listener = net.createServer(server._handleRequest);
server.listener.listen(defaultPath + server.options.port, callback);
});
Expand Down
18 changes: 3 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,12 @@

/* Requires ------------------------------------------------------------------*/

var Client = require('./client');
var Server = require('./server');

/* Methods -------------------------------------------------------------------*/

function listen(options) {
return new Promise(function(resolve) {
var server = new Server(options);
server.listen(function() {
resolve(server);
});
});
}
var Client = require('./Client');
var Server = require('./Server');

/* Exports -------------------------------------------------------------------*/

module.exports = {
Client: Client,
Server: Server,
listen: listen
Server: Server
};
23 changes: 23 additions & 0 deletions src/middleware/bundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function process() {
if (this.bundles.length > this.peer.options.maxBundle) {
this._bundleTimer = setTimeout(this._tick.bind(this), this.peer.options.bundleDelay);
}
else {
this._bundleTimer = null;
}

this.adapter.prototype.send.call(this, encoders[this.peer.options.encoder].encode(this.bundles.splice(0, this.peer.options.maxBundle)));
this.onComplete.dispatch();
}
// Bundling logic
// -----------------------------
// Add new calls to the bundle stack
// If stack length exceeds limit OR
// If time since last chunk sent is greater than the delay -
// AND there is an item in the stack
// Send.
// If an item is added and delay timer is not started, start it.

module.exports = {
process: process
};
26 changes: 26 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var Kalm = require('../index');

var server = new Kalm.Server({
port: 3000,
adapter: 'ipc',
encoder: 'msg-pack'
});

server.on('connection', function(client) {
console.log('client');
console.log(client);
});

server.on('ready', function() {
var client = new Kalm.Client({
port: 3000,
adapter: 'ipc',
encoder:'msg-pack',
hostname: '0.0.0.0'
});

client.channel('rare_pepes').send('kalm_pepe');
client.channel('rare_pepes').send('kalm_doge');

client.channel().send('klam klam');
});
4 changes: 3 additions & 1 deletion tests/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var Kalm = require('../../index');

describe('Starting service', function() {
it('constructor', function(done) {
Kalm.listen({
console.log('test');
console.log(Kalm);
var server = new Kalm.Server({
port: 3000,
adapter: 'ipc',
encoder: 'msg-pack'
Expand Down

0 comments on commit f10f758

Please sign in to comment.