omnet++tictoc2案例解析

ned模块

simple Txc2
{
    parameters:
        //加上这句话以后,图标看起来像路由器。所有Txc1类型的模块均会显示成这个形式
        @display("i=block/routing"); // add a default icon
    gates:
        input in;
        output out;
}

//
// Make the two module look a bit different with colorization effect.
// Use cyan for `tic', and yellow for `toc'.
//
network Tictoc2
{
    submodules:
        tic: Txc2 {
            parameters:
                @display("i=,cyan"); // do not change the icon (first arg of i=) just colorize it
        }
        toc: Txc2 {
            parameters:
                @display("i=,gold"); // here too
        }
    connections:
        tic.out --> {  delay = 100ms; } --> toc.in;
        tic.in <-- {  delay = 100ms; } <-- toc.out;
}

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

//实现模块的显示,实现日志的打印输出

#include 
#include 

using namespace omnetpp;

class Txc2 : public cSimpleModule
{
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Txc2);

void Txc2::initialize()
{
    if (strcmp("tic", getName()) == 0) {
        // The `ev' object works like `cout' in C++.
        //c/c++打印输出   omnet++自带的打印EV(日常的基本类型都可打印)
        //此处是在代码中添加换行符\n实现换行
        //还有一种方法是在EV<<""<
        EV << "Sending initial message\n";
        cMessage *msg = new cMessage("tictocMsg");
        send(msg, "out");
    }
}

void Txc2::handleMessage(cMessage *msg)
{
    // msg->getName() is name of the msg object, here it will be "tictocMsg".
    //打印消息接收 打印消息名称
    EV << "Received message `" << msg->getName() << "', sending it out again\n";
    send(msg, "out");
}
//黑色显示的消息就是打印的日志,蓝色的就是消息日志

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

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