如果需要將自己開發的元件加入NS2應該怎麼做? 這裡將程序RUN一次,並將中間使用到的檔案稍微說明。首先將需要的檔案ex-linkage.cc下載下來使用另一個視窗開啟,在以下的程序可以很快的參考到。
NS2的檔案架構如下圖

依照這個架構,新寫好的C程式只要放置在ns-2的目錄下即可。在我們的課程裡是ns-2.29 (or ns-2.33)。當然如果為了便於管理你可以開一個子目錄放置自己開發的程式碼。
另外有幾個檔案是比較重要的
- ns-lib.tcl: The simulator class and most of its member function definitions except for LAN, Web, and Multicast related ones are located here. If you want to know which member functions of the Simulator object class are available and how they work, this is a place to look.
- ns-default.tcl: The default values for configurable parameters for various network components are located here. Since most of network components are implemented in C++, the configurable parameters are actually C++ variables made available to OTcl via an OTcl linkage function, bind(C++_variable_name, OTcl_variable_name). How to make OTcl linkages from C++ code is described in the next section.
- ns-packet.tcl: The packet header format initialization implementation is located here. When you create a new packet header, you should register the header in this file to make the packet header initialization process to include your header into the header stack format and give you the offset of your header in the stack. A new header creating example is shown in "Add New Application and Agent" section.
- other OTcl files: Other OTcl files in this directory, contain OTcl implementation of compound network objects or the front end (control part) of network objects in C++. The FTP application is entirely implemented in OTcl and the source code is located in "ns-source.tcl".
我當然知道英文是有看沒有懂,簡單用中文說一下,在這個範例裡面我們只用到ns-default.tcl,它放在ns/tlc/lib目錄下,它是一個tcl檔,所以裡面用的是otcl的語法,C語言不通。這個檔案放的是你用C語言開發出來的程式的一些變數的初值。基本上每一個變數都需要初值的設定,如果沒有設定在執行ns的時候就會有warning message出現,不過如果你的tcl檔案中在使用變數以前有指定數值那就可以不要管這個warning message。最好每一個變數都在ns-default.tcl中定義初值。另外和Simulator相關的初值設定放在ns-lib.tcl這個檔案裡。對於自己開發的程式,放在哪裡沒有影響。
ns-default.tcl中定義的是otcl的變數初值,我們開發的C語言所使用的變數名稱須要經過link的程序連結到otcl的變數,這點等下會提到。
C物件的開發需要遵循NS2的物件架構,所以在開發的時候先判斷一下所開發的物件應該是繼承哪一個NS2類別。這個範例我們開發一個新的agent類別稱為MyAgent,繼承自Agent類別。
MyAgent類別中宣告了兩個變數(my_var1, my_var2)和一個函式(MyPrivFunc)。另外MyAgentClass是用來當作鏈結類別,連結C類別MyAgent到Otcl類別Agent/MyAgentOtcl。Agent/MyAgentOtcl前面的Agent/指明MyAgentOtcl是Agent下的一個衍生類別。當ns開始執行的時候會建立 class_my_agent物件,此時Agent/MyAgentOtcl類別和其相關的函式就會被建立在OTcl空間中。上面所提到的鏈結類別(static class開頭那段)可以拷貝到你開發的程式碼當中,只要將相對應的類別名稱(有顏色的部分,以及class_my_agent)做修改即可。
變數的連結:
需要在建構子當中將變數的繫結做好。NS2提供四種繫結的方式
- bind(): real or integer variables
- bind_time(): time variable
- bind_bw(): bandwidth variable
- bind_bool(): boolean variable
變數的初值可以設定在/tcl/lib/ns-default.tcl中。
函數的繫結:OTcl command interpreter
NS2利用command這個函式呼叫來處理C和OTcl間的函式繫結,基本上拷貝後做適當修改貼上即可。上面的程式將C語言中的MyPrivFunc繫結到Otcl上的call-my-priv-func函式。在.tcl程式中使用的時候看起來就像
set myagent [new Agent/MyAgentOtcl]
$myagent call-my-priv-func #這個陳述式會呼叫MyAgent的MyPrivFunc()
Compile, run and test "MyAgent"
- Download "ex-linkage.cc" file, and save it under the "ns-2" directory.
- Open "Makefile", add "ex-linkage.o" at the end of object file list.
- Re-compile NS using the "make" command.
- Download the "ex-linkage.tcl" file that contains "MyAgent" testing OTcl commands. (see Figure 22 for the input script and the result)
- Run the OTcl script using command "ns ex-linkage.tcl".
第二個步驟指示在做連結(link or bind)的時候要加入剛剛開發完成的C程式語言的.obj檔。"Makefile"這個檔案放在ns目錄下(ns-2.29 or ns-2.33),修改完成後執行make重新編譯與連結NS2的核心。.obj檔放置的位置在Makefile檔案中OBJ_CC = \和$(OBJ_STL)陳述句中間。
上面的warning message是因為變數的初值沒有事先設定,你可以嘗試設定看看。
另外的例子可以看一下課本實驗四,在實驗中作者加入了mTcpSink和mUDP,mUdpSink等三個類別,光是只學會這裡講的新增Agent還不足以完成實驗四,不過可以先追蹤mTcpSink和mUdpSink的程式撰寫方法,程式碼稍微的追蹤一下應該可以有很大的收穫。
(還有NS2的擴充方法(二)(三),未完待續)


沒有留言:
張貼留言