omnet++ 第一个工程的创建

mode :

for a start ,we begin with a “network” that consists of two nodes . The nodes will do something simple:one of the notes will create a packet .and two nodes will keep passing the same packet back and forth.

step

here are the steps you take to implement your first simulation from scratch.

setting up a project

1、 start your omnet++ IDE by typing omnetpp in your terminal.

2、 once in the IDE,choose File-New-OMNeT++Project

在这里插入图片描述

3、one wizard dialog will appear.

Enter mtictoc as project name
在这里插入图片描述

4、 choose Empty project when asked about the initial contents of the project,and the click Finish.

在这里插入图片描述
An empty project will be created,as(如) you can see in the Project Explorer .
在这里插入图片描述
(note:some omnet++ version will generate(生成) a packet.ned file into the project. We don’t need it now: delete the file by selecting it and click Delete.)
the project we created will hold all files that belong to(属于) our simulation.
在这里插入图片描述

5、 Adding the NED file.

OMNet++ uses NED files to define components(定义组件) and to assemble(聚集,组装) them into larger units like network.

We start implementing(实施。执行) our mode by adding a NED file.
right-click the project directory in the Project Explorer ,and choose New-Network Description File (NED) from the menu.
在这里插入图片描述
Enter mtictoc1.net as file name.
在这里插入图片描述在这里插入图片描述
Once created,the file can be edited in the Editor area which has two modes:Design and Source .
在这里插入图片描述
one can switch between them using the tabs at the bottom of the editor.
In Design mode, the topology can be edited graphically, using the mouse and the palette on the right.
In Source mode, the NED source code can be directly edited as text.
Changes done in one mode will be immediately reflected in the other, so you can freely switch between modes during editing, and do each change in whichever mode it is more convenient.

6、 Switch Source mode and enter the following:

//
// This file is part of an OMNeT++/OMNEST simulation example.
//
// Copyright (C) 2003 Ahmet Sekercioglu
// Copyright (C) 2003-2015 Andras Varga
//
// This file is distributed WITHOUT ANY WARRANTY. See the file
// `license' for details on this and other legal matters.
//


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;
}

在这里插入图片描述
When you’ve done ,switch back to Design mode.You should see something like this:
在这里插入图片描述
在这里插入图片描述
The first block in the file declares Txc1 as a simple module type.
The declaration of Txc1 says that
it has an input gate named in,
and an output gate named out.
(Simple modules are atomic on NED level. Simple modules are active components,and their behavior is implemented in C++.)

在这里插入图片描述
The second block declares Tictoc1 as a network.
Tictoc1 is assembled from two submodules, tic and toc, both instances of the module type Txc1.
tic’s output gate is connected to toc’s input gate, and vica versa(反之亦然).
There will be a 100ms propagation delay both ways.
在这里插入图片描述

7、Adding the C++ files.

We now need to implement the functionality of the Txc1 simple module in C++.
Create a file named txc.cc
Choosing New-Source File form the project context menu(项目的上下文菜单).
在这里插入图片描述
and enter the following:

#include <string.h>
#include <omnetpp.h>

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
}

在这里插入图片描述
The Txc1 simple module type is represented by the C++ class Txc1.(TXC1简单模块类型由C++类TXC1表示。) The Txc1 class needs to subclass from OMNeT++'s cSimpleModule class, and needs to be registered in OMNeT++ with the Define_Module() macro.
We redefine two methods from cSimpleModule: initialize() and handleMessage().
They are invoked from the simulation kernel:
initialize() ** only once,** (只在初始化调用一次)
handleMessage() whenever a message arrives at the module.(消息到达时调用)

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");
    }

In initialize() we create a message object (cMessage), and send it out on gate out.
由于此门连接到另一个模块的输入门,因此模拟内核将在handleMessage()的参数中将此消息传递给另一个模块。

8、Adding omnetpp.ini

To be able to run the simulation ,we need to create an omnetpp.ini file.
omnetpp.ini tells the simulation program which network you want to simulate(告诉 模拟器 你想模拟哪个 网络 )(as NED files may contain several networks),
you can pass parameters to the model, explicitly specify seeds for the random number generators, etc.(可以将参数传递给模型,显式指定随机数生成器的种子等。???)

Create an omnetpp.ini file using the File -> New -> Initialization file (INI) menu item.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
For now, just switch to Source mode and enter the following:

[General]
network = Tictoc1

You can verify the result in Form mode:
在这里插入图片描述
tictoc2 and further steps will all share a common omnetpp.ini file.
We are now done with creating the first model, and ready to compile and run it.

9、Launching the simulation pragram

you can launch the simulation by selecting omnetpp.ini , and pressing the Run button.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

10、 Running the simulation

After successfully building and launching your simulation, you should see a new GUI window appear, similar to the one in the screenshot below. You should also see the network containing tic and toc displayed graphically in the main area.
在这里插入图片描述
Press the Run button. 在这里插入图片描述
What you should see is that tic and toc are exchanging messages with each other.
在这里插入图片描述

已标记关键词 清除标记
相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案例,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页