从omnet ++的AdhocHost继承的新模块

时间:2018-08-07 19:34:45

标签: omnet++ adhoc inet

我需要一个继承自AdhocHost的新模块。朋友在this question前问,Jerzy D先生回答:

  

在OMNeT ++中,行为仅针对简单模块定义。所以一个不能   为复合模块定义C ++类。

但手册指出:

  

尽管复合模块的C ++类可以用   @class属性,此功能可能永远不应该   用过的。将代码封装到一个简单的模块中,然后将其添加为   子模块。

如何创建此模块?从头开始创建cSimpleModule模块是不合逻辑的,因为我想使用预定义的AdhocHost参数,方法...以及我的新定义。

1 个答案:

答案 0 :(得分:0)

是的,在OMNeT++中可以为复合模块定义一个类。您提到的我的回答不是100%正确。


要创建一个继承自AdhocHost并具有自己的类的新模块,至少应这样做:

  1. inet4\src\inet\node\inet中创建一个新文件AdhocHostExtended.ned

    package inet.node.inet;
    import inet.node.inet.AdhocHost;
    
    module AdhocHostExtended extends AdhocHost {
        @class(AdhocHostExtended);
        int par1;
        double par2;
    }
    
  2. 在同一目录中创建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. 在同一目录中创建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
    

请注意在initialize()内使用适当的舞台。阶段在/inet4/src/inet/common/InitStages.h

中定义