0

我在 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);

但是我怎样才能将此信息发送给其他邻居车辆。我想发送和累积每辆车的信息,但只发送初始信息,即我不发送所有累积的信息。

4

1 回答 1

1

您可以扩展WSM以包含要交换的信息。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;

不幸的是,由于我不知道您的代码的详细信息,因此我无法为您提供即读即用的解决方案,但这应该可以让您大致了解应该做什么。

于 2016-03-04T12:04:38.530 回答