Omnet/Inet 访问 C++ 中的自定义应用程序

问题描述 投票:0回答:1

我有一个已设置并正在运行的自定义应用程序。我可以使用 onmetpp.ini 来获得应用程序值,该值随后在开始模拟后应用。我想要做的是更改应用程序文件中的值(例如 messageLength 参数)。这是我的设置:

  1. Omnetpp.ini 文件(定义了应用程序的部分)
...
*.drone[*].**.bitrate = 10Mbps
*.drone[*].ipv4.arp.typename = "GlobalArp"
*.drone[*].numApps = 1
*.drone[*].app[0].typename = "MyApp"
*.drone[*].app[0].destAddresses = "server"
*.drone[*].app[0].destPort = 5000
*.drone[*].app[0].messageLength = 30000B
*.drone[*].app[0].sendInterval = exponential(120ms)
*.drone[*].app[0].packetName = "myUDPData"
*.drone[*].wlan[0].typename = "AckingWirelessInterface"
*.drone[*].wlan[0].mac.useAck = false
*.drone[*].wlan[0].mac.fullDuplex = false
*.drone[*].wlan[0].radio.transmitter.communicationRange = 50000m
*.drone[*].wlan[0].radio.receiver.ignoreInterference = true
*.drone[*].wlan[0].mac.headerLength = 23B
...
  1. 定义无人机节点的网络的 Ned 文件
import inet.node.contract.INetworkNode;
import inet.physicallayer.wireless.common.contract.packetlevel.IRadioMedium;
import inet.networklayer.configurator.ipv4.Ipv4NetworkConfigurator;

network myNetwork {
...
    submodules:
        drone[numDrones]: <default("ganetwork.DroneNode")> like INetworkNode {
            @display("p=400,150;i=device/drone;is=vs");
        }
...
  1. MyApp 头文件
#include "inet/applications/base/ApplicationBase.h"
#include "inet/common/clock/ClockUserModuleMixin.h"
#include "inet/transportlayer/contract/udp/UdpSocket.h"

class INET_API MyApp : public inet::ClockUserModuleMixin<inet::ApplicationBase>, public inet::UdpSocket::ICallback {
public:
        void setMessageLength() {messageLength = 1234;};
protected:
        enum SelfMsgKinds { START = 1, SEND, STOP };

        // parameters
        std::vector<inet::L3Address> destAddresses;
        std::vector<std::string> destAddressStr;
        int localPort = -1, destPort = -1;
        inet::clocktime_t startTime;
        inet::clocktime_t stopTime;
        bool dontFragment = false;
        const char *packetName = nullptr;
        int messageLength = 0;

...
}
  1. droneNode.cc 文件(这是我想要更改应用程序 MyApp.cc 的消息长度的位置。每个无人机都有此类的一个实例,因此我尝试使用 getModuleByPath() 来访问特定的一个(? ). 当我这样做时,无法访问 MyApp 的现有实例来允许我更改消息长度。
    注意: 我不想在这里创建MyApp的实例,它已经由模拟器基于omnetpp.ini文件创建了。

    即使调试器可以在断点上看到成员和变量,下面的代码也会出现以下编译问题。

droneNode.cc:56:11:错误:“omnetpp::cModule”中没有名为“setMessageLength”的成员

...
#include "droneNode.h"
#include "myApp.h"

Define_Module(DroneNode);

DroneNode::DroneNode() {}
DroneNode::~DroneNode() {}

void DroneNode::initialize(int stage) {'some init code in here'}
void DroneNode::move() {
    char buf2[20] = "app";
    cModule *mod2 = (MyApp*)getSubmodule(buf2, 0); //magic numbers until I get this working!
    mod2->setMessageLength(); //<--- this give me compile issue  
}
...

注意:我的网络和设置基于以下两个教程:

  1. https://inet.omnetpp.org/docs/tutorials/wireless/doc/index.html
  2. osg-earth 包含在 omnetpp-6.0 中

环境: OMNeT++集成开发环境, 版本:6.0,内部版本号:220413-71d8fab425 版权所有 (C) 2005-2022 OpenSim Ltd.
图书馆 inet-4.5.0-3833582230

感谢您的帮助,如果我的问题中有任何遗漏的细节,请告诉我。

c++ omnet++ inet
1个回答
0
投票

在您的代码中,

mod2
是指向
cModule
实例的指针,而不是
MyApp
cModule
没有
setMessageLength()
因此编译器会显示上述错误。您应该将该指针强制转换为指向
MyApp
的指针,例如使用以下代码:

char buf2[20] = "app";
cModule *mod2 = getSubmodule(buf2, 0); 
if (mod2) {
  MyApp * mod3 = dynamic_cast<MyApp*>(mod2);
  if (mod3) {
    mod3->setMessageLength();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.