我编写了一个传输 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() {
}
}