From 77aa8bbe936cec7d784d7548ea7f7f40c754ec42 Mon Sep 17 00:00:00 2001 From: Mattijs Hoitink Date: Tue, 3 Apr 2012 13:20:07 +0200 Subject: [PATCH] Initial commit --- README.md | 58 ++++++++++++++++++++ index.js | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++ 3 files changed, 233 insertions(+) create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..06a6ea8 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# Redubber + +Redubber can be used to control multiple Redis PubSub servers through +[redub](https://github.com/two-screen/redub) and +[redis-pubsub](https://github.com/two-screen/redis-pubsub). + +## Usage + +Redubber can be used from the terminal. It requires a configuration file to tell it what +servers and channels to listen to. This will file is simple JSON file containing a list of +servers and a list of channels: + +```json +{ + "channels": [ "message" ], + "servers": [ "localhost:6379" ] +} +``` + +## Monitor + +You can monitor messages received on multiple Redis servers from the CLI + +```bash +$ redubber monitor +``` + +## Send + +Messages can be send from the commandline as arguments to the send command: + +```bash +$ redubber send "a message" +``` + +### Options + +- `-c|--channel CHANNEL`: The channel to send the message to +- `-j|--json`: Treat input as json and parse it +- `-a`: Read input from STDIN + +Messages can also be read from `STDIN` by piping it into `redubber`: + +```bash +$ cat message.json | redubber send -j -a +``` + +# License + +(The MIT License) + +Copyright (c) 2012, Two Screen + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/index.js b/index.js new file mode 100644 index 0000000..2193812 --- /dev/null +++ b/index.js @@ -0,0 +1,150 @@ +var _ = require('underscore'), + pubsub = require('redis-pubsub'), + Redub = require('redub'); + +// DEFAULTS +var DEFAULT_PORT = 6379; +var DEFAULT_HOST = 'localhost'; + +/** + * Parse a host:port spec into a separate values. Defaults will always be supplied + * @param {String} address + * @return Object + */ +function parseAddressSpec(address) { + var addressSplit = address.split(':', 2); + + return { + port: addressSplit[1] || DEFAULT_PORT, + host: addressSplit[0] || DEFAULT_HOST + }; +} + +/** + * Spy on one or more redis servers + * @param {Array} servers + */ +function Redubber(servers) { + this.connections = []; // Server connections + this.channels = []; // Channel names across all connections + + // Redub for managing multiple servers + this.redub = new Redub(); + + // Parse servers passed to this constructor + (servers || []).map(Redubber.prototype.addServer, this); +} + +Redubber.prototype.listen = function(listener) { + var redub = this.redub; + this.channels.forEach(function(channel) { + redub.on('message', function(message) { + listener(channel, message); + }); + }); +}; + +/** + * Add a server to the Redis Spy + * @param {Object|String} config + * @return Redubber + */ +Redubber.prototype.addServer = function(config) { + var server = this.parseServer(config); + if (false === server) { + return this; + } + + var channelNames = [], redub = this.redub; + server.channels.forEach(function(config) { + // Create a new PubSub channel + var channel = pubsub.createChannel(server.port, server.host, config.name); + //channel.raw = config.raw; + + channel.on('ready', function() { + console.log('Connection to ' + server.host + ':' + server.port + ' is ready'); + }); + + // Add it to Redub + redub.add(channel); + + // Register the channel name + channelNames.push(config.name); + }); + + // Filter channel names + this.channels = _(this.channels).union(channelNames); + + return this; +}; + +/** + * Parse a server configuration into a server object + * @param {Object} config + * @return Object + */ +Redubber.prototype.parseServer = function(config) { + var server = {}, address, channels; + + // String type server config + if (typeof(config) === 'string') { + // Split channels from address + var channelSplit = config.split('#', 2); + + // Parse the address + address = parseAddressSpec(channelSplit[0]); + server.port = address.port || DEFAULT_PORT; + server.host = address.host || DEFAULT_HOST; + + // Split channels + channels = (channelSplit[1] || '').split(',').map(function(channel) { return channel.trim(); }); + } + // Server config object + else { + if (config.host) { + server.host = config.host; + server.port = config.port || DEFAULT_PORT; + } + else if (config.address) { + address = parseAddressSpec(config.address); + server.port = address.port || DEFAULT_PORT; + server.host = address.host || DEFAULT_HOST; + } + else { + return false; + } + + //address = server.address; + channels = config.channels || []; + } + + // Parse all channels + server.channels = []; + channels.forEach(function(channel) { + + // Channel as object + if (typeof(channel) === 'object') { + if (!channel.name) return; + + // Push the channel on the stack + server.channels.push({ "name": channel.name, "raw": channel.raw || false }); + } + else if (typeof(channel) === 'string') { + // Check for raw option + var raw = false; + if ((channel.length - 4) === channel.indexOf(':raw')) { + raw = true; + channel = channel.substring(0, channel.length - 4); + } + + // Push the channel on the stack + server.channels.push({ "name": channel, "raw": raw }); + } + }); + + return server; +}; + + +// our awesome export products +exports = module.exports = Redubber; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8009ac6 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "redubber", + "description": "Control multiple Redis PubSub servers through redub and redis-pubsub", + "version": "0.0.1", + "homepage": "https://github.com/two-screen/redubber", + "author": "Mattijs Hoitink (http://monkeyandmachine.com)", + "keywords": ["redis", "pubsub", "redub"], + "repository": { + "type": "git", + "url": "git://github.com/two-screen/redubber.git" + }, + "license": "MIT", + "dependencies": { + "redis-pubsub": "*", + "redub": "*", + "underscore": ">=1.3" + }, + "main": "./index.js", + "bin": { + "redubber": "./bin/redubber" + }, + "engines": { + "node": "0.6" + } +}