文章目录
- mode :
- step
- 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.
- 4、 choose **Empty project** when asked about the initial contents of the project,and the click **Finish**.
- 5、 Adding the NED file.
- 6、 Switch Source mode and enter the following:
- 7、Adding the C++ files.
- 8、Adding omnetpp.ini
- 9、Launching the simulation pragram
- 10、 Running the simulation
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.