简体   繁体   English

在omnet中将功能作为消息发送

[英]send function as a message in omnet

I am using Veins 4a2 in omnet++4.6. 我在omnet ++ 4.6中使用Veins 4a2。 I would like to send the information contained in a function as a message to the neighbor nodes. 我想将函数中包含的信息作为消息发送给邻居节点。 How can achieve this? 如何实现呢? the function in .cc looks like this: .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;

}

I can call the method in void TraCITestApp::onData(WaveShortMessage* wsm) as showInfo_D(currentNumberofVehicles); 我可以在void TraCITestApp::onData(WaveShortMessage* wsm) showInfo_D(currentNumberofVehicles);方法调用为showInfo_D(currentNumberofVehicles);

But how can i send this information to other neighbor vehicles. 但是我如何将这些信息发送给其他邻居车辆。 I want to send and accumulate the information in each vehicles but only the initial information ie i dont send all the accumulated information. 我想发送和累积每辆车中的信息,但只提供初始信息,即我不发送所有累积的信息。

You can extend the WSM to contain the information that you want to exchange. 您可以扩展WSM以包含要交换的信息。 Here is an example of extending WSM and creating a message for your own purpose. 是扩展WSM并为您自己的目的创建消息的示例。

Simply declare variables inside the message definition that will hold your data 只需在消息定义内声明变量即可保存您的数据

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;
}

Then when generating MyAppsPacket you can do: 然后,在生成MyAppsPacket您可以执行以下操作:

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

Unfortunately I can not give you a read-to-use solution since I don't know the very details of your code, but this should give you a rough idea of what you should do. 不幸的是,由于我不了解您的代码的非常详细的信息,因此我无法为您提供一个即用型解决方案,但这应该使您对应该执行的操作有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM