var io = require('socket.io')
  , ss = require('socket.io-stream')
  , path = require('path')
  , fs = require('fs')
  , brain = require('./brain.js');



var start = function (config) {
  console.log(config.port);
  io.listen(config.port);


  io.sockets.on('connection', function (socket) {

    ss(socket).on('addController', function(stream, data) {
      var filename = path.basename(data.name);
      
      stream.on('end', function (){
        brain.addController(config.folderPath + '/' + filename);
      });

      stream.pipe(fs.createWriteStream(config.folderPath + '/' + filename), function (err, result) {
        if (err) {console.log(err);}
      });

    });


    socket.on('execute', function(options) {
      brain.execute(options.controllerID, options.command);
    });


    socket.on('disconnect', function(){
      console.log('Socket disconnect');
    });
  });

};


module.exports = {
  start: start
};







@@ -1,37 +1,7 @@
 var
- io = require('socket.io')%0A  ,
  ss 
@@ -156,38 +156,37 @@
 %7B%0A  
-console.log(config.port);%0A  io
+var io = require('socket.io')
 .lis
@@ -204,17 +204,16 @@
 port);%0A%0A
-%0A
   io.soc
@@ -895,11 +895,101 @@
 t%0A%7D;%0A%0A%0A%0A
+var config = %7B%0A  port: 8080,%0A  folderPath: __dirname + 'nmrcontrollers'%0A%7D;%0A%0Astart(config);
 %0A%0A%0A

refactor all the way