'How to provide user input dynamically in Omnet++?

I need to send few messages dynamically from a node Tag to node Anchor and viceversa. Here i have defined the msg variable in ned file.

simple Tag
{
    parameters:
   // bool sendMsgOnInit = default(false); // whether the module 
should send out a message on initialization
    int limit = default(0);   // another parameter with a default 
value
    string msg3;
    @display("i=block/routing");
    gates:
    input in;
    output out;
}


Below is the ini file.

network = Taganchor
**.messageSent.msg3 = ask 

Below is the .cc file:

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

using namespace omnetpp;

/**
 * Derive the Tag class from cSimpleModule. In the Taganchor network,
 * both the `tag' and `anchor' modules are Tag objects, created by 
OMNeT++
 * at the beginning of the simulation.
 */
class Tag : public cSimpleModule
{
  private:
    int counter;
  public:
    // The following redefined virtual function holds the algorithm.
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

// The module class needs to be registered with OMNeT++
Define_Module(Tag);

void Tag::initialize()
{
// Initialize is called at the beginning of the simulation.
// To bootstrap the tag-anchor process, one of the modules needs
// to send the first message. Let this be `init'.
counter = par("limit");
// Am I tag or anchor?
if (strcmp("tag", getName()) == 0) {
    // create and send first message on gate "out". "init" is an
    // arbitrary string which will be the name of the message object.
    cMessage *msg1 = new cMessage("init");
    //cMessage *msg2 = new cMessage("poll");
    send(msg1, "out");


    }
}

void Tag::handleMessage(cMessage *msg)
{
// The handleMessage() method is called whenever a message arrives
// at the module. Here, we just send it to the other module, through
// gate `out'. Because both `tag' and `anchor' does the same, the 
message
// will bounce between the two.


counter++;
//    if (counter == 0) {
//        EV << getName() << "'s counter reached zero, deleting 
message\n";
//        delete msg;
//    }
      if(counter == 1){
        if (strcmp("anchor", getName()) == 0){
       cMessage *msg2= new cMessage("poll");

        send(msg2, "out"); // send out the message

    }

    else {
        
                cMessage *msg3 = new cMessage("response");
                send(msg3, "out");

            }

}
 else if (counter == 2){
     if (strcmp("anchor", getName()) == 0){
        cMessage *msg4 = new cMessage("final");
        send(msg4, "out");
    }

    }
}

When I do not initialize msg3 in .cc file and enter the msg3 value when I run the simulation there seems to be no change in simulation. How to achieve my objective?



Solution 1:[1]

OMNeT++ is a C++ library, so anything that works in C++ works in OMNeT++.

There is also a mechanism to ask the user: https://doc.omnetpp.org/omnetpp/manual/#sec:ned-lang:parameter-assignments

Finally, one can explicitly ask the simulator to prompt the user interactively for the value (again, provided that interactivity is enabled; otherwise this will result in an error): **.sendInterval = ask

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rudi