我已经开发了一种能源模型,可以为网络中存在的每个节点加载该模型。该模型计算发射和接收能量。 现在我要做的是,如果一个节点请求所有的邻居节点,那么这些邻居节点应该通过发送其剩余能量值进行回复,以便该节点可以确定哪个邻居具有更高的能量水平。
如何实现这一点以及该请求节点将如何获取收到的所有能量值 来自各个邻居节点?
这是我的能量模型:
import org.arl.fjage.*
import org.arl.unet.*
import org.arl.unet.phy.*
import java.math.*
import Java.util.*
class EnergyModel extends UnetAgent {
int neighbor, addr
float neighbor_distance;
def ranging
def init_energy = 10
def dist
def data
def depth
def C = 1.3312e-9 // empirical constant
static def Tot_bits
def fr = 10 //carrier freq.(Khz)
def d = 0.036*Math.pow(fr,1.5) //Thorp's constant
static def source
static HashMap<Integer, Integer[]> map = new HashMap<>();
def sum=0.0,avg=0.0,count=0;
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;
depth = nodeInfo.location[2]
map.put(addr, nodeInfo.location);
}
public void processMessage(Message msg) {
if (msg instanceof DatagramFailureNtf){
System.out.println "\n\tDatagramFailureNtf occured!!\nt"+msg.toString()
println "\n\t BadFrameNtf occured !!!!\n\t"+msg.toString()
}
if (msg instanceof DatagramNtf && msg.protocol == Protocol.DATA) {
count++
neighbor = msg.from;
source = msg.from;
data = msg.getData()
int[] loc1 = map.get(source)
int[] loc2 = map.get(msg.getTo())
def x = loc1[0] - loc2[0]
def y = loc1[1] - loc2[1]
def distance = Math.sqrt((x)*(x) +(y)*(y));
def bits=32
Tot_bits = bits*data.size()
System.out.println "\n\tNumber of bits sent :"+Tot_bits
dist = distance/1000.0 // converting the distance in Km.
BigDecimal Tx_EG = new BigDecimal("1"); // Or BigInteger.ONE
Tx_EG = Tx_EG.multiply(BigDecimal.valueOf(Tot_bits*50e-9+ Tot_bits*
(0.001)*dist*(depth*-0.001)*C*Math.pow(Math.E,d*dist)));
init_energy = init_energy - Tx_EG ;
sum = sum + Tx_EG
avg = sum/count
String value = String.valueOf(Tx_EG.doubleValue());
System.out.println '\n\tTransmission Energy : '+value+" Joules";
System.out.println '\tRemaining Energy : '+(init_energy)
File file = new File("I:\\out.txt")
def text = file.getText()
System.out.println "ENERGY: -t "+text+" -i "+source+" -d
"+Tot_bits+" -e "+init_energy+" T"
println "ENERGY: -t "+text+" -i "+source+" -d "+Tot_bits+" -e
"+init_energy+" T"
}
if (msg instanceof RxFrameNtf && msg.protocol == Protocol.DATA){
data = msg.getData() // getting data
System.out.println "\tData is :"+data
def bits=32
Tot_bits = bits*data.size() //caculating total number of bits
System.out.println "\tNumber of bits received :"+Tot_bits
BigDecimal Rx_EG = new BigDecimal("1"); // Or BigInteger.ONE
Rx_EG = Rx_EG.multiply(BigDecimal.valueOf(Tot_bits*50e-9));
init_energy = init_energy - Rx_EG ;
String value = String.valueOf(Rx_EG.doubleValue());
System.out.println '\n\tReception Energy : '+value+" Joules";
System.out.println '\tRemaining Energy : '+(init_energy)
System.out.println '\tTime : '+msg.getRxTime()
System.out.println '\tNode ID : '+msg.getTo()
System.out.println "ENERGY: -t "+msg.getRxTime()+" -i
"+msg.getTo()+" -d "+Tot_bits+" -e "+init_energy+" R"
println "ENERGY: -t "+msg.getRxTime()+" -i "+msg.getTo()+" -d
"+Tot_bits+" -e "+init_energy+" R"
}
if (msg instanceof BadFrameNtf){
System.out.println "\n\tBadFrameNtf occured !!!!\n\t"+msg.toString()
println "\n\t BadFrameNtf occured !!!!\n\t"+msg.toString()
}
if (msg instanceof CollisionNtf){
System.out.println "\n\tCollision occured !!!!\n\t"+msg.toString()
println "\n\tCollision occured !!!!\n\t"+msg.toString()
}
}
void setup() {
}
}
答案 0 :(得分:1)
您将需要定义特定于应用程序的PDU来请求此信息并提供此信息,并实现自己的协议以在收到请求PDU时以适当的响应PDU进行响应。在开发人员指南中查看如何从ping示例(模拟器中的samples/ping
文件夹)编写自己的特定于应用程序的协议。
一种替代方法是将能量公开为PHY的代理参数,并使用remote access服务请求使用RemoteParamReq
来请求它。