Search code examples
groovylocationunetstack

How to get Location of a node using it's address in the script?


I have written a transmit cmd that takes address and data as input and sends data to that node. I tried to get the location of the node using it's address but it is showing null value, but data is getting successfully transferred.
Why is this happening and what's the solution?

Example:

transmit 2,[1,2,3]   //transmit data [1,2,3] to node-2

transmit = { addr, data ->
    println "TRANSMIT $addr, $data"
    println phy << new DatagramReq(to: addr, protocol: Protocol.MAC, data: data)
    NodeInfo n = new NodeInfo(addr)
    println 'location = '+n.getLocation()
    println 'Address :'+addr+'\nData :'+data

    def txNtf = receive(TxFrameNtf, 1000)

    println txNtf  
}

Model.groovy :

class Model extends UnetAgent {

int neighbor, addr;
float neighbor_distance;
def ranging

def dist;
def data


public void startup() {
AgentID phy = agentForService(Services.PHYSICAL);
subscribe(topic(phy));

ranging = agentForService Services.RANGING;
subscribe topic(ranging);

def nodeInfo = agentForService Services.NODE_INFO;
addr = nodeInfo.address;

}

void processMessage(Message msg) {
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{     
    neighbor = msg.from;
    println " BEACON RECEIVED FROM:" +neighbor
    data = msg.getData()

    def bits=32

    System.out.println "number of bits sent :"+bits*data.size()

    ranging << new RangeReq(to: neighbor);
  }
  else if (msg instanceof RangeNtf )
  {   
    float neighbor_distance = msg.getRange();
    println( "\n Distance between node "+addr + " and neighbor " +neighbor+ " is " + neighbor_distance+"m.\n")


    }
  }

  void setup() {


       }

   }

Solution

  • Your code:

    NodeInfo n = new NodeInfo(addr)
    println 'location = '+n.getLocation()
    

    creates a node info object and tries to get a location from it. By default, such an object does not have any location, and hence you get a null value. The object you create does not refer to the node info agent running on the node.

    What you perhaps intended was to get the information from the node info agent, which you need to look up and not try to create locally. Example code snippet:

    def n = agentForService org.arl.unet.Services.NODE_INFO
    println 'location = '+n.location