我正在尝试使用广播请求从相邻节点获取坐标并将它们存储到列表中。我只能得到一个响应,我认为这是因为节点同时响应时响应之间的冲突而发生的。我曾尝试在相邻节点的代理上使用带有随机退避的退避行为,但它没有解决问题。如何同步机制以避免冲突?我为每个盲节点(将发出广播请求)和相邻节点编写了一个代理。
以下是代码片段。
盲节点上的代理:
import org.arl.fjage.*
import org.arl.fjage.Message
import org.arl.fjage.RealTimePlatform
import org.arl.unet.phy.*
import org.arl.unet.mac.*
import org.arl.unet.*
import org.arl.unet.PDU
import org.arl.unet.net.Router
import org.arl.unet.nodeinfo.NodeInfo
import org.arl.unet.localization.*
import org.arl.unet.localization.RangeNtf.*
import org.arl.unet.localization.Ranging.*
import org.arl.unet.localization.RangeReq
import org.arl.unet.net.RouteDiscoveryReq
class node_agent extends UnetAgent {
float neighbor_addr;
float distance;
def xlist = [];
def ylist = [];
def zlist = [];
def dlist = [];
private final static PDU format = PDU.withFormat
{
uint16('source')
}
void startup()
{
def phy = agentForService Services.PHYSICAL;
subscribe topic(phy);
println 'Starting discovery...'
phy << new DatagramReq(to: 0, protocol:Protocol.MAC);
}
void processMessage(Message msg)
{
def ranging = agentForService Services.RANGING;
subscribe topic(ranging);
if(msg instanceof RxFrameNtf && msg.protocol==Protocol.MAC)
{
def rx = format.decode(msg.data);
neighbor_addr=rx.source;
println "Found one neighbour with address "+neighbor_addr;
ranging<< new RangeReq(to: neighbor_addr,requestLocation: true);
}
else if (msg instanceof RangeNtf )
{
distance = msg.getRange();
def locat=new double[3];
locat = msg.getPeerLocation();
double x,y,z;
x=locat[0];
y=locat[1];
z=locat[2];
xlist.add(x);
ylist.add(y);
zlist.add(z);
dlist.add(distance);
println " The coordinates of "+msg.to + " are " +locat+ " and the distance is "+distance;
println xlist;
println ylist;
println zlist;
println dlist;
}
}
}
相邻节点上的代理:
import org.arl.fjage.*
import org.arl.fjage.Message
import org.arl.fjage.RealTimePlatform
import org.arl.unet.phy.*
import org.arl.unet.mac.*
import org.arl.unet.*
import org.arl.unet.PDU
import org.arl.unet.net.Router
import org.arl.unet.nodeinfo.NodeInfo
import org.arl.unet.localization.*
import org.arl.unet.localization.RangeNtf.*
import org.arl.unet.localization.Ranging.*
import org.arl.unet.localization.RangeReq
import org.arl.unet.net.RouteDiscoveryReq
class anchor_agent extends UnetAgent {
def addr;
private final static PDU format = PDU.withFormat
{
uint16('source')
}
void startup()
{
def phy = agentForService Services.PHYSICAL;
subscribe topic(phy);
}
void processMessage(Message msg)
{
def phy = agentForService Services.PHYSICAL;
subscribe topic(phy);
def nodeInfo = agentForService Services.NODE_INFO;
addr = nodeInfo.address;
def datapacket = format.encode(source: addr);
if(msg instanceof DatagramNtf && msg.protocol==Protocol.MAC)
{
def n=rndint(3);
add new BackoffBehavior(n*1000,{
phy << new TxFrameReq(to: msg.from,
protocol: Protocol.MAC,
data: datapacket)})
backoff(n*1000)})
}
}
}
模拟脚本:
import org.arl.fjage.*
println '''
3-node network
--------------
'''
platform = RealTimePlatform
simulate {
node 'B', address: 101, location: [ 0.km, 0.km, -15.m], web:8081,api: 1101, shell: true, stack: "$home/etc/setup2"
node '1', address: 102, location: [ 0.km, 1.km, -15.m], web:8082,api: 1102, shell: 5101, stack: "$home/etc/setup3"
node '2', address: 103, location: [-900.m, 0.km, -15.m], web:8083,api: 1103, shell: 5102, stack: "$home/etc/setup3"
node '3', address: 104, location: [ 600.m, 0.km, -15.m], web:8084,api: 1104, shell: 5103, stack: "$home/etc/setup3"
}