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 {

    setAction: function(id, action, options){
      shared.set(id, action);
    },

    executeAction: function(id, args, options){
      var options = options || {};

      context = options['context'] || this;
      //var meth = shared.get(id);
      //console.log("exec'ing", meth);
      //Shared[id].apply(context, args);

      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!

@@ -1023,90 +1023,26 @@
 n %7B%0A
-%0A
     s
-etAction: function(id, action, options)%7B%0A      shared.set(id, action);%0A    %7D
+hared: shared
 ,%0A%0A 
@@ -1127,169 +1127,8 @@
 %7B%7D;%0A
-%0A      context = options%5B'context'%5D %7C%7C this;%0A      //var meth = shared.get(id);%0A      //console.log(%22exec'ing%22, meth);%0A      //Shared%5Bid%5D.apply(context, args);%0A%0A
     

allow for access to shared object in Async