【问题标题】:A new module that inherit from AdhocHost in omnet++继承自 omnet++ 中 AdhocHost 的新模块
【发布时间】:2018-08-07 19:34:44
【问题描述】:

我需要一个继承自AdhocHost 的新模块。之前有朋友问过this questionJerzy D先生回答说:

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

但手册指出:

尽管复合模块的 C++ 类可以用 @class 属性,这是一个可能永远不应该出现的功能 用过的。将代码封装成一个简单的模块,并将其添加为 子模块。

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

【问题讨论】:

    标签: omnet++ adhoc inet


    【解决方案1】:

    你是对的,在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

    【讨论】:

    • 虽然复合模块的 C++ 类可以被 @class 属性覆盖,但这是一个可能永远不应该使用的特性。将代码封装成一个简单的模块,并将其添加为子模块。
    • 在文件 AdhocHostExtended.cc 中,函数“void AdhocHostExtended::handleMessage(cMessage *msg)”永远不会被调用。其他功能未测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多