I am trying to implement a routing algorithm in which node A first sends RouteDiscoveryReq to a non-existent node to find its neighbours. Its neighbours, a1 and a2, on receiving a RouteDiscoveryNtf should reply with a random number to node A. I have made an agent startnode.groovy which is added on nodes a1 and a2 but it is giving me an error. These are the scripts that I have wrote:
- multiHop.groovy: A simulation script in which I have deployed 6 nodes. Nodes a1 and a2 are the neighbouring nodes of node A and should run the startnode agent on them. I have only attached code snippets of node A, a1 and a2.
- startnode.groovy: The agent which should reply with a random number to A, after waiting for a random amount of time(to avoid collision).
multiHop.groovy:
def n2 = node 'A', address: 2, location: [ 0.km, 2.km, 0.m], web: 8087, stack: "$home/etc/setup"
n2.startup={
def rdp = agentForService Services.ROUTE_MAINTENANCE
subscribe topic(rdp);
def datagram = agentForService(org.arl.unet.Services.DATAGRAM)
subscribe topic(datagram);
n = []
rdp << new RouteDiscoveryReq(to: 100, count: 1)
while (ntf = receive(RouteDiscoveryNtf, 10000)) {
n << ntf.nextHop // add neighbor to list
}
n = n.unique() // remove duplicates
println("Neighbors are ${n}")
}
def a1 = node 'a1', address: 3, location: [ 0.km, 3.km, -1850.m], web: 8088, stack: "$home/etc/setup"
a1.startup={
container.add 'sn', new startnode();
}
def a2 = node 'a2', address: 4, location: [ 1.5.km, 3.km, -1850.m], web: 8089, stack: "$home/etc/setup"
a2.startup={
container.add 'sn', new startnode();
}
startnode.groovy:
import org.arl.fjage.*
import org.arl.fjage.param.Parameter
// import static org.arl.unet.utils.MathUtils.*
import org.arl.unet.*
class startnode extends UnetAgent {
int delay = Math.abs(new Random().nextInt()) % 1000 + 1
int rand_num = Math.abs(new Random().nextInt()) % 10 + 1
void startup() {
subscribeForServices(Services.DATAGRAM)
subscribeForServices(Services.ROUTE_MAINTENANCE)
}
void processMessage(Message msg) {
if (msg instanceof RouteDiscoveryNtf) {
add new WakerBehavior(delay, {
send new DatagramReq(
recipient: msg.sender,
to: msg.from,
data: rand_num
)
})
}
}
}