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.
在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值