在omnet

时间:2016-03-03 02:06:32

标签: c++ dictionary omnet++ veins

我在omnet ++ 4.6中使用Veins 4a2。我想将函数中包含的信息作为消息发送到邻居节点。怎么能实现这个? .cc中的函数如下所示:

void TraCITestApp::append2List(short carId, short firstEmptyArrayIndex,     simtime_t messageTime, double theta, std::string vType) {
listedVehicles[firstEmptyArrayIndex].id = carId; // ~~here the Id is changed name to car ID.
listedVehicles[firstEmptyArrayIndex].lastSeenAt = messageTime;
listedVehicles[firstEmptyArrayIndex].vType = vType;
listedVehicles[firstEmptyArrayIndex].theta = theta;
EV << "Appending car with id " << carId <<" type "<< vType << " to the list of known vehicle." << endl;



/* @brief Increase related counting variable
* The total number always increased for each vehicle
*/
currentNumberofTotalDetectedVehicles++;
}

void TraCITestApp::showInfo_D(short counter){
EV << "Listed Table for Truthtelling:" << endl;
   for (int i = 0; i < counter; i++)
 { EV << "Serial [" << i << "] " <<"ID="<< listedVehicles[i].id <<  "\tTruthtelling prob.\t" << listedVehicles[i].theta <<endl;

std::ofstream tracefile;
   tracefile.open("traceFiledata.txt", std::ios_base::app);
   tracefile << "============================================";
   tracefile << "MyID=" << getMyID() << ";" <<"Serial [" << i << "] " <<"ID="<< listedVehicles[i].id <<  ";" << "Time=" << simTime() << ";" << "TTP=" << listedVehicles[i].theta << getMetaData() << std::endl;
   tracefile.close();

}

EV << "Total number of detected vehicle\t: " << currentNumberofTotalDetectedVehicles << endl;

}

我可以将void TraCITestApp::onData(WaveShortMessage* wsm)中的方法称为showInfo_D(currentNumberofVehicles);

但是我如何将这些信息发送给其他邻居的车辆。我想发送和累积每辆车的信息,但只有初始信息,即我不发送所有累积的信息。

1 个答案:

答案 0 :(得分:1)

您可以扩展WSM以包含您要交换的信息。 Here是一个扩展WSM并为您自己的目的创建消息的示例。

只需在消息定义中声明将保存数据的变量

cplusplus {{
#include "veins/modules/messages/WaveShortMessage_m.h"
}}

class WaveShortMessage;

message MyAppsPacket extends WaveShortMessage {
    string sourceAddress;           
    string destinationAddress;      
    simtime_t sendingTime;
    string vehicleID;
    whateverType theta;
}

然后在生成MyAppsPacket时,你可以这样做:

MyAppsPacket->setTheta(theta);
MyAppsPacket->setSendingTime(simeTime());
MyAppsPacket->setVehicleID(listedVehicles[i].id;

不幸的是,由于我不知道你的代码的细节,我不能给你一个读取使用的解决方案,但这应该让你大致了解你应该做什么。