Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c++ - 在omnet ++中到达目的地时无法删除msg?(unable to delete msg when reaches to the destination in omnet++?)

I'm a newbie in OMNeT++ and I want to make a simple broadcast mechanism where starting node sends a msg and it will broadcast it to all nodes connecting in a mesh topology.

(我是OMNeT ++的新手,我想建立一个简单的广播机制,其中起始节点发送一个味精,它将广播它到以网格拓扑连接的所有节点。)

When msg reaches its destination, the msg will be deleted and does not broadcast further more.

(当味精到达其目的地时,该味精将被删除并且不再广播。)

Continuously, I'm working on it from 2 days by following it's official documentation.

(持续地,我从2天开始按照其官方文档进行研究。)

My code is successfully simulated.

(我的代码已成功模拟。)

But it doesn't delete the msg when it reaches to destination.

(但是当到达目的地时,它不会删除味精。)

I'm starting with just 25 nodes (computer) in which source node is computer0 and destination node is computer24.

(我仅从25个节点(计算机)开始,其中源节点是computer0,目标节点是computer24。)

Tell me, where I'm wrong.

(告诉我,我错了。)

Below is my source code:

(下面是我的源代码:)

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>

using namespace omnetpp;

class computer : public cSimpleModule
{
protected:
    virtual void forwardMessage(cMessage *msg);
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(computer);

void computer::initialize()
{
    if (getIndex() == 0) {

        char msgname[20];
        sprintf(msgname, "msg-%d", getIndex());
        cMessage *msg = new cMessage(msgname);
        scheduleAt(0.0, msg);
    }
}

void computer::handleMessage(cMessage *msg)
{
    if (getIndex() == 24) {

            EV << "Message " << msg << " arrived.
";
            delete msg;
        }
        else {

            forwardMessage(msg);
        }
    }

void computer::forwardMessage(cMessage *msg)
{

    int n = gateSize("gate");
    int k = intuniform(0, n-1);

    EV << "Forwarding message " << msg << " on gate[" << k << "]
";
    send(msg, "gate$o", k);
}

simulation snap: https://i.stack.imgur.com/a4JRQ.png

(模拟快照: https : //i.stack.imgur.com/a4JRQ.png)

  ask by Shahnawaz Irfan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You use getIndex() method that return the position of a module in the array of modules.

(您可以使用getIndex()方法返回模块在模块数组中的位置。)

However, the simulation snap shows that you do not use array of modules.

(但是,仿真快照显示您没有使用模块数组。)

Therefore getIndex() returns zero for every module.

(因此, getIndex()对于每个模块都返回零。)

The simplest way to correct your model is to check the name of a module instead of its position, for example:

(校正模型的最简单方法是检查模块的名称而不是模块的位置,例如:)

void computer::initialize()
{
   // if (getIndex() == 0) {
   if (isName("computer0")) {
        char msgname[20];
   // ...


void computer::handleMessage(cMessage *msg)
{
   // if (getIndex() == 24) {
   if (isName("computer24")) {
        EV << "Message " << msg << " arrived.
";
   // ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...