仅发送一次消息而不是定期发送

时间:2017-04-03 11:02:23

标签: omnet++ veins

我开发了一个场景,首先车辆发出自我消息,并在收到自我消息车辆时向RSU发送消息。

自我消息代码以initialize()方法编写。但在模拟过程中,车辆每秒都会向RSU发送消息。

我希望邮件只发送一次。我该怎么办? 我附上了我的 TraCIDemo11p.cc 类的handleSelfmessage方法。

    if(msg->isSelfMessage()==true)
    {
        cModule *tmpMobility = getParentModule()->getSubmodule("veinsmobility");
        mobility = dynamic_cast<Veins::TraCIMobility*>(tmpMobility);
        ASSERT(mobility);
        t_channel channel = dataOnSch ? type_SCH : type_CCH;
        WaveShortMessage* wsm = prepareWSM("data", dataLengthBits, channel, dataPriority, -1,2);

        wsm->setSenderAddress(myAddress);
        wsm->setRecipientAddress(1001);
        sendMessage(wsm->getWsmData());
    }

2 个答案:

答案 0 :(得分:2)

您的方法似乎正确,但显然您的实施存在一些问题。

或者,您可以创建新消息并将其发送给自己

myOneTimeMsg = new cMessage("OneTimeMsg");
scheduleAt(simTime()+1.0, myOneTimeMsg); // this will send the message at t=currentTime+1.0 seconds

然后您可以按如下方式处理该消息:

if(msg->isSelfMessage()==true){
    if (msg == myOneTimeMsg) {
        // do what you need next...

答案 1 :(得分:1)

修改@ user4786271的答案:

handleSelfMsg的{​​{1}}方法显然是针对此模块接收的每条自我消息执行的 - 可能也是非WSM。因此,如果您只是在那里添加了给定的代码,它将为每个自我消息发送WSM。因此,仅检查自助消息类型是不够的。您需要创建一个新的消息类型并检查该类型,如@ user4786271所示。

相关问题