I have been trying to implement neighbour discovery in a unetagent. This is the code that I have come up with so far:
@Override
protected void setup() {
this.nodeList = new ArrayList<>();
this.currentNodeIndex = 0;
this.register(Services.MAC);
this.register(Services.DATAGRAM);
this.register(Services.ROUTE_MAINTENANCE);
this.phy = this.agentForService(Services.PHYSICAL);
}
@Override
protected void startup() {
if (this.phy == null) {
this.phy = this.agentForService(Services.PHYSICAL);
}
if (this.phy == null) {
this.log.warning("No PHY found, carrier sensing disabled!");
}
this.subscribe(this.topic(this.phy, "snoop"));
// Discover other nodes in the network
discoverNodes();
}
private void discoverNodes() {
int attempts = 2; // try only a single attempt at discovery
int phantom = 132; // non-existent node address
int timeout = 10000; // 10 second timeout
AgentID rdp = agentForService(Services.ROUTE_MAINTENANCE);
this.subscribe(this.topic(rdp));
AgentID datagram = agentForService(org.arl.unet.Services.DATAGRAM);
this.subscribe(this.topic(datagram));
RouteDiscoveryReq l = new org.arl.unet.net.RouteDiscoveryReq(phantom);
l.setCount(attempts);
rdp.send(l);
Message ntf;
while ((ntf = receive(org.arl.unet.net.RouteDiscoveryNtf.class, timeout)) != null) {
this.log.warning(" Discovered neighbor: " + ((RouteDiscoveryNtf) ntf).getNextHop());
nodeList.add(((RouteDiscoveryNtf) ntf).getNextHop());
}
nodeList = nodeList.stream().distinct().collect(Collectors.toList());
this.log.warning("Neighbors are " + nodeList);
NUM_NODES = nodeList.size();
}
When I try to run the code on my agents it gives me an error that the "mac reservation request was denied: null"
I tried registering all the relevant services to the unetagent but am not able to figure out what the issue seems to be.
When I run the code from a separate .groovy script file on the shell then it works perfectly fine showing the neighbours of the node.
Is there something wrong with my code or am I missing something? Also would it be easier to implement this using a different technique?