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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…