diff --git a/README.md b/README.md index a360555..3fd84de 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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!'); diff --git a/src/client.js b/src/Client.js similarity index 86% rename from src/client.js rename to src/Client.js index fb48c6b..61a6eab 100644 --- a/src/client.js +++ b/src/Client.js @@ -8,6 +8,9 @@ /* Requires ------------------------------------------------------------------*/ +var util = require('util'); +var EventEmitter = require('events').EventEmitter; + var debug = require('debug')('kalm'); var adapters = require('./adapters'); @@ -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 = { @@ -49,9 +54,6 @@ function Client(options) { // Data packets - transient state - by channel this._packets = {}; - - // Init - this._updateSocket(); } /** @@ -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] = []; } @@ -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; \ No newline at end of file diff --git a/src/server.js b/src/Server.js similarity index 57% rename from src/server.js rename to src/Server.js index 417a2db..93422ed 100644 --- a/src/server.js +++ b/src/Server.js @@ -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'); @@ -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'; @@ -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(); } /** @@ -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; \ No newline at end of file diff --git a/src/adapters/index.js b/src/adapters/index.js index 3bc4a11..6ca16d7 100644 --- a/src/adapters/index.js +++ b/src/adapters/index.js @@ -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'); @@ -19,8 +18,7 @@ var debug = require('debug')('kalm'); var list = { ipc: ipc, tcp: tcp, - udp: udp, - ws: ws + udp: udp }; /* Methods -------------------------------------------------------------------*/ diff --git a/src/adapters/ipc.adapter.js b/src/adapters/ipc.adapter.js index b7f7427..c13b317 100644 --- a/src/adapters/ipc.adapter.js +++ b/src/adapters/ipc.adapter.js @@ -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); }); diff --git a/src/index.js b/src/index.js index 95e4806..36ac000 100644 --- a/src/index.js +++ b/src/index.js @@ -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 }; \ No newline at end of file diff --git a/src/middleware/bundler.js b/src/middleware/bundler.js index e69de29..3fa2a71 100644 --- a/src/middleware/bundler.js +++ b/src/middleware/bundler.js @@ -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 +}; \ No newline at end of file diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..e0f1434 --- /dev/null +++ b/tests/test.js @@ -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'); +}); \ No newline at end of file diff --git a/tests/unit/index.js b/tests/unit/index.js index 7fa7589..6c0caef 100644 --- a/tests/unit/index.js +++ b/tests/unit/index.js @@ -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'