// Fork, migration, remote signalling
// Requires test-forkB, too.

Aios.Sig.options.debug.send=true;

port(DIR.IP('http://localhost:6666'))
start();

function ag (options) {
  this.child=null;
  this.master=null;
  
  this.act = {
    init : function () {
      this.master=true;
      log('Starting.');
      sleep(3000);
    },
    
    replicate : function () {
      log('Replicating...');
      this.child=fork();
    },
    
    migrate : function () {
      var nodes = link(DIR.IP('%'));
      log('Migrating to '+nodes[0]);
      if (nodes.length) moveto(DIR.NODE(nodes[0]));
    },
    
    killing : function () {
      log('Killing child...');
      kill(this.child);
    },
    
    wait : function () {
      log('Waiting ...');
      if (this.child) sleep(3000); else sleep(1000);
    },
    
    end : function () {
      log('Terminating.');
      kill();
    }
  }
  
  this.trans = {
    init      : replicate,
    replicate : function () { return this.child?wait:migrate },
    migrate   : wait,
    killing   : end,
    wait      : function () { return this.child?killing:wait },
  }
  
  this.next=init;

}

compile(ag);

create('ag',{ },2);
