0
votes

I am trying to create a node at run time in my module in Omnet. I am able to create it with this code and its working fine.

cModule* parentmod = getParentModule();
cModule* grantParentMod = parentmod->getParentModule();
cModule* grantParentMod1 = grantParentMod->getParentModule();
// To check if the module is already created
for (cSubModIterator iter(*grantParentMod1); !iter.end(); iter++)
{
    EV << iter()->getFullName()<<endl;
    if (iter()->getFullName() == "host_send4")
        return;
 }
cModuleType *meshnode1 = cModuleType::get("inet.networklayer.manetrouting.PASER.meshnode");
cModule *mod = meshnode1->create("host_send4", grantParentMod1);
cDisplayString& dispstr =  getDisplayString();
dispstr.parse("p=1000,535;r=200,green");
mod->finalizeParameters();
mod->buildInside();
mod->scheduleStart(simTime()+2*beaconInterval);

However this module is not generated at desired place in simulation output (the coordinates and the display). I believe the display string created here is not attached to the module and hence I tried to do it by this :-

cDisplayString& dispstr =  getDisplayString();
dispstr.parse("p=1000,535;r=200,green");
mod->getDisplayString().set(dispstr);

But with this I encounter following error at run time :- Cannot access display string yet: Parameters not yet set up . I know the problem is in mod->getDisplayString().set(dispstr); So is there any other way to assign the parameter or am I doing some minor error. Thanks for this help.

2

2 Answers

3
votes

Make sure you are following the module creation procedure as given in the OMNeT++ manual.

If you navigate to the The Detailed Procedure sub-section you will notice a comprehensive list which tells what step should be performed where:

  1. Find the factory object;
  2. Create the module;
  3. Set up its parameters and gate sizes as needed;
  4. Tell the (possibly compound) module to recursively create its internal submodules and connections;
  5. Schedule activation message(s) for the new simple module(s).

Step 3 I believe is the one you are looking for. Little below is given a detailed explanation of what should be done for step 3:

If you want to set up parameter values or gate vector sizes (Step 3.), the code goes between the create() and buildInside() calls:

// create
cModuleType *moduleType = cModuleType::get("foo.nodes.WirelessNode");
cModule *module = moduleType->create("node", this);

// set up parameters and gate sizes before we set up its submodules
module->par("address") = ++lastAddress;
module->finalizeParameters();

module->setGateSize("in", 3);
module->setGateSize("out", 3);

// create internals, and schedule it
module->buildInside();
module->scheduleStart(simTime());

Be aware of the usage of the module->par("<parameter_name>") function.

PS: I was writing my answer, and in meanwhile you answered your own question. This answer can be left there for future reference, if useful.

1
votes

Well I modified the code as :-

cModuleType *meshnode1 = cModuleType::get("inet.networklayer.manetrouting.PASER.meshnode");
    cModule *mod = meshnode1->create("host_send4", grantParentMod1);
    mod->finalizeParameters();

    std::string displayString = "p=1000,535;r=200,green;i=device/smallrouter";
    mod->getDisplayString().parse(displayString.c_str());

    mod->buildInside();
    mod->scheduleStart(simTime()+2*beaconInterval);

and then its working perfect. According to my understanding, I should add mod->finalizeParameters(); before changing the display setting and display string should be a simple string but not the cDisplayString object.