使用 INET 扩展模块未正确执行覆盖代码
Not properly executed overridden code using an INET extended module
我必须在 OMNeT++ 5.6.1 (Ubuntu 18.04.4) 中从 INET4 扩展 UdpBasicApp 模块并执行两个覆盖的方法(初始化和 handleMessageWhenUp)。
这似乎是一项简单的任务,当我构建项目(暂时存储在 inet4 的 "examples" 文件夹中)时看起来一切正常,没有错误,但是在开始模拟后,代码没有被成功覆盖。改为执行原始代码(来自 UdpBasicApp 的代码)。
事实上,在编辑器中检查代码后,方法的名称不是 bold(应该是)并且关键字没有像通常应该的那样着色。
我的 C++ 技能不高(我的 OMNeT 技能也不高)因此我不确定到底发生了什么。
扩展的简单模块称为“PriorityApp”(我只添加了一个整数字段)。
这是.ned代码(不知道你是否需要)
package inet.examples.MyProject.src;
import inet.applications.udpapp.UdpBasicApp;
// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
parameters:
int priority = default(0); // priority 0 = higher priority
}
这是.h代码
#ifndef __INET4_PRIORITYAPP_H_
#define __INET4_PRIORITYAPP_H_
#include "inet/applications/udpapp/UdpBasicApp.h"
namespace inet {
class PriorityApp : public UdpBasicApp
{
protected:
int priority;
virtual void initialize(int stage) override;
virtual void handleMessageWhenUp(cMessage *msg) override;
};
} // namespace inet
#endif // ifndef __INET_PRIORITYAPP_H
这是.cc代码
#include "PriorityApp.h"
namespace inet {
Define_Module(PriorityApp);
void PriorityApp::initialize(int stage)
{
msg = new cMessage("priority");
EV << "Example Message" << endl;
}
void PriorityApp::handleMessageWhenUp(cMessage *msg)
{
}
} //namespace inet
这是 .ini 文件
[General]
[Config SwitchedNet1G]
description = "1Gbps Switched Network"
network = SwitchedNet
**.dataRate = 1Gbps # 1 Gbps channel datarate
# Priority stuff
**.host[0].app[0].priority = 1
# Dst Params
**.sink.app[*].typename = "UdpSink" # Destination host (it receives messages)
**.sink.app[*].localPort = 2500
# Src Params
**.host[0].app[0].packetName = "Host0-Data"
**.host[*].app[0].typename = "PriorityApp" # Source host (they send messages)
**.host[*].app[0].destPort = 2500
**.host[*].app[0].destAddresses = "sink"
**.host[0].app[0].sendInterval = 3s
**.host[*].app[0].startTime = uniform (0s, 3s)
# EthInterface Setup
**.eth[*].qEncap.typename = "Ieee8021qEncap" # 8021Q Encapsulation
**.etherSwitch.numEthInterfaces = 2 # Switch ethInterfaces total number (2)
**.numEthInterfaces = 1
# Global Params
**.messageLength = 100B
**.numHosts = 1
**.numApps = 1 # every host has 1 app (UdpBasicApp or UdpSink)
这里有两个独立的故障:
1.) 您不应将 C++ 代码添加到 INET 的示例文件夹中。该文件夹未指定为源文件夹,因此构建系统不会获取、构建和 link 可执行文件,这意味着您的代码不存在于模型中。这就是您没有获得新行为的原因之一。
2.) 即使您将源代码与 UdpBasicApp 一起放在 src
文件夹中并且构建系统会正确 link 它,您仍然必须正确设置 NED部分。具体来说,您必须指定 @class 属性 来告诉 NED 您打算使用哪个 C++ class 来指定行为:@class(PriorityApp)
否则您的 PriorityApp 模块将继承 @ 的值class 属性 来自 UdpBasicApp,并将使用 CdpBasicApp C++ class。
import inet.applications.udpapp.UdpBasicApp;
// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
@class(PriorityApp)
parameters:
int priority = default(0); // priority 0 = higher priority
}
我必须在 OMNeT++ 5.6.1 (Ubuntu 18.04.4) 中从 INET4 扩展 UdpBasicApp 模块并执行两个覆盖的方法(初始化和 handleMessageWhenUp)。 这似乎是一项简单的任务,当我构建项目(暂时存储在 inet4 的 "examples" 文件夹中)时看起来一切正常,没有错误,但是在开始模拟后,代码没有被成功覆盖。改为执行原始代码(来自 UdpBasicApp 的代码)。 事实上,在编辑器中检查代码后,方法的名称不是 bold(应该是)并且关键字没有像通常应该的那样着色。 我的 C++ 技能不高(我的 OMNeT 技能也不高)因此我不确定到底发生了什么。 扩展的简单模块称为“PriorityApp”(我只添加了一个整数字段)。
这是.ned代码(不知道你是否需要)
package inet.examples.MyProject.src;
import inet.applications.udpapp.UdpBasicApp;
// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
parameters:
int priority = default(0); // priority 0 = higher priority
}
这是.h代码
#ifndef __INET4_PRIORITYAPP_H_
#define __INET4_PRIORITYAPP_H_
#include "inet/applications/udpapp/UdpBasicApp.h"
namespace inet {
class PriorityApp : public UdpBasicApp
{
protected:
int priority;
virtual void initialize(int stage) override;
virtual void handleMessageWhenUp(cMessage *msg) override;
};
} // namespace inet
#endif // ifndef __INET_PRIORITYAPP_H
这是.cc代码
#include "PriorityApp.h"
namespace inet {
Define_Module(PriorityApp);
void PriorityApp::initialize(int stage)
{
msg = new cMessage("priority");
EV << "Example Message" << endl;
}
void PriorityApp::handleMessageWhenUp(cMessage *msg)
{
}
} //namespace inet
这是 .ini 文件
[General]
[Config SwitchedNet1G]
description = "1Gbps Switched Network"
network = SwitchedNet
**.dataRate = 1Gbps # 1 Gbps channel datarate
# Priority stuff
**.host[0].app[0].priority = 1
# Dst Params
**.sink.app[*].typename = "UdpSink" # Destination host (it receives messages)
**.sink.app[*].localPort = 2500
# Src Params
**.host[0].app[0].packetName = "Host0-Data"
**.host[*].app[0].typename = "PriorityApp" # Source host (they send messages)
**.host[*].app[0].destPort = 2500
**.host[*].app[0].destAddresses = "sink"
**.host[0].app[0].sendInterval = 3s
**.host[*].app[0].startTime = uniform (0s, 3s)
# EthInterface Setup
**.eth[*].qEncap.typename = "Ieee8021qEncap" # 8021Q Encapsulation
**.etherSwitch.numEthInterfaces = 2 # Switch ethInterfaces total number (2)
**.numEthInterfaces = 1
# Global Params
**.messageLength = 100B
**.numHosts = 1
**.numApps = 1 # every host has 1 app (UdpBasicApp or UdpSink)
这里有两个独立的故障:
1.) 您不应将 C++ 代码添加到 INET 的示例文件夹中。该文件夹未指定为源文件夹,因此构建系统不会获取、构建和 link 可执行文件,这意味着您的代码不存在于模型中。这就是您没有获得新行为的原因之一。
2.) 即使您将源代码与 UdpBasicApp 一起放在 src
文件夹中并且构建系统会正确 link 它,您仍然必须正确设置 NED部分。具体来说,您必须指定 @class 属性 来告诉 NED 您打算使用哪个 C++ class 来指定行为:@class(PriorityApp)
否则您的 PriorityApp 模块将继承 @ 的值class 属性 来自 UdpBasicApp,并将使用 CdpBasicApp C++ class。
import inet.applications.udpapp.UdpBasicApp;
// The module extends UdpBasicApp adding a priority parameter
simple PriorityApp extends UdpBasicApp
{
@class(PriorityApp)
parameters:
int priority = default(0); // priority 0 = higher priority
}