omnet++tictoc3案例解析

ned模块

simple Txc3
{
    parameters:
        @display("i=block/routing");
    gates:
        input in;
        output out;
}

//
// Same as Tictoc2.
//
network Tictoc3
{
    submodules:
        //在tictoc1.ned文件里面,添加节点的图标,图标在安装文件夹里面的block文件夹。并且更加tic蓝色、toc节点黄色:
        tic: Txc3 {
            parameters:
                @display("i=,cyan");
        }
        toc: Txc3 {
            parameters:
                @display("i=,gold");
        }
    connections:
        tic.out --> {  delay = 100ms; } --> toc.in;
        tic.in <-- {  delay = 100ms; } <-- toc.out;
}

omnet++tictoc3案例解析_第1张图片
cc文件

//实现WATCH,实现消息删除,网络事件结束
#include 
#include 
#include 
using namespace omnetpp;

/**
 * In this class we add a counter, and delete the message after ten exchanges.
 */
class Txc3 : public cSimpleModule
{
  private:
    int counter;  // Note the counter here计数器 观察计数

  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Txc3);

void Txc3::initialize()
{
    // Initialize counter to ten. We'll decrement it every time and delete
    // the message when it reaches zero.
    counter = 10;

    // The WATCH() statement below will let you examine the variable under
    // Tkenv. After doing a few steps in the simulation, double-click either
    // `tic' or `toc', select the Contents tab in the dialog that pops up,
    // and you'll find "counter" in the list.
    //观察当前变量的值的变化 自己添加观看窗口
    WATCH(counter);

    //判断当前字符串是否是tic
    //此处获取getName()是获得模块的名称tic/toc
    if (strcmp("tic", getName()) == 0) 
    {
        EV << "Sending initial message\n";
        cMessage *msg = new cMessage("tictocMsg");
        send(msg, "out");
    }
}

void Txc3::handleMessage(cMessage *msg)
{
    // Increment counter and check value.
    counter--;
    if (counter == 0) {
        // If counter is zero, delete message. If you run the model, you'll
        // find that the simulation will stop at this point with the message
        // "no more events".
        //此处getName()获得打印出tic/toc,如果改为msg->getName()获得打印出tictocMsg
        EV << getName() << "'s counter reached zero, deleting message\n";
        delete msg;
        //delete删除指针之后仿真就结束了,还有一个结束仿真的方法调用endSimulation()
    }
    else {
        EV << getName() << "'s counter is " << counter << ", sending back message\n";
        send(msg, "out");
    }
}

运行结果
omnet++tictoc3案例解析_第2张图片
此处tic和toc都有计数器counter,两个counter值是分别减少的
看左下角的框内,每运行一次tcounter的变化
omnet++tictoc3案例解析_第3张图片
omnet++tictoc3案例解析_第4张图片

你可能感兴趣的:(omnet++,github,c++)