OMNeT 例程 Tictoc6 学习笔记

本文档详细介绍了OMNeT++ Txc6模块的代码,该模块实现了延迟1秒发送消息的功能。通过创建自消息并使用scheduleAt()函数,模块在特定时间点触发发送操作。文中还展示了如何处理接收到的消息,并提供了关键代码段的解释。
摘要由CSDN通过智能技术生成

已弃OPNET坑,开始OMNeT学习,一定要坚持下去!
Tictoc1-16 例程一定要一个一个好好的看过去,给自己一个督促,也给学习OMNeT的战友们分享自己的想法。
以下是 txc6.cc 的代码,自己加了一些注释。txc6.cc 是针对 ned 中 txc6 所做的详细描述,实现了延迟发送的功能。经大神指点,感觉看的还算通透。

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

using namespace omnetpp;//名字空间

/**
 * In the previous models, `tic' and `toc' immediately sent back the
 * received message. Here we'll add some timing: tic and toc will hold the
 * message for 1 simulated second before sending it back. In OMNeT++
 * such timing is achieved by the module sending a message to itself.
 * Such messages are called self-messages (but only because of the way they
 * are used, otherwise they are completely ordinary messages) or events.
 * Self-messages can be "sent" with the scheduleAt() function, and you can
 * specify when they should arrive back at the module.
 *
 * We leave out the counter, to keep the source code small.
 */
class Txc6 : public cSimpleModule
{
  private:		//私有属性,定义了两个cMessage的指针变量
    cMessage *event;  // pointer to the event object which we'll use for timing
    cMessage *tictocMsg;  // variable to remember the message until we send it back

  public:
    Txc6();//构造函数,在txc6的基础上构造函数
    virtual ~Txc6();//析构函数,把构造的函数删除

  protected:	//固定的写法
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Txc6);	//宏注册

Txc6::Txc6()
{
    // Set the pointer to nullptr, so that the destructor won't crash
    // even if initialize() doesn't get called because of a runtime
    // error or user cancellation during the startup process.
    event = tictocMsg = nullptr;	//初始化为空指针
}

Txc6::~Txc6()
{
    // Dispose of dynamically allocated the objects
    //模块析构函数中,删除已分配的自消息
    cancelAndDelete(event);		//把自消息event先取消再删除
    delete tictocMsg;		//直接删除
}

void Txc6::initialize()
{
    // Create the event object we'll use for timing -- just any ordinary message.
    event = new cMessage("event");

    // No tictoc message yet.
    tictocMsg = nullptr;

    if (strcmp("tic", getName()) == 0) {
        // We don't start right away, but instead send an message to ourselves
        // (a "self-message") -- we'll do the first sending when it arrives
        // back to us, at t=5.0s simulated time.
        EV << "Scheduling first send to t=5.0s\n";
        tictocMsg = new cMessage("tictocMsg");
        scheduleAt(5.0, event);//在5s时刻把消息发给自己
    }
}

void Txc6::handleMessage(cMessage *msg)
{
    // There are several ways of distinguishing messages, for example by message
    // kind (an int attribute of cMessage) or by class using dynamic_cast
    // (provided you subclass from cMessage). In this code we just check if we
    // recognize the pointer, which (if feasible) is the easiest and fastest
    // method.
    if (msg == event) {
        // The self-message arrived, so we can send out tictocMsg and nullptr out
        // its pointer so that it doesn't confuse us later.
        EV << "Wait period is over, sending back message\n";
        send(tictocMsg, "out");
        tictocMsg = nullptr;
    }
    else {
        // If the message we received is not our self-message, then it must
        // be the tic-toc message arriving from our partner. We remember its
        // pointer in the tictocMsg variable, then schedule our self-message
        // to come back to us in 1s simulated time.
        EV << "Message arrived, starting to wait 1 sec...\n";
        tictocMsg = msg;
        scheduleAt(simTime()+1.0, event);//在当前仿真时刻+1s时刻把消息发给自己
    }
}

namespace 是C++的内容,作用是同样名字的东西不会有冲突。
strcmp 是字符串比较,是一起比较的,不是一位一位比较。strcmp("tic", getName()) == 0 说明 tic 等于 getName() ;若等于1,则不相等。strcmp 是很重要的!
EV 是OMNeT的打印,C++ 的打印是 count ,C语言的打印是 printf 。

在OMNeT里,通过模块给自身发送消息来实现处理延迟。这种消息也被称之为“自消息”(Self-messages)。

Event1 在 t = 5 时刻,tic 收到自消息,打印信息(Wait period is over, sending back message),并把 tictocMsg 通过 out 门发送出去;
Event2 在 t = 5.1 时刻,toc 收到消息,打印信息(Message arrived, starting to wait 1 sec…);
Event3 在 t = 6.1 时刻,toc 收到自消息,打印信息(Wait period is over, sending back message),并把 tictocMsg 通过 out 门发送出去;
Event4 在 t = 6.2 时刻,tic 收到消息,打印信息(Message arrived, starting to wait 1 sec…)
……

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值