omnet++tictoc7案例解析

tictoc7.ned

simple Txc7
{
    parameters:
        //volatile可变的,@unit(s)属性修饰指明当前变量的单位,此处s表示单位为秒
        volatile double delayTime @unit(s);   // delay before sending back message发回消息前延迟
        @display("i=block/routing");
    gates:
        input in;
        output out;
}

network Tictoc7
{
    submodules:
        tic: Txc7 {
            parameters:
                @display("i=,cyan");
        }
        toc: Txc7 {
            parameters:
                @display("i=,gold");
        }
    connections:
        tic.out --> {  delay = 100ms; } --> toc.in;
        tic.in <-- {  delay = 100ms; } <-- toc.out;
}

Txc7.cc

//NED参数定义,NED中的exponential(3S)truncnormal(3s,1s)函数
#include 
#include 
#include 
using namespace omnetpp;
/**
 * In this step we'll introduce random numbers. We change the delay from 1s
 * to a random value which can be set from the NED file or from omnetpp.ini.
 * In addition, we'll "lose" (delete) the packet with a small probability.
 */
class Txc7 : public cSimpleModule
{
  private:
    cMessage *event;
    cMessage *tictocMsg;

  public:
    Txc7();
    virtual ~Txc7();

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

Define_Module(Txc7);
//Txc7::Txc7()
//Txc7::~Txc7()表示函数~Txc7()只对Txc7起作用,或者说只操作Txc7里面的内容,对其他函数的内容不作更改。这里的实际意义是:~Txc7()把Txc7函数残留的消息event、tictocMsg占用的内存释放掉,而不释放其他函数的内存。
//因为第1、2要把其他之前运行后残留的消息event、tictocMsg占用的内存释放掉

Txc7::Txc7()
{
    event = tictocMsg = nullptr;
}

Txc7::~Txc7()
{
    cancelAndDelete(event);
    delete tictocMsg;
}

void Txc7::initialize()
{
    event = new cMessage("event");
    tictocMsg = nullptr;

    if (strcmp("tic", getName()) == 0) {
        EV << "Scheduling first send to t=5.0s\n";
        scheduleAt(5.0, event);
        tictocMsg = new cMessage("tictocMsg");
    }
}

void Txc7::handleMessage(cMessage *msg)
{
    if (msg == event) {
        EV << "Wait period is over, sending back message\n";
        send(tictocMsg, "out");
        tictocMsg = nullptr;
    }
    else {
        // "Lose" the message with 0.1 probability:“丢失”消息的概率为0.1:
        //uniform产生一个double类型的0~1随机数
        //intuniform(a,b)产生一个int类型的a~b随机数
        if (uniform(0, 1) < 0.1) {
            EV << "\"Losing\" message\n";
            delete msg;
        }
        else {
            // The "delayTime" module parameter can be set to values like
            // "exponential(5)" (tictoc7.ned, omnetpp.ini), and then here
            // we'll get a different delay every time.
            //使用par函数读取在简单模块中定义的参数delayTime
            //获取ned参数
            simtime_t delay = par("delayTime");

            EV << "Message arrived, starting to wait " << delay << " secs...\n";
            tictocMsg = msg;
            //自消息延时delay
            scheduleAt(simTime()+delay, event);
        }
    }
}

omnetpp.ini文件

[Config Tictoc7]
network = Tictoc7
# argument to exponential() is the mean; truncnormal() returns values from,exponential()的参数是平均值;truncnormal()返回
# the normal distribution truncated to nonnegative values,正态分布截断为非负值
Tictoc7.tic.delayTime = exponential(3s)
Tictoc7.toc.delayTime = truncnormal(3s,1s)

运行结果
omnet++tictoc7案例解析_第1张图片
omnet++tictoc7案例解析_第2张图片

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