omnet++tictoc8案例解析

tictoc8.ned文件

//此处定义了Tic8和Toc8的两个模块,所以需要在cc文件中分别实现Tic8和Toc8两个类
simple Tic8
{
    parameters:
        @display("i=block/routing");
    gates:
        input in;
        output out;
}
simple Toc8
{
    parameters:
        @display("i=block/process");
    gates:
        input in;
        output out;
}
network Tictoc8
{
    @display("bgb=273,257");
    submodules:
        tic: Tic8 {
            parameters:
                @display("i=,cyan;p=205,208");
        }
        toc: Toc8 {
            parameters:
                @display("i=,gold");
        }
    connections:
        tic.out --> {  delay = 100ms; } --> toc.in;
        tic.in <-- {  delay = 100ms; } <-- toc.out;
}

Txc8.cc文件

//丢包消息处理,定时判断消息是否到达若没有到达则创建消息
#include 
#include 
#include 
using namespace omnetpp;
class Tic8 : public cSimpleModule
{
  private:
    simtime_t timeout;  // timeout超时时间
    cMessage *timeoutEvent;  // holds pointer to the timeout self-message超时消息事件,保存指向超时自消息的指针
  public:
    Tic8();
    virtual ~Tic8();
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Tic8);
Tic8::Tic8()
{
    timeoutEvent = nullptr;
}
Tic8::~Tic8()
{
    cancelAndDelete(timeoutEvent);
}

void Tic8::initialize()
{
    timeout = 1.0;
    timeoutEvent = new cMessage("timeoutEvent");
    EV << "Sending initial message\n";
    cMessage *msg = new cMessage("tictocMsg");
    //tic8发送给toc8
    send(msg, "out");//0.0s发出去
    //消息发送完成后,timeout时间后判断消息有没有接受到
    scheduleAt(simTime()+timeout, timeoutEvent);//1.0s,timeoutEvent发给 tic模块
}

void Tic8::handleMessage(cMessage *msg)
{
    if (msg == timeoutEvent) {
        // If we receive the timeout event, that means the packet hasn't如果我们接收到超时事件,这意味着数据包没有
        // arrived in time and we have to re-send it.及时到达,我们必须重新发送。
        EV << "Timeout expired, resending message and restarting timer\n";
        cMessage *newMsg = new cMessage("tictocMsg");
        send(newMsg, "out");
        scheduleAt(simTime()+timeout, timeoutEvent);
    }
    else {  
    // message arrived消息到达
    // Acknowledgement received -- delete the received message and cancel接收到确认——删除接收到的消息并取消
    // the timeout event.超时事件
        EV << "Timer cancelled.\n";//0.2s
        //cancelEvent取消自消息。定时消息
        cancelEvent(timeoutEvent);
        delete msg;
        // Ready to send another one.重新定义一个tictocMsg
        cMessage *newMsg = new cMessage("tictocMsg");
        send(newMsg, "out");
        EV<< "simTime() is "<<simTime()<<endl;
        scheduleAt(simTime()+timeout, timeoutEvent);//1.2s给自己发送一个消息
    }
}

/**
 * Sends back an acknowledgement -- or not.发回一个确认或不
 */
class Toc8 : public cSimpleModule
{
  protected:
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Toc8);

void Toc8::handleMessage(cMessage *msg)
{
    if (uniform(0, 1) < 0.1) {
        EV << "\"Losing\" message.\n";
        bubble("message lost");  // making animation more informative...出现一个闪动的动画信息提示
        delete msg;
    }
    else {
        EV << "Sending back same message as acknowledgement.\n";//0.1s
        send(msg, "out");
    }
}

omnetpp.ini文件

[Config Tictoc8]
network = Tictoc8

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

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