indraneelb1903
indraneelb1903

Reputation: 41

How to get access to the other similar modules in Omnet++

I am using multiple Adhoc Hosts in my Network and I need pointers to all adhoc hosts present in the network in my UDPApp.cc file. If I use getParentModule(),I get access to only the module on which that application is called. So if there are 3 hosts-> host1,host2 and host3. I get access to only host1, host 2 , host 3 but that separately.I want pointers to all 3 at the same time.

Or a pointer to the network which contains them.

Upvotes: 4

Views: 1879

Answers (2)

Jeffrey Redondo
Jeffrey Redondo

Reputation: 1

Really good answer! I was having problems using getSubmodule, instead after using the suggested iteration I was able to find the module. from @Jerzy D.

here my code:

cModule* test = this->getParentModule();
for (SubmoduleIterator it(test); !it.end(); ++it) {
    cModule * mod = *it;
    EV << "Module : " << mod->getName() << endl;
    std::string mod_name = mod->getName();
    std::string x = "wlan";
    if (x.compare(mod_name) == 0) {
        // check whether it is the same host
        test3 = mod;
        EV << "Module : " << mod->getName() << endl;
        cModule *test2 = test1->getSubmodule("mac");
    }
}

Upvotes: 0

Jerzy D.
Jerzy D.

Reputation: 7002

You can use getModuleByPath(path) from any module to look for a module with indicated name and path in the whole simulation network.
An example (assuming that there are 10 hosts):

for (int i = 0; i < 9; ++i) {
    char buf[20];
    sprintf(buf, "host%d", i);
    cModule * mod = getModuleByPath(buf);
    if (mod != nullptr) {
       // ... 
       // now mod contains the pointer to another host's module
    } else {
        EV << "No module " << buf << endl;
    }
}

EDIT
Assuming that every AdHoc host has a submodule called manetrouting a more generic solution may be used:

cModule *network = cSimulation::getActiveSimulation()->getSystemModule();
for (SubmoduleIterator it(network); !it.end(); ++it) {
    cModule * mod = *it;
    if (mod->getSubmodule("manetrouting") != nullptr) {
        // check whether it is the same host
        if (this != mod && getParentModule() != mod) {
           EV << "Host " << mod->getName() << " is anther AdHoc host (not itself)" << endl;
        }
    } 
}

An additional condition has been added to omit the host involving this code in results.

Upvotes: 3

Related Questions