# Omnet++入門 ###### tags: `Omnet++` * What Is OMNeT++? OMNeT++ is an object-oriented modular discrete event network simulation framework. ## 安裝 * [OMNet++ download](https://omnetpp.org/download/) (我所用的版本 : OMNet++ 5.6.2) * 下載後解壓縮至你要的路徑 * 在 omnetpp-5.6.2/ 下可以找到 `mingwenv` 點開執行 ![](https://i.imgur.com/iUjWdGz.png) * 同個路徑下可找到`INSTALL.txt`,依照裡面指令快速安裝 ``` $ . setenv $ ./configure $ make ``` * 若想更改一些設定 (如:編譯器設定,在windows上建議改PREFER_CLANG=no) ``` $ notepad configure.user $ ./configure $ make ``` * 編譯完沒有問題使用 `omnetpp` 指令即可開啟IDE * 如果想知道整個資料夾每層在做什麼 : `omnetpp-5.6.2/README` * `omnetpp-5.6.2/sample/` 下放著一些內建好的專案,自己的專案也可放於此 * 進入IDE會先選擇你的workspace,接著左側會列出路徑下所有project,點兩下或右鍵"open project" 即可載入 project ![](https://i.imgur.com/8KEnUG8.png) * 對專案右鍵>properties>Project Reference 這裡可選擇要reference的project (有時會用到別的project的功能,如inet、queueinglib...),用別人的project丟進來也要注意這裡的設定是否不一致 ![](https://i.imgur.com/QkuRuCR.png) ## 模擬 [參考這篇](https://www.cnblogs.com/ShineLeBlog/category/1972607.html) 官方主要都是以`tictoc`這個範例project做教學,讀完它18篇範例可以比較熟練omnet中基本操作 本篇說明以簡潔為主,絕不詳細,還是得去讀文件比較好 (明天一定...) ### 觀念 * omnet++主要以C ++、NED等等**物件導向**語言來開發 * NED 全名為 NEtwork Description,其實就是用來描述網路的一種語言。 * NED 可以把它看成由很多個module來組成,最底層的module稱為simple module,simple module 又可以組成 compound module,在omnet++中定義的網路由眾多的module組成。 ![](https://i.imgur.com/5QgOztL.png) * 一個網路模擬可以大致分成三步 : * 構建網路(你的網路裡有哪些module) -- 編寫NED檔 * 定義網路行為(每個module的行為) -- 編寫C++(.cc檔) * 其餘模擬參數設定 ### NED檔分析 * NED檔有兩種方式撰寫,一種是用拉的(像C#),一種是source code(推薦) ```c= simple Satellite // 最基本的module { parameters: // 這裡可定義module的一些參數,這些參數只能在下面submodule定義中賦值, // 或在模擬設定檔(ini)中賦值 int state = default(1); @display("i=device/satellite"); // module 在網路中的圖示 // signal 與 statistic 一起使用 // 這個信號的用途就是在觸發時可以將指定的data(如這裡的hopCount)存下 // statistic後面參數 : source=來自的signal name; record= 記錄的資料型態 // 在對應的.cc 檔中要使用signal(自己命名) = registerSignal("signal名")註冊 // 並可使用 emit (signal, "要記錄的資料/變數值") @signal[arrival](type="long"); @statistic[hopCount](title="hop count";source="arrival";record=vector,stats;interpolationmode=none); gates: in g1; // 向內的gateway (這個gate msg只能進來 out g2; // 向外的gateway (這個gate msg只能出去 inout g3; // 雙向的gateway (這個gate msg可以進來跟出去 } simple Sat_clock // 同個 network 可以由不同module來組成 { parameters: @display("i=device/router"); gates: inout self; } network satnet { types: // 用來定義channel的type // omnet有三種定義好的channel可以繼承,ned.DatarateChannel為其中一種 channel interISL extends ned.DatarateChannel { delay = 0ms; datarate = 25.0Mbps; } channel intraISL extends ned.DatarateChannel { delay = 0ms; datarate = 25.0Mbps; } submodules: sat_clock: Sat_clock { // sat_clock 繼承 Sat_clock module屬性 @display("p=700,900"); } sat2_10: Satellite { // sat_2_10 繼承 Satellite module屬性 @display("p=250,770"); } sat2_11: Satellite { @display("p=200,820"); } connections allowunconnected: //inter-plane1 sat2_10.g3 <--> intraISL <--> sat2_11.g3; // (submodule名字).(gate名) <-->(雙箭頭表示inout) // (intraISL表示哪種channel type) } ``` ## C++ file 分析(.cc) * 每個NED檔都一個與之對應的.cc檔,在.cc中必須實現與NED中定義的module相對應的class * 注意.cc檔中的class名稱與NED檔中的對應module名稱必須相同!!! * `Define_module("module名稱")`就是在做綁定的動作 * 注意,這個例子在.cc這裡定義了每個屬於Satellite module的module會做什麼事情,表示全部人會做完這些定義的事件才繼續推動時間,所以在omnet++中這個時刻的event都發生完,時間才會繼續往下跑。 ```c= class Satellite : public cSimpleModule { private: simsignal_t arrivalSignal; // protected: // The following redefined virtual function holds the algorithm. // initialize() 為模擬開始前Satellite module一開始會做的動作 virtual void initialize() override; // 自己定義的fun 通常用來產生message virtual SatMsg *generateMessage(int type); // handleMessage 用來處理當有msg進來這個module時要做的行為 virtual void handleMessage(cMessage *msg) override; // forwardMessage 通常用來寫轉發的行為 virtual void forwardMessage(SatMsg *msg); // 模擬結束後會進的地方,通常拿來寫數據統計相關的行為 virtual void finish() override; 最後調用的fun }; class Sat_clock : public cSimpleModule { protected: // The following redefined virtual function holds the algorithm. virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; // virtual void finish() override; 最後調用的fun }; // The module class needs to be registered with OMNeT++ Define_Module(Satellite); Define_Module(Sat_clock); ... ``` * [omnet++ 常用的方法](https://www.cnblogs.com/ShineLeBlog/p/14761855.html) * [omnet++:cMessage、cSimpleModule、cGate](https://www.cnblogs.com/ShineLeBlog/p/14772988.html) * 再來就是去把Reference的[OMNet++ - tictoc中文教學](https://www.cnblogs.com/ShineLeBlog/category/1972607.html)都看過一遍(很簡單的),恭喜你~~畢業~~學會基本操作,下面幫你整理好每個單元的主題了。 ``` ---------------------- tictoc2 : 添加icon / 添加logging tictoc3 : 添加變數 (为Module添加一个計數器) tictoc4 : 添加Parameters(在NED文件中宣告 parameter、修改cc文件,在cc文件中接收这些para) tictoc5 : NED繼承 tictoc6 : 模擬節點產生延遲 (self-messages、scheduleAt()) tictoc7 : 隨機數 tictoc8 : 超時、計時器 tictoc9 : 重傳相同訊息 ---------------------- tictoc10 : 兩個節點以上的網路 tictoc11 : channel與內部型態定義 tictoc12 : 雙向連接 tictoc13 : message class (继承cMessage .msg) tictoc14 : 顯示收發包的數量 tictoc15 : 添加統計訊息 (finish()) tictoc16 : 不修改model完成數據統計(signal()) ``` ### Reference [OMNet++ - tictoc中文教學](https://www.cnblogs.com/ShineLeBlog/category/1972607.html) [OMNeT++ Documentation](https://omnetpp.org/documentation/) [OMNeT++教程(从入门到半精通)](https://zhuanlan.zhihu.com/p/565232631)