一些OMNET使用心得
一些菜雞學(xué)習(xí)心得,如果有錯(cuò)的話希望大佬能幫忙指出,感激不盡!!
(底層組織結(jié)構(gòu)是大佬幫忙寫的,感謝大佬帶入門)
- 項(xiàng)目組織
\prj
\prjname
\simulation
\results
package.ned
omnet.ini
network.ned
…
\src
package.ned
host.ned
host.cc
host.h
…
其中ned主要負(fù)責(zé)簡(jiǎn)單模塊、復(fù)雜模塊、網(wǎng)絡(luò)等的組件與拓?fù)涞拿枋觥文件來聲明,c文件來定義模塊的行為。
如簡(jiǎn)單的tictoc項(xiàng)目:
- 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文件則是進(jìn)行網(wǎng)絡(luò)的配置,如:
[Tictoc1]
network?= Tictoc1
以上是最簡(jiǎn)單的網(wǎng)絡(luò)配置,后續(xù)還可以通過在ned中定義一些參數(shù)然后在ini中修改它來配置需要的網(wǎng)絡(luò)。
復(fù)雜的網(wǎng)絡(luò)則會(huì)在src文件夾中定義簡(jiǎn)單模塊與復(fù)雜模塊,然后在simulation文件夾中定義網(wǎng)絡(luò)的ned及ini文件。
- 新建項(xiàng)目
File->new->OMNET++ Project
?
填寫項(xiàng)目名稱:
?
->NEXT->選擇空項(xiàng)目/帶src和simulation文件夾的項(xiàng)目。網(wǎng)絡(luò)上的簡(jiǎn)易教程多為空項(xiàng)目,但帶倆文件夾的項(xiàng)目后續(xù)會(huì)更適合復(fù)雜network的組織。
?
一般情況下,simulation文件夾放網(wǎng)絡(luò)模塊文件及ini配置,src文件夾放組織網(wǎng)絡(luò)的簡(jiǎn)單及復(fù)雜模塊(如node、host等)
三、簡(jiǎn)單模塊與復(fù)雜模塊的介紹及拼裝網(wǎng)絡(luò)
這部分以簡(jiǎn)易自組織網(wǎng)絡(luò)為例介紹簡(jiǎn)單模塊與復(fù)雜模塊的ned部分。
簡(jiǎn)單的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;
}
簡(jiǎn)單來說,簡(jiǎn)單模塊都是這個(gè)樣子:
package?……;
simple?……
{
????parameters:
????????……
????gates:
????????input?……;
????????output?……;
}
而用簡(jiǎn)單模塊的復(fù)雜模塊則長(zhǎng)這個(gè)樣子:
package?……;
simple?Node
{
????parameters:
????????……
????gates:
????????input?……;
????????output?……;
}
submodules:
????????trafficGen: TrafficGen {
????????????……
????????}
????????macLayer: MacLayer {
????????????……
????????}
????????phyLayer: PhyLayer {
????????????……
????????}
connections:
trafficGen.lowerOut -->?macLayer.upperIn;
……
}
再把復(fù)雜模塊node拼裝成一個(gè)網(wǎng)絡(luò)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指參數(shù),default()為默認(rèn),默認(rèn)后就可以在ini快樂的更改它了~
gates指門(用來連接);submodule為子模塊;connections指連接,用-->或者<--來表示。Channel為信道,可以直接繼承(extends)omnet庫中的信道類型,直接在types中定義一些參數(shù)的值。
注:
在parameters中可以用@class屬性明確指定C++類,若未指定則默認(rèn)當(dāng)前文件夾的下的同名文件。
在ned中定義的參數(shù),可以直接賦值,也可以設(shè)為默認(rèn)后在ini文件中配置它。而在ini文件中賦值并不能覆蓋ned中的賦值。參數(shù)還有一些屬性,如@mutable、@unit、@derectIn等。
更具體可以參照《OMNET++與網(wǎng)絡(luò)仿真 趙永利 張杰 著》及其他教程。
四、簡(jiǎn)單模塊行為描述
首先舉一個(gè)例子,下述是Txc1.h文件。和普通的C++類一樣的聲明,但繼承簡(jiǎn)單模塊cSimpleModule,initialize()、handleMessage()也是必須的——須在.cc文件中進(jìn)行初始化及處理信息的描述。
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
}
在仿真時(shí)會(huì)自動(dòng)運(yùn)行上述兩個(gè)函數(shù),也可以加入其他函數(shù)的聲明及定義,但需要要initialize()和handleMessage()來call它們。
initialize()和handleMessage()都如其名,進(jìn)行初始化和處理消息,初始化函數(shù)則是在仿真進(jìn)行的開始對(duì)模塊進(jìn)行初始化,可以將ned中的參數(shù)導(dǎo)入,初始化變量等。處理消息函數(shù)則是在消息來到時(shí)自行調(diào)用該函數(shù)對(duì)msg進(jìn)行處理,這個(gè)msg包括外來消息及自消息。
這個(gè)自消息則是一個(gè)新概念,比如modula A可以自己給自己發(fā)消息,用scheduleAfter ( delaytime, msg),在delaytime后發(fā)送msg給自己,就像定鬧鐘,到時(shí)間了告訴自己該起床、開會(huì)等等。
寫不動(dòng)了……
總結(jié)
以上是生活随笔為你收集整理的一些OMNET使用心得的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux指令:tar打包与压缩
- 下一篇: nlp-with-transformer