2

我编写了一个传输 cmd,它将地址和数据作为输入并将数据发送到该节点。我试图使用它的地址获取节点的位置,但它显示空值,但数据正在成功传输。
为什么会发生这种情况,解决方案是什么?

例子:

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  
}

模型.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() {


       }

   }
4

1 回答 1

1

你的代码:

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

创建一个节点信息对象并尝试从中获取一个location。默认情况下,这样的对象没有任何location,因此您会得到一个null值。您创建的对象不引用节点上运行的节点信息代理。

您可能打算从节点信息代理获取信息,您需要查找而不是尝试在本地创建信息。示例代码片段:

def n = agentForService org.arl.unet.Services.NODE_INFO
println 'location = '+n.location
于 2019-07-20T06:50:52.617 回答