• 向NS-3添加新模块_ns3.37添加新模块_ns3.37不同版本模块移植


     使用ns3的时候,我们需要调用很多模块,比如对wifi的简单功能进行仿真时:

    ns-3.35_third.cc_ns-3网络仿真工具wifi脚本解析_wifi脚本网络拓扑_ns-3third脚本全注释_基础ns-3_ns-3入门_ns-3third脚本解析_Part1_Mr_liu_666的博客-CSDN博客IntroBG:ns-3网络仿真工具是一个用于搭建网络拓扑,测试网络算法、模拟不同环境的网络性能的一套软件,这套软件由多个小模块构成(wifi ipv4之类的模块),运行时调用哪个就编译链接哪个。(这一段是我的理解)NS2(Network Simulator, version 2)是一种面向对象的网络仿真器,本质上是一个离散事件模拟器。由UC Berkeley开发而成。它本身有一个虚拟时钟,所有的仿真都由离散事件驱动...https://blog.csdn.net/Mr_liu_666/article/details/121625416?spm=1001.2014.3001.5501
    ns-3.35_third.cc_ns-3网络仿真工具wifi脚本解析_wifi脚本网络拓扑_ns-3third脚本全注释_基础ns-3_ns-3入门_ns-3third脚本解析_Part2_Mr_liu_666的博客-CSDN博客ns-3全注释系列,上篇见:https://blog.csdn.net/Mr_liu_666/article/details/121625416?spm=1001.2014.3001.5501https://blog.csdn.net/Mr_liu_666/article/details/122203797?spm=1001.2014.3001.5501

    我们就用到了mobility、PointToPoint、csma、wifimac、wifiphty等等这些模块,那么当我需要仿真使用一个常用但ns3里面没有的模块的时候,我就需要新生成一个模块,或者把其他人写好的模块移植到当前环境——那么如果ns3版本不同的话,我们还是需要一些类似于新生成ns3模块的操作。

    第一步 模块 layout

    module layout,模块布局,也就是新建一个module你的装module的文件夹应该是啥格式的,首先,module都在src目录下,以spectrum模块为例,基本的目录结构如下:

    1. src/
    2. module-name/
    3. bindings/
    4. doc/
    5. examples/
    6. CMakeLists.txt
    7. helper/
    8. model/
    9. test/
    10. examples-to-run.py
    11. CMakeLists.txt

    第二步 创建模块skeleton

     以上的目录可以手动创建,也可以自动创建,假设您新建模块的名字为new-module,在ns3.37目录下的utils目录下有自动化脚本可用

    ./utils/create-module.py new-module

    默认情况下,新模块建立在Contrib中(原文档说会建立在src中,但是实际上建立在Contrib中):

    两个Cmakelist.txt里面放的是源文件目录,同时指定需要调用哪些其他模块(类似于在Makefile 里面指定需要链接哪些库)。调用模块在new-module/Cmakelist.txt里定义,不在example下定义。

     创建初始状态如上图所示,可以看到两个源文件、两个头文件、一个要链接的libcore库和一个测试源文件,如果加一些别的库就在LIBRARIES_TO_LINK里面加:

    1. build_lib(
    2. LIBNAME new-module
    3. SOURCE_FILES helper/new-module-helper.cc
    4. model/new-module.cc
    5. HEADER_FILES helper/new-module-helper.h
    6. model/new-module.h
    7. LIBRARIES_TO_LINK
    8. ${libinternet}
    9. ${libmobility}
    10. ${libaodv}
    11. TEST_SOURCES test/new-module-test-suite.cc
    12. )

     由于internet model依赖于core库,所以不用重复引用。

    第三步 添加源文件列表

    在new-module/Cmakelist.txt里面指定了new-module.cc new-module.h 等5个文件,运行ns configure之后就会进一步调用cmake,然后检查这些文件,再运行ns3 build,就编译Cmakelist.txt里面的源文件,再链接上指明的库,以spectrum模块为例子:

    1. set(source_files
    2. helper/adhoc-aloha-noack-ideal-phy-helper.cc
    3. helper/spectrum-analyzer-helper.cc
    4. helper/spectrum-helper.cc
    5. helper/tv-spectrum-transmitter-helper.cc
    6. helper/waveform-generator-helper.cc
    7. model/aloha-noack-mac-header.cc
    8. model/aloha-noack-net-device.cc
    9. model/constant-spectrum-propagation-loss.cc
    10. model/friis-spectrum-propagation-loss.cc
    11. model/half-duplex-ideal-phy-signal-parameters.cc
    12. model/half-duplex-ideal-phy.cc
    13. model/matrix-based-channel-model.cc
    14. model/microwave-oven-spectrum-value-helper.cc
    15. model/multi-model-spectrum-channel.cc
    16. model/non-communicating-net-device.cc
    17. model/single-model-spectrum-channel.cc
    18. model/spectrum-analyzer.cc
    19. model/spectrum-channel.cc
    20. model/spectrum-converter.cc
    21. model/spectrum-error-model.cc
    22. model/spectrum-interference.cc
    23. model/spectrum-model-300kHz-300GHz-log.cc
    24. model/spectrum-model-ism2400MHz-res1MHz.cc
    25. model/spectrum-model.cc
    26. model/spectrum-phy.cc
    27. model/spectrum-propagation-loss-model.cc
    28. model/phased-array-spectrum-propagation-loss-model.cc
    29. model/spectrum-signal-parameters.cc
    30. model/spectrum-value.cc
    31. model/three-gpp-channel-model.cc
    32. model/three-gpp-spectrum-propagation-loss-model.cc
    33. model/trace-fading-loss-model.cc
    34. model/tv-spectrum-transmitter.cc
    35. model/waveform-generator.cc
    36. model/wifi-spectrum-value-helper.cc
    37. )
    38. set(header_files
    39. helper/adhoc-aloha-noack-ideal-phy-helper.h
    40. helper/spectrum-analyzer-helper.h
    41. helper/spectrum-helper.h
    42. helper/tv-spectrum-transmitter-helper.h
    43. helper/waveform-generator-helper.h
    44. model/aloha-noack-mac-header.h
    45. model/aloha-noack-net-device.h
    46. model/constant-spectrum-propagation-loss.h
    47. model/friis-spectrum-propagation-loss.h
    48. model/half-duplex-ideal-phy-signal-parameters.h
    49. model/half-duplex-ideal-phy.h
    50. model/matrix-based-channel-model.h
    51. model/microwave-oven-spectrum-value-helper.h
    52. model/multi-model-spectrum-channel.h
    53. model/non-communicating-net-device.h
    54. model/single-model-spectrum-channel.h
    55. model/spectrum-analyzer.h
    56. model/spectrum-channel.h
    57. model/spectrum-converter.h
    58. model/spectrum-error-model.h
    59. model/spectrum-interference.h
    60. model/spectrum-model-300kHz-300GHz-log.h
    61. model/spectrum-model-ism2400MHz-res1MHz.h
    62. model/spectrum-model.h
    63. model/spectrum-phy.h
    64. model/spectrum-propagation-loss-model.h
    65. model/phased-array-spectrum-propagation-loss-model.h
    66. model/spectrum-signal-parameters.h
    67. model/spectrum-value.h
    68. model/three-gpp-channel-model.h
    69. model/three-gpp-spectrum-propagation-loss-model.h
    70. model/trace-fading-loss-model.h
    71. model/tv-spectrum-transmitter.h
    72. model/waveform-generator.h
    73. model/wifi-spectrum-value-helper.h
    74. test/spectrum-test.h
    75. )
    76. build_lib(
    77. LIBNAME spectrum
    78. SOURCE_FILES ${source_files}
    79. HEADER_FILES ${header_files}
    80. LIBRARIES_TO_LINK ${libpropagation}
    81. ${libantenna}
    82. TEST_SOURCES
    83. test/spectrum-ideal-phy-test.cc
    84. test/spectrum-interference-test.cc
    85. test/spectrum-value-test.cc
    86. test/spectrum-waveform-generator-test.cc
    87. test/three-gpp-channel-test-suite.cc
    88. test/tv-helper-distribution-test.cc
    89. test/tv-spectrum-transmitter-test.cc
    90. )

     可见spectrum模块也是一样。可以看到第二步和第三步的Cmakelist.txt格式不一样,两种都是可以的,比较长久像spectrum一样,短一点就像new-module一样就行

    另外,source_files和header_files可以空着,也能编译,但是别的模块就调用不了newmodule的API了。

    第四步 声明公共header文件

     如果在Cmakelist.txt里面指出了header文件,那么就会在build/include/ns3给你复制一个副本,其他模块include的时候就include "ns3/xxx.h"就行了。如果不想被外部调用,那就不放在列表里就好,这样就不会被复制了。

    第五步 声明测试文件

    如果module中含test,也需要在Cmakelist.txt里面指出,依旧以spectrum为例子(在上面代码里面已经粘贴过一次了,这里再粘贴一次):

    1. build_lib(
    2. LIBNAME spectrum
    3. SOURCE_FILES ${source_files}
    4. HEADER_FILES ${header_files}
    5. LIBRARIES_TO_LINK ${libpropagation}
    6. ${libantenna}
    7. TEST_SOURCES
    8. test/spectrum-ideal-phy-test.cc
    9. test/spectrum-interference-test.cc
    10. test/spectrum-value-test.cc
    11. test/spectrum-waveform-generator-test.cc
    12. test/three-gpp-channel-test-suite.cc
    13. test/tv-helper-distribution-test.cc
    14. test/tv-spectrum-transmitter-test.cc
    15. )

    加了test之后可以用test.py --list查找到,

     也可以用test.py执行测试:

     第六步 声明example

     比如third first这类的都属于example,声明example需要源文件并且把它在examples /cmakelists.t xt 里面声明一下。examples下的cmakelists.txt会被上层的cmakelists.txt递归调用

     如果example需要链接外部module,也需要在example/cmakelists.txt里面指出:

    第七步 将example 作为 test执行

    需要在test的examples-to-run.py里面添加需要测试的example的源码文件名,依然以spectrum为例:

     example_name是要运行的可执行文件

    do_run是运行示例的条件

    do_valgrind_run是在valgrind下运行示例的条件。

    第八步 configure 和 build

    新生成的cmakelist.txt需要经过ns3 configure之后才能被读取、生成相应Makefile。有了Makefile之后运行ns3 biuld就可以把心添加的模块编译了。如果configure 的时候使能了test,那么test.py --list就可以看到新模块了,如果configure使能了example,那么ns3 run 就能直接运行声明的example了。

    第九步 添加Python Bindings

    如果想用python的方式调用,就需要这个了,由于我使用c++,以上内容就够了。

  • 相关阅读:
    Java版 招投标系统简介 招投标系统源码 java招投标系统 招投标系统功能设计
    计算机网络八股文
    前端DOM操作精解:基础概念、方法与最佳实践
    vue3后台管理系统之路由守卫
    Vue2.x - Vue Router
    Android 喝水项目总结(10.18)
    如何保护 LDAP 目录服务中的用户安全?
    SpringMVC(JSR303和拦截器)
    2023年11月12日阿里云产品全面故障的启示
    环境生态学知识点
  • 原文地址:https://blog.csdn.net/Mr_liu_666/article/details/128054390