var cluster = require("cluster");
var os = require('os');
var path = require("path");

var Message = require(path.join(__dirname, 'message'));
var Worker = require(path.join(__dirname, 'worker'));
var Shared = require(path.join(__dirname, 'shared'));

var numCPUs = require("os").cpus().length;


var Async = function(){
  var workers = [];
  var currentWorker = -1;
  var swapFile = path.join(os.tmpdir(), ('__npc-' + process.pid + '.swap'));

  var shared = new Shared({
    'swap': swapFile,
  });

  cluster.setupMaster({
    exec: path.join(__dirname, 'worker.js')
  });

  for (var cpuNum = 0; cpuNum < numCPUs; cpuNum += 1){

    var worker = new Worker({
      'shared': shared,
      'onMessage': function(msg){
        //console.log('master #' + process.pid + ' rec\'d message from slave #' + this.process.pid + ':', msg);


        //var msg = new Message();
        //msg.setPayload('ping', Shared.ping.master_message);
        //this.process.send(msg);
      }
    });
    workers.push(worker);

  };

  return {
    shared: shared,

    executeAction: function(id, args, options){
      var options = options || {};
      var workerIndex = ++currentWorker % workers.length;
      var msg = new Message();
      msg.setPayload('action', {
        'id': id,
        'arguments': args || [],
        'options': options || {},
      });
      workers[workerIndex].send(msg);
    }

  };

};

module.exports = Async(); // it's a module!

@@ -1374,24 +1374,285 @@
 .send(msg);%0A
+    %7D,%0A%0A    executeFunction: function(func, args, options)%7B%0A      actionName = process.pid.toString() +%0A        '-' + Math.round(Math.random() * 10e15).toString(36);%0A%0A      this.shared.set(actionName, func);%0A      this.executeAction(actionName, args, options);%0A
     %7D%0A%0A  %7D;%0A

allow execution of actions on-the-fly