omnet++tictoc1案例解析

ned模块

//简单模块:模块分为simple\module
simple Txc1
{
    gates:
        input in;
        output out;
}
//网络:网络中容纳了很多的parameter\submodules\connections\属性等
network Tictoc1
{
    submodules:
        tic: Txc1;
        toc: Txc1;
    connections:
        tic.out --> {  delay = 100ms; } --> toc.in;
        tic.in <-- {  delay = 100ms; } <-- toc.out;
}

omnet++tictoc1案例解析_第1张图片
cc模块

#include 
#include 
using namespace omnetpp;
class Txc1 : public cSimpleModule
{
  protected:
    // The following redefined virtual function holds the algorithm.
    //重写函数 所有继承自simple的类都要重写这两个函数
    //重写虚函数,注册后程序运行时自动调用,initialize调用一次,handleMessage接收到消息后运行
    virtual void initialize() override;//初始化
    virtual void handleMessage(cMessage *msg) override;//接收消息
};
// The module class needs to be registered with OMNeT++
//所有模块实现时,必须添加下面这个定义
Define_Module(Txc1);
void Txc1::initialize()
{
    // Initialize is called at the beginning of the simulation.
    // To bootstrap the tic-toc-tic-toc process, one of the modules needs
    // to send the first message. Let this be `tic'.
    // Am I Tic or Toc?
    //getName获取当前实例的名称,也就是NED模块中实例的名称
    //strcmp函数是string compare(字符串比较)的缩写,用于比较两个字符串并根据比较结果返回整数。
    if (strcmp("tic", getName()) == 0) {
        // create and send first message on gate "out". "tictocMsg" is an
        // arbitrary string which will be the name of the message object.
        //定义消息 消息中传入字符串  字符串中就是消息的名称
        cMessage *msg = new cMessage("tictocMsg");
        //进行消息发送 通过out输出门
        send(msg, "out");
        //当send通过Out输出门发送出去后,OMnet++默认加入一个消息事件,toc默认通过内部的消息接受到这个事件
    }
}
//再通过handleMessage回调处理这个事件,回调接收到之后toc将这个消息通过send函数再发送出去,也就是通过toc的out
void Txc1::handleMessage(cMessage *msg)
{
    // The handleMessage() method is called whenever a message arrives
    // at the module. Here, we just send it to the other module, through
    // gate `out'. Because both `tic' and `toc' does the same, the message
    // will bounce between the two.
    send(msg, "out"); // send out the message
}


运行
omnet++tictoc1案例解析_第2张图片

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