Search code examples
omnet++adhocinet

A new module that inherit from AdhocHost in omnet++


I need a new module that inherit from AdhocHost. Friends asked before this question, and Mr Jerzy D answered that:

In OMNeT++ behavior is defined only for simple modules. So one cannot define a C++ classes for a compound module.

but the manual states:

Although the C++ class for a compound module can be overridden with the @class property, this is a feature that should probably never be used. Encapsulate the code into a simple module, and add it as a submodule.

How can I create this module? It is not logical to create a cSimpleModule module from scratch because I wanna use predefined AdhocHost parameters,method,... and also my new define.


Solution

  • You are right, in OMNeT++ there is the possibility to define a class for compound module. My answer you have mentioned was not 100% correct.


    To create a new module that inherits from AdhocHost and has own class one should do at least:

    1. In inet4\src\inet\node\inet create a new file AdhocHostExtended.ned:

      package inet.node.inet;
      import inet.node.inet.AdhocHost;
      
      module AdhocHostExtended extends AdhocHost {
          @class(AdhocHostExtended);
          int par1;
          double par2;
      }
      
    2. In the same directory create AdhocHostExtended.h:

      #ifndef __INET4_ADHOCHOSTEXTENDED_H_
      #define __INET4_ADHOCHOSTEXTENDED_H_
      
      #include <omnetpp.h>
      using namespace omnetpp;
      
      namespace inet {
      
      class AdhocHostExtended : public cModule  {
        protected:
          virtual int numInitStages() const override { return NUM_INIT_STAGES; }
          virtual void initialize(int stage) override;
          virtual void handleMessage(cMessage *msg);
      };
      
      } //namespace
      #endif
      
    3. In the same directory create AdhocHostExtended.cc:

      #include "AdhocHostExtended.h"
      namespace inet {
      
      Define_Module(AdhocHostExtended);
      
      void AdhocHostExtended::initialize(int stage) {
        // an example of reading new parameter in the first stage
        if (stage == INITSTAGE_LOCAL) {
           EV << "par1 = " << par("par1").intValue() << endl;
        }
        // your code
      }
      
      void AdhocHostExtended::handleMessage(cMessage *msg) {
           // your code
      }      
      
      } //namespace
      

    Take care of using a proper stage inside initialize(). Stages are defined in /inet4/src/inet/common/InitStages.h