0

I have developed a scenario where at first the vehicles send a self messsage and upon reception of the self message vehicles send a message to RSU.

The self message code is written in the initialize() method. But during simulation the vehicles send the message to RSU every second.

I want the message to be sent only once. What should I do? I have attached the handleSelfmessage method of my TraCIDemo11p.cc class.

    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());
    }
Julian Heinovski
  • 1,822
  • 3
  • 16
  • 27
Tapu
  • 25
  • 5

2 Answers2

2

Your approach seems right, but obviously you have some problem in your implementation.

Alternatively you can create a new message and send it to yourself

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

Then you can handle that message as follows:

if(msg->isSelfMessage()==true){
    if (msg == myOneTimeMsg) {
        // do what you need next...
user4786271
  • 1,555
  • 13
  • 18
1

Amending the answer of @user4786271:

The handleSelfMsg method of TraCIDemo11p.cc obviously is executed for every self-message which this module receives - possibly also non WSMs. So if you just added the given code there, it will send a WSM for every of those self-messages. Thus, only checking for self-message type is not enough. You need to create a new message type and check for that type as shown by @user4786271.

Julian Heinovski
  • 1,822
  • 3
  • 16
  • 27