Update message after visiting central node every time

27 views
Skip to first unread message

Ribhu Majumder

unread,
Jun 28, 2016, 12:59:34 PM6/28/16
to OMNeT++ Users
Hi all,
I have created a network like this:
The code is :

/*
 * Sample2.cc
 *
 *  Created on: Jun 27, 2016
 *      Author: subhrajit
 */

#include <string.h>
#include <omnetpp.h>

using namespace omnetpp;

class Sample2 : public cSimpleModule
{

    bool homeNode = false;

    int cont = 0;

  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;

};

Define_Module(Sample2);

void Sample2::initialize()
{
    if (strcmp("sample2", getName()) == 0) {
        // The `ev' object works like `cout' in C++.
        EV << "Sending initial message\n";

        homeNode = true;
        cMessage *msg = new cMessage("Subhrajit");
        send(msg, "gate$o",1);
    }
}

void Sample2::handleMessage(cMessage *msg)
{

    if (homeNode)

    {

          // do something

          cont++;

          msg->setName("abcd");
    }
    int n = gateSize("gate");
        int k = intuniform(0, n-1);

        EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
        send(msg, "gate$o", k);


    // msg->getName() is name of the msg object, here it will be "tictocMsg".
    EV << "Received message `" << msg->getName() << "', sending it out again\n";

}

NOW THE MESSGAE IS GETTING CHANGED ONLY THE FIRST TIME IT IS REACHING THE CENTRAL NODE(SAMPLE2)

BUT I WANT TO UPDATE THE MESSAGE EACH AND EVERY TIME  IT VISITS THE CENTRAL NODE(SAMPLE2). SUPPOSE THE FIRST TIME THE MESSAGE WILL BE "ABC".NEXT TIME THE NEXT LETTER WILL ADD AND IT WILL BE" ABCD" AND SO ON. CAN ANYONE PLEASE TELL ME WHAT TO DO IN THE CODE TO DO LIKE THIS.

THANK YOU.

Alfonso Ariza Quintana

unread,
Jun 28, 2016, 2:54:26 PM6/28/16
to omn...@googlegroups.com

You should study the c++ stl, this is a c++ problem, not a omnet problem

 

  if (homeNode)

{
          // do something
          cont++;

          std::string str(msg->getName());

          if (str.size() < 3)

                 str = “abc”;

         else

                str += “d”;

          msg->setName(str.c_str());
    }

--
You received this message because you are subscribed to the Google Groups "OMNeT++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+u...@googlegroups.com.
Visit this group at https://groups.google.com/group/omnetpp.
For more options, visit https://groups.google.com/d/optout.

Ribhu Majumder

unread,
Jun 28, 2016, 3:41:44 PM6/28/16
to OMNeT++ Users
Thank you so much Sir. I dont know C++ much. I am doing the rest part of the OMNET simulation. But I am facing so much troubles in the coding part. Thanks a lot for helping me. It worked. :)
Reply all
Reply to author
Forward
0 new messages