gpt4 book ai didi

c++ - 将结构与消息一起发送,累积消息并重新发送给 Veins 中的邻居

转载 作者:行者123 更新时间:2023-11-28 05:49:33 26 4
gpt4 key购买 nike

我想从所有邻居节点连同消息一起累积一种信任值(比如 'theta=',介于 0.5 到 1 之间),然后将累积的消息和信任值发送到其他节点。还需要以 .txt 文件或 cvs 格式制作这些值的日志文件。 结构可能是这样的:

struct knownVehicle {
int id;
std::string vType;
double theta=0.5;
simtime_t lastSeenAt;
};

void TraCITestApp::onData(EWaveShortMessage* wsm) {
findHost()->getDisplayString().updateWith("r=16,green");
annotations->scheduleErase(1, annotations->drawLine(wsm->getSenderPos(), mobility->getPositionAt(simTime()), "blue"));
if (mobility->getRoadId()[0] != ':') traciVehicle->changeRoute("-39539626", 9999);
if (sentMessage){

//Added for Knownvehicle receiving
knownVehicle &recveh = wsm->getKnownVeh();
double rectheta = recveh.theta;
// process other fields of recveh
delete &recveh; // remember to remove dynamically created object

// check if new warning
bool isNewWarning = true;
for (map<long,WaveShortMessages>::iterator i = receivedWarningMessageMap.begin(); i != receivedWarningMessageMap.end(); ++i) {
WaveShortMessage* msg = i->second[0];
if (msg->getTreeId() == wsm->getTreeId())
isNewWarning = false;
}
// add warning message to received messages storage
receivedWarningMessageMap[wsm->getTreeId()].push_back(wsm->dup());
std::ofstream logFile;
logFile.open("log.txt", std::ios_base::app); // if exist it will be overwritten if added "std::ios_base::app" Appends
std::stringstream ss;
std::cout<<"MyID="<<getMyID()<<";"<< "Sender Id="<< wsm->getSenderAddress()<<endl;
std::cout << "Accumulated receivedWarningMessageMap="<<wsm->dup() << std::endl;
logFile <<"MyID="<<getMyID()<< "Sender Id="<< wsm->getSenderAddress()<<endl;

for (auto it = receivedWarningMessageMap.begin(); it != receivedWarningMessageMap.end(); ++it) {
ss << "id=" << static_cast<int>(it->first) << "; wsms Contents=";
for (auto it2 : it->second) {
ss << it2 << "; ";
}
}
ss << endl;

std::cout<<"MyID="<<getMyID()<<";"<< "Sender Id="<< wsm->getSenderAddress()<<endl;
std::cout << "Accumulated receivedWarningMessageMap="<<wsm->dup() << std::endl;
logFile <<"MyID="<<getMyID()<< "Sender Id="<< wsm->getSenderAddress()<<endl;
logFile << "====Accumulated receivedWarningMessageMap (wsm->dup())=>"<<wsm->dup() <<"====\n"<< std::endl;
logFile << ss.str();
logFile <<"======================\n" <<endl;
// logFile.close();


void TraCITestApp::sendMessage1(double theta)
{
sentMessage = true;
t_channel channel = dataOnSch ? type_SCH : type_CCH;
EWaveShortMessage* wsm = prepareWSM("data", dataLengthBits, channel, dataPriority, -1,2);
knownVehicle * veh = new knownVehicle();
veh->theta = 0.5;

// set other fields...
WaveShortMessage * packet = new WaveShortMessage();

packet->setKnownVeh(*veh); // I got error here ...

// and now packet is ready to send, using send() OMNeT++ method
sendWSM(wsm);
}

最佳答案

为了使用 WaveShortMessage 发送 knownVehicle 实例,对 WaveShortMessage.msg 进行以下修改:

// file: /veins/src/veins/modules/messages/WaveShortMessage.msg
cplusplus {{
#include "veins/base/utils/Coord.h"
struct knownVehicle {
int id;
std::string vType;
double theta;
simtime_t lastSeenAt;
};
}}

class noncobject knownVehicle;
class noncobject Coord;

packet WaveShortMessage {
//Version of the Wave Short Message
int wsmVersion = 0;
// ...
knownVehicle knownVeh;
}

当你想发送带有 knownVehicle 对象的 WaveShortMessage 时,使用下面的代码:

// add include:
// #include "WaveShortMessage_m.h"

knownVehicle * veh = new knownVehicle();
veh->theta = 0.5;
veh->id = 15;
// set other fields...
WaveShortMessage * packet = new WaveShortMessage();
packet->setKnownVeh(*veh);
// and now packet is ready to send, using send() OMNeT++ method

接收 WaveShortMessage:

void TraCIDemo11p::onData(WaveShortMessage* wsm) {
knownVehicle & recveh = wsm->getKnownVeh();
double rectheta = recveh.theta;
// process other fields of recveh
delete &recveh; // remember to remove dynamically created object

// existing code of this method
findHost()->getDisplayString().updateWith("r=16,green");
// ...
}

关于c++ - 将结构与消息一起发送,累积消息并重新发送给 Veins 中的邻居,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com