一些OMNET使用心得
一些菜雞學習心得,如果有錯的話希望大佬能幫忙指出,感激不盡!!
(底層組織結構是大佬幫忙寫的,感謝大佬帶入門)
- 項目組織
\prj
\prjname
\simulation
\results
package.ned
omnet.ini
network.ned
…
\src
package.ned
host.ned
host.cc
host.h
…
其中ned主要負責簡單模塊、復雜模塊、網絡等的組件與拓撲的描述。h文件來聲明,c文件來定義模塊的行為。
如簡單的tictoc項目:
- tictoc.ned
simple?Txc1
{
????gates:
????????input?in;
????????output?out;
}
- tictoc.h
class?Txc1?: public?cSimpleModule
{
??protected:
????// The following redefined virtual function holds the algorithm.
????virtual?void?initialize() override;
????virtual?void?handleMessage(cMessage?*msg) override;
};
- tictoc.cc
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
}
而ini文件則是進行網絡的配置,如:
[Tictoc1]
network?= Tictoc1
以上是最簡單的網絡配置,后續還可以通過在ned中定義一些參數然后在ini中修改它來配置需要的網絡。
復雜的網絡則會在src文件夾中定義簡單模塊與復雜模塊,然后在simulation文件夾中定義網絡的ned及ini文件。
- 新建項目
File->new->OMNET++ Project
?
填寫項目名稱:
?
->NEXT->選擇空項目/帶src和simulation文件夾的項目。網絡上的簡易教程多為空項目,但帶倆文件夾的項目后續會更適合復雜network的組織。
?
一般情況下,simulation文件夾放網絡模塊文件及ini配置,src文件夾放組織網絡的簡單及復雜模塊(如node、host等)
三、簡單模塊與復雜模塊的介紹及拼裝網絡
這部分以簡易自組織網絡為例介紹簡單模塊與復雜模塊的ned部分。
簡單的maclayer.ned:
package?tdma_mac_demo;
//此處定義文件位置,處于tdma_mac_demo/src/maclayer.ned
//若需要在tdma_mac_demo/文件夾下但/src以外的地方(如simulation)引用它
//則需要import tdma_mac_demo/maclayer.ned
simple?MacLayer
{
????parameters:
????????@display("i=block/routing");
????????int?numSlots = default(10);
????????double?slotDuration @unit(s) = default(500ms);
????????string?slotAllocate = default("");
????gates:
????????input?upperIn;
????????output?upperOut;
????????input?phyIn;
????????output?phyOut;
}
簡單來說,簡單模塊都是這個樣子:
package?……;
simple?……
{
????parameters:
????????……
????gates:
????????input?……;
????????output?……;
}
而用簡單模塊的復雜模塊則長這個樣子:
package?……;
simple?Node
{
????parameters:
????????……
????gates:
????????input?……;
????????output?……;
}
submodules:
????????trafficGen: TrafficGen {
????????????……
????????}
????????macLayer: MacLayer {
????????????……
????????}
????????phyLayer: PhyLayer {
????????????……
????????}
connections:
trafficGen.lowerOut -->?macLayer.upperIn;
……
}
再把復雜模塊node拼裝成一個網絡network:
package?tdma_mac_demo.simulations;
import?tdma_mac_demo.DemoNode;
network?DemoNetwork
{
????
????parameters:
????????@display("bgb=500,500");
????????int?nodenum = default(5);
????types:
????????channel?Channel extends?ned.DatarateChannel
????????{
????????????datarate = 100Mbps;
????????????delay = default(100ms);
????????}
????submodules:
????????node[nodenum]: DemoNode;
????connections:
????????for i=0..sizeof(node[0].radioIn) , for j = 0..sizeof(node[0].radioIn){
????????node[i].radioOut++ --> Channel --> node[j].radioIn++ if i!=j ;}
????????
}
很顯然,parameters指參數,default()為默認,默認后就可以在ini快樂的更改它了~
gates指門(用來連接);submodule為子模塊;connections指連接,用-->或者<--來表示。Channel為信道,可以直接繼承(extends)omnet庫中的信道類型,直接在types中定義一些參數的值。
注:
在parameters中可以用@class屬性明確指定C++類,若未指定則默認當前文件夾的下的同名文件。
在ned中定義的參數,可以直接賦值,也可以設為默認后在ini文件中配置它。而在ini文件中賦值并不能覆蓋ned中的賦值。參數還有一些屬性,如@mutable、@unit、@derectIn等。
更具體可以參照《OMNET++與網絡仿真 趙永利 張杰 著》及其他教程。
四、簡單模塊行為描述
首先舉一個例子,下述是Txc1.h文件。和普通的C++類一樣的聲明,但繼承簡單模塊cSimpleModule,initialize()、handleMessage()也是必須的——須在.cc文件中進行初始化及處理信息的描述。
class?Txc1?: public?cSimpleModule
{
??protected:
????// The following redefined virtual function holds the algorithm.
????virtual?void?initialize() override;
????virtual?void?handleMessage(cMessage?*msg) override;
};
然后看Txc1.cc文件:
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
}
在仿真時會自動運行上述兩個函數,也可以加入其他函數的聲明及定義,但需要要initialize()和handleMessage()來call它們。
initialize()和handleMessage()都如其名,進行初始化和處理消息,初始化函數則是在仿真進行的開始對模塊進行初始化,可以將ned中的參數導入,初始化變量等。處理消息函數則是在消息來到時自行調用該函數對msg進行處理,這個msg包括外來消息及自消息。
這個自消息則是一個新概念,比如modula A可以自己給自己發消息,用scheduleAfter ( delaytime, msg),在delaytime后發送msg給自己,就像定鬧鐘,到時間了告訴自己該起床、開會等等。
寫不動了……
總結
以上是生活随笔為你收集整理的一些OMNET使用心得的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux指令:tar打包与压缩
- 下一篇: Educational Codeforc