// Simple jamsh test demonstrating lifetime resource control and negotiation
// with capabilities

// Ports for node negotiation
var port = Port.unique()
var rand = Port.unique();

function hello(cap) {
  this.cap=cap;
  this.act = {
    init: function () { 
      log('START'); 
      // negotiate more lifetime (default is 2000)
      log(negotiate('LIFE',10000,this.cap))
      // if negotiation was successful, we can sleep in peace, else..
      sleep(5000) 
    },
    stop: function () { log('STOP'); kill() }
  }
  this.trans = {
    init:"stop"
  }
  this.on = {
    error: function (e,arg) { log('Error: '+e) },
  }
  this.next="init"
}
var security = {}
security[Port.toString(port)]=Port.toString(rand);

// default platform settings
config({
  IDLETIME:1000,
  LIFETIME:2000,
  // remember the private ports for node negotiation
  security :security,
  verbose:3,
})

compile(hello,{verbose:1})
start()

// Capability for lifetime negotation of the agent
var cap  = Capability(port,Private.encode(0,Rights.NEG_LIFE,rand))
print(Capability.toString(cap))
var id1=create(hello,cap)
var id2=create(hello)
later(500,function () {
  print(info('agent',id1).resources)
  print(info('agent',id2).resources)
})