我想从所有邻居节点累积一种信任值(比如说' 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);
}
答案 0 :(得分:1)
要使用knownVehicle
发送 WaveShortMessage
的实例,请执行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;
}
如果要使用WaveShortMessage
对象发送knownVehicle
,请使用以下代码:
// 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
接收 a 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");
// ...
}