/**
 * settings
 */

var webPort = 8090;

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes');
var io = require('socket.io');
var redis = require('redis');

var app = module.exports = express.createServer(),
    io = io.listen(app);

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(webPort);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

// socket.io
io.sockets.on('connection', function (socket) {
	console.log('Connection: ' + socket.id);

	var rc = redis.createClient();
	var ids = new Array();
	var limit = 3;

        socket.on('sub', function(id) {
		// TODO: allow multiple subscriptions when id=[123,456]
		if (ids.indexOf(id) < 0) {
		    if (ids.push(id) > limit)
			rc.unsubscribe(ids.pop());
		    rc.subscribe(id);

		}
		console.log('sub: ' + id + ' (' + ids.length + ' subscriptions: ' + ids +')');

	    });

	socket.on('unsub', function(id) {
		var i = ids.indexOf(id);
		if (i > -1) {
		    var rest = ids.slice(i + 1 || ids.length);
		    ids.length = i < 0 ? ids.length + i : i;
		    ids.push.apply(ids, rest);
		    rc.unsubscribe(id);

		}
		console.log('unsub: ' + id + ' (' + ids.length + ' subscriptions: ' + ids + ')');

	    });

        socket.on('subs', function(id) {
		socket.emit('info', ids);
		console.log('subs: ' + ids +'');

	    });

	socket.on('disconnect', function () {
		console.log('disconnect');
		rc.end();

	    });

	rc.on('message', function(id, data) {
		var o = new Object();
		o[id] = JSON.parse(data);
		console.log('rc.on(data): ' + JSON.stringify(o));
		socket.volatile.emit('data', o);
	    });

	rc.on( 'error', function(err) {
		console.log('Redis Error: ' + err );

	    });

    });

@@ -1080,17 +1080,18 @@
 limit = 
-3
+10
 ;%0A%0A     
@@ -1134,63 +1134,123 @@
 %09// 
-TODO: allow multiple subscriptions when id=%5B123,456%5D%0A%09%09
+enforce alphanumeric ids%0A%09%09var rx = new RegExp(/%5CW/);%0A%09%09var a = id.match(rx);%0A%09%09if (a == null && id != '') %7B%0A%09%09    
 if (
@@ -1270,28 +1270,25 @@
 d) %3C 0) %7B%0A%09%09
-    
+%09
 if (ids.push
@@ -1304,16 +1304,20 @@
 mit)%0A%09%09%09
+    
 rc.unsub
@@ -1337,20 +1337,17 @@
 p());%0A%09%09
-    
+%09
 rc.subsc
@@ -1348,39 +1348,46 @@
 .subscribe(id);%0A
-%0A
 %09%09
-%7D%0A%09%09
+    %7D%0A%09%09    
 console.log('sub
@@ -1444,24 +1444,97 @@
 + ids +')');
+%0A%09%09%7D%0A%09%09else %7B%0A%09%09    console.log('ERROR: illegal subscription ' + id);%0A%09%09%7D
 %0A%0A%09    %7D);%0A%0A

enforce alphanumeric ids and increase subscription limit to 10 not supporting multiple subscriptions in one shot because a single subscription isn't in JSON anyway - should be interpreted as a literal. we only accept [a-zA-Z0-9] (in other words, \w)