omnet++仿真实例(一)

0x00 tictoc

1.建立项目

选择一个空文件夹作为 工作台(workspace),从File->New -> OMNeT++ Project from the menu.新建项目

项目名输入tictoc

图片.png

选择空项目


然后直接点击完成

一个项目通常会被分为 src/simulations/目录,并且他们下面可能会有子目录

2. 添加NED文件

omnet++ 使用ned文件定义组件并且将他们整合起来组成更大的单元(例如网络),我们通过添加ned文件来实现模型

通过右键项目 New -> Network Description File (NED)来添加ned文件

下面创建一个名为tictoc1.ned的文件



使用下面的Source模式来编辑源代码

ned文件的编辑有两种模式:源码模式(Source)和设计模式(Design)

输入以下代码

simple Txc1
    {
        gates:
            input in;
            output out;
    }
    
    //
    // Two instances (tic and toc) of Txc1 connected both ways.
    // Tic and toc will pass messages to one another.
    //
    network Tictoc1
    {
        submodules:
            tic: Txc1;
            toc: Txc1;
        connections:
            tic.out --> {  delay = 100ms; } --> toc.in;
            tic.in <-- {  delay = 100ms; } <-- toc.out;
    }
  1. 上面的第一个代码块定义了一个简单模块对象类型Txc1Txc1拥有名为in的输入门(gates),以及名为out的输出门
  2. 第二个代码段声明了一个名为Tictoc1的network对象,他由两个子模块组成,tictoc,他门都是模块对象类型Txc1的实例化,tictoc输入门输出门互相连接,并且会拥有100ms的传输延迟

切换回设计模式,可以看到如下的样子

3. 添加C++文件

现在我们需要在C++中实现TXC1简单模块的功能。从项目的上下文菜单中选择“新建”->“源文件”(或从IDE的主菜单中选择“文件”->“新建”->“文件”),创建一个名为txc1.cc的文件,并输入以下内容:

#include 
    #include 
    
    using namespace omnetpp;
    
    /**
     * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
     * both the `tic' and `toc' modules are Txc1 objects, created by OMNeT++
     * at the beginning of the simulation.
     */
    class Txc1 : public cSimpleModule
    {
      protected:
        // The following redefined virtual function holds the algorithm.
        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?
        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");
            send(msg, "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
    }

代码解释

这里从父类 cSimpleModule中重写(redefine)了两个方法 initialize()handleMessage().他们是由模拟器内核进行调用的,initailize()只会执行一次,而handleMessage()只要有消息到达就会执行一次

  • initalize(): 在这个方法中我们创建了一个消息对象(cMessage),并且将他发送到输出门,由于门于输入门相连接,模拟器内核会将这个信息作为参数传递给那个模块的handleMessage()方法(在100ms的传输延迟之后)
  • handleMessage(): 受到消息之后将同样的消息从输出门返回
  • cMessage对象: 消息Messages(包Packet,帧frames,工作jobs等)以及事件events(定时器timers,超时timeouts)都是由cMessage对象表示的。在你发送或调度他们之后,他们会被模拟器内核的"调度事件"或"未来事件"列表 (scheduled events or future events list)所拥有,直到需要他们被使用并且通过handleMessage()传送给需要的模块

注意:

  1. Txc1简单模块类型(simple module type)使用C++的对象类型(class) Txc1来表示
  2. Txc1必须继承自 cSimpleModule类,并且必须使用C++宏定义Define_Module(Txc1)来进行注册

忘记Define_Module()行是一个常见的错误,如果你忘记了添加,会出现诸如如下的错误"Error: Class 'Txc1' not found -- perhapsits code was not linked in, or the class wasn't registered with Register_Class(), or inthe case of modules and channels, with Define_Module()/Define_Channel()".

4. 添加omnetpp.ini配置文件

为了可以运行模拟器,我们需要建立omnetpp.ini文件,omnetpp.ini文件告诉模拟器程序你想要模拟哪一个网络(由于ned文件可以包含多个网络),还可以显示的指定随机数生成器的随机数种子

下面,我们建立一个omnetpp.ini文件,右键项目 New->Initialization file(INI).新文件会打开一个ini文件编辑器,与NED编辑器一样,这个ini编辑器也具有两种模式:``form和Source.前者更适合配置模拟器内核,后者更适合输入模块参数

现在我们切换到Source模式并且输入如下内容

[General]
netword = Tictoc1

注意大小写


Source.png

你可以在form模式中验证你在Source模式中所输入的内容


form.png

实例2tictoc2以及之后的内容会使用共同的omnetpp.ini文件,我们现在成功建立了第一个模型,并且准备编译运行它。

5. 运行模拟器

你可能感兴趣的:(omnet++仿真实例(一))