本文將以自己的一個實例來進行詳解:
我的ns-3版本爲 ns-3-dev
/home/repos/ns-3-allinone/ns-3-dev/
本文將把一個高速公路車聯自組網的模型添加到ns-3中。
一、編寫代碼
這一步,就要是編寫相關的代碼
本文代碼見: http://download.csdn.net/detail/barcodegun/3710431
二、把自己編寫的模塊加入ns-3中
1. 文件結構
本文是一個高速公路車聯自組網的模型,vanet
a. 在repos/ns-3-allinone/ns-3-dev/src目錄下,添加新的模塊
ns-3的模塊,都在src目錄下。自己創建的模塊,也複製到此目錄下。
我在 src 目錄下新建 vanet 文件夾
gpf@guopengfei:~/repos/ns-3-allinone/ns-3-dev/src$ mkdir vanet
vanet 目錄下文件結構
src/
vanet/
examples/
vanet-highway-test.cc
wscript
model/
controller.cc
controller.h
...
vehicle.cc
vehicle.h
test/
examples-to-run.py
vanet-temp-test.cc
wscript
文件初步說明:
examples主要是作者編寫的該模塊的相關例子。用戶的實際例子,不需要放在這裏,可以放在scratch目錄下編譯及執行。
model 主要爲作者編寫的該模塊的核心代碼。
test 主要爲作者在編寫代碼過程中,編寫的相關測試程序。
2.修改src/vanet/wscript文件
a. 把 src/spectrum/wscript 這個文件複製到 vanet目錄下。
複製一個模版程序,直接修改該程序。
b. 我的src/vanet/wscript文件:
def build(bld):
obj = bld.create_ns3_module('vanet', ['network', 'propagation', 'core', 'wifi', 'mobility'])
obj.source = [
'model/controller.cc',
'model/highway.cc',
'model/lane-change.cc',
'model/model.cc',
'model/obstacle.cc',
'model/vehicle.cc',
]
obj_test = bld.create_ns3_module_test_library('vanet')
obj_test.source = [
'test/vanet-temp-test.cc',
]
headers = bld.new_task_gen('ns3header')
headers.module = 'vanet'
headers.source = [
'model/controller.h',
'model/highway.h',
'model/lane-change.h',
'model/model.h',
'model/obstacle.h',
'model/vehicle.h',
]
if (bld.env['ENABLE_EXAMPLES']):
bld.add_subdirs('examples')
c. 相關語法說明:
obj = bld.create_ns3_module('vanet', ['network', 'propagation', 'core', 'wifi', 'mobility'])
// vanet爲創建的組件名, 最後文檔生成後,將在repos/ns-3-allinone/ns-3-dev/build/debug/ns3/ 目錄下,生成一個 vanet-module.h的文件
以後用戶自己編寫程序, #include "ns3/vanet-module.h" 就可以直接調用用戶編寫的這個模塊了。
後面的network,propagation ... 是該模塊在代碼編寫過程中,需要信賴的相關組件。
obj.source 表示該模塊的源代碼
obj_test 爲該模塊的測試相關文檔
headers = bld.new_task_gen('ns3header') 是固定格式,用戶不用改變。
headers.module = 'vanet'
headers.source = ... 用戶對 .h頭文件進行設置.
3.修改 src/vanet/examples/wscript
同上,先把src/spectrum/examples/wscript 這個文件複製到 vanet/examples目錄下,再修改該文件,
我的文件:
def build(bld):
if not bld.env['ENABLE_EXAMPLES']:
return;
obj = bld.create_ns3_program('vanet-highway-test',
['core', 'mobility', 'network', 'wifi', 'vanet'])
obj.source = 'vanet-highway-test.cc'
4.修改 src/vanet/test/examples-to-run.py
同上,先把 src/spectrum/test/examples-to-run.py 複製到 vanet/test目錄下,再修改該文件,
我的文件:
...
cpp_examples = [
("vanet-highway-test", "True", "True"),
]
# A list of Python examples to run in order to ensure that they remain
# runnable over time. Each tuple in the list contains
#
# (example_name, do_run).
#
# See test.py for more information.
python_examples = []
5. 在 src/wscript文件中添加該組件
gpf@guopengfei:~/repos/ns-3-allinone/ns-3-dev/src$ vim wscript
all_modules = [
'core',
'network',
'config-store',
'internet',
'propagation',
'point-to-point',
...
'tools',
'visualizer',
'point-to-point-layout',
'csma-layout',
'template',
'vanet',
]
注意上面最後一行 'vanet', 是我添加的該組件。
三、編譯,完成最後的添加:
./waf configure --enable-examples --enable-tests
./waf build
顯示結果:
Modules built:
aodv applications bridge
config-store core csma
csma-layout dsdv emu
energy flow-monitor internet
lte mesh mobility
mpi netanim network
nix-vector-routing ns3tcp ns3wifi
olsr point-to-point point-to-point-layout
propagation spectrum stats
tap-bridge template test
tools topology-read uan
vanet virtual-net-device visualizer
wifi wimax
注意上面的vanet ,表示此模塊已經成功添加。
執行完成後,用戶編寫代碼時:
#include "ns3/vanet-module.h"
即包含此模塊。
vanet-module.h 文件在 repos/ns-3-allinone/ns-3-dev/build/debug/ns3/目錄下,如果用戶的組件爲 new-template,則生成的組件名爲 new-template-module.h,使用時引用該組件頭文件即可。