0

我正在研究 OMNET++ 内的 Veins 框架。我正在开发一种车载网络中的受控洪水。我试图让车辆仅在 6 秒后发送数据(此值可能会更改),所以我在每辆车的初始化时创建了一个时间表(在 TraCIDemo11p.cc 上),如下所示:

if(sendData){ scheduleAt(simTime()+par("dataInterval").doubleValue(),sendDataEvt);}

在 TraCIDemo11p.h 我创建了一个地图作为我的缓冲区:

//Creating a map to represent the vehicle's message buffer
typedef map<string, WaveShortMessage*> MyMapContextMessageBuffer;
typedef pair<string, WaveShortMessage*> MyPairContextMessageBuffer;
MyMapContextMessageBuffer contextLocalMessageBuffer;

所以在 TraCIDemo11p.cc onData(WaveShortMessage* wsm) 函数上,我复制接收到的消息,更新接收到的消息的数量,在车辆缓冲区中添加消息并打印缓冲区的内容只是为了测试我是否插入了它正确:

//duplicating the message, so this vehicle has his own copy of the message
WaveShortMessage* wsmdup = wsm->dup();

//number of received messages
receivedMsgs++;

//add the msg in the  vehicle buffer, the GlobalMessageIdentification is the id of the message
contextLocalMessageBuffer.insert(MyPairContextMessageBuffer(wsmdup->getGlobalMessageIdentificaton(),wsmdup));

//accessing something in the ->second on the buffer, it works in this function
cout<<"Sender "<< contextLocalMessageBuffer.begin()->second->getSender()<<endl;

然后在预定时间调用 SEND_DATA_EVT (handleSelfMsg) 来发送数据,但是当我尝试访问 contextLocalMessageBuffer.begin()-second (或 it->second)时出现错误。

//handleSelfMsg(cMessage* msg)
case SEND_DATA_EVT 

//if the vehicle has received a msg before
if(receivedMsgs != 0){
  //and the buffer isn't empty, the function is able to verify if the buffer isnt empty
  if(!contextLocalMessageBuffer.empty()){


    //if i try to access the begin->first it works    
    cout<<"GlobalMessageID "<<contextLocalMessageBuffer.begin()->first<<" ";
    //but if I try to access the begin->second->getSource(), it doesn't works in this function (i get an Error 139), but works inside of onData    
    cout<<findHost()->getFullName()<<" messageID "<<contextLocalMessageBuffer.begin()->first<<" "<<contextLocalMessageBuffer.begin()->second->getSource()<<endl;

    //if I remove those couts the iterator and the for works but the send doesn't (Error 139), because it is trying to access the it->second
    map<string, WaveShortMessage*>::iterator it;
    for(it = contextLocalMessageBuffer.begin(); it != contextLocalMessageBuffer.end(); it++){

      send(it->second)
    }    
  }
}
//create a new schedule to send data after a new interval
scheduleAt(simTime() + par("dataInterval").doubleValue(), sendDataEvt);

所以,总而言之,我的问题是我能够在 onData 中使用我的地图(contextLocalMessagebuffer),但无法在 HandleSelfMsg 上正确访问它,两者都在同一代码(TraCIDemo11p.cc)中。Obs:我尝试创建一个返回缓冲区值的函数,但该函数也无法访问缓冲区。

谢谢!

4

0 回答 0