jam/demo/sejam2/simple/node.js

109 lines
2.6 KiB
JavaScript

// Sample node agent
/*
level 3 agents have access to extended simu object:
{changeVisual:function(),
chat:{..},
clear:function(),
createNode:function(),
createOn:function(),
deleteNode:function(),
event:{..},
get:function(),
getNode:function(),
getVisual:function(),
getStats:function(),
getSteps:function(),
load:function(),
log:function(),
inspect:function(),
model:function(),
message:function(),
move:function(nodeid,dx,dy,dz?),
moveTo:function(nodeid,x,y,z?),
network:{..},
ofJSON:function(),
options:{..},
parameter:function(),
position:function(),
print:function(),
save:function(),
set:function(),
start:function(),
stat:function(),
stop:function(),
toJSON:function(),
time:function(),
utime:function(),
csv:{..},
db:{..},
sql:{..},
units:{..},
phy:{..},
simuPhy:{..}
}
nodeid: node[<nodename>] !!!
*/
function (options) {
this.location={x:options.x,y:options.y,z:options.z};
this.mynode=none;
this.sensor=0;
this.child=none;
this.parameter=options.parameter;
this.act = {
init: function () {
this.mynode=myNode();
log('Starting at '+this.location.x+','+this.location.y+' on node '+this.mynode);
log('My id is '+me())
log('My node id is '+myNode())
log('My level is '+myLevel())
log(negotiate('?'))
log('My secret is '+this.parameter.mysecret)
log('My moving direction is'+this.parameter.dir)
},
percept: function () {
// get all connected nodes
this.sensor=link(DIR.PATH('*'));
log(this.sensor)
// here we have pixel units!!!
this.location=myPosition()
log('My position is '+simu.inspect(this.location))
// nodeid: node[<nodename>] !!!
simu.moveTo('node['+myNode()+']',this.location.x+10,this.location.y+10)
},
service : function () {
if (random(1,1000)>950) {
log('deleteNode '+myNode())
simu.deleteNode(myNode()) // nodeid only!
}
if (random(1,1000)>950 && this.sensor) {
var node = random(this.sensor)
log('sendto '+node)
// send signal to all agents on specified node which are listening to the signal
sendto(DIR.NODE(node),'HEYOU',random(1,100))
}
},
wait: function () {
sleep(random(1,200)); // don't call sleep(0), blocks until wakeup!
}
}
this.trans = {
init: percept,
percept: service,
service: wait,
wait: percept
}
this.on = {
HEYOU: function (arg,fromAgent,fromNode) {
log('Got HEYOU ('+arg+') from agent '+fromAgent+' on node '+fromNode)
}
}
this.next='init';
}