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

本文详细介绍了在ns-3中创建新模块的步骤,包括模块布局、创建模块骨架、添加源文件列表、声明公共头文件、测试文件、示例以及构建过程。通过这些步骤,你可以为ns-3添加自定义功能,实现特定的网络仿真实验。此外,还提到了如何使用Python绑定,尽管本文主要关注C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

src/
    module-name/
    bindings/
    doc/
    examples/
        CMakeLists.txt
    helper/
    model/
    test/
        examples-to-run.py
    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里面加:

build_lib(
    LIBNAME new-module
    SOURCE_FILES helper/new-module-helper.cc
                 model/new-module.cc
    HEADER_FILES helper/new-module-helper.h
                 model/new-module.h
    LIBRARIES_TO_LINK
        ${libinternet}
        ${libmobility}
        ${libaodv}
    TEST_SOURCES test/new-module-test-suite.cc
)

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

第三步 添加源文件列表

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

set(source_files
    helper/adhoc-aloha-noack-ideal-phy-helper.cc
    helper/spectrum-analyzer-helper.cc
    helper/spectrum-helper.cc
    helper/tv-spectrum-transmitter-helper.cc
    helper/waveform-generator-helper.cc
    model/aloha-noack-mac-header.cc
    model/aloha-noack-net-device.cc
    model/constant-spectrum-propagation-loss.cc
    model/friis-spectrum-propagation-loss.cc
    model/half-duplex-ideal-phy-signal-parameters.cc
    model/half-duplex-ideal-phy.cc
    model/matrix-based-channel-model.cc
    model/microwave-oven-spectrum-value-helper.cc
    model/multi-model-spectrum-channel.cc
    model/non-communicating-net-device.cc
    model/single-model-spectrum-channel.cc
    model/spectrum-analyzer.cc
    model/spectrum-channel.cc
    model/spectrum-converter.cc
    model/spectrum-error-model.cc
    model/spectrum-interference.cc
    model/spectrum-model-300kHz-300GHz-log.cc
    model/spectrum-model-ism2400MHz-res1MHz.cc
    model/spectrum-model.cc
    model/spectrum-phy.cc
    model/spectrum-propagation-loss-model.cc
    model/phased-array-spectrum-propagation-loss-model.cc
    model/spectrum-signal-parameters.cc
    model/spectrum-value.cc
    model/three-gpp-channel-model.cc
    model/three-gpp-spectrum-propagation-loss-model.cc
    model/trace-fading-loss-model.cc
    model/tv-spectrum-transmitter.cc
    model/waveform-generator.cc
    model/wifi-spectrum-value-helper.cc
)

set(header_files
    helper/adhoc-aloha-noack-ideal-phy-helper.h
    helper/spectrum-analyzer-helper.h
    helper/spectrum-helper.h
    helper/tv-spectrum-transmitter-helper.h
    helper/waveform-generator-helper.h
    model/aloha-noack-mac-header.h
    model/aloha-noack-net-device.h
    model/constant-spectrum-propagation-loss.h
    model/friis-spectrum-propagation-loss.h
    model/half-duplex-ideal-phy-signal-parameters.h
    model/half-duplex-ideal-phy.h
    model/matrix-based-channel-model.h
    model/microwave-oven-spectrum-value-helper.h
    model/multi-model-spectrum-channel.h
    model/non-communicating-net-device.h
    model/single-model-spectrum-channel.h
    model/spectrum-analyzer.h
    model/spectrum-channel.h
    model/spectrum-converter.h
    model/spectrum-error-model.h
    model/spectrum-interference.h
    model/spectrum-model-300kHz-300GHz-log.h
    model/spectrum-model-ism2400MHz-res1MHz.h
    model/spectrum-model.h
    model/spectrum-phy.h
    model/spectrum-propagation-loss-model.h
    model/phased-array-spectrum-propagation-loss-model.h
    model/spectrum-signal-parameters.h
    model/spectrum-value.h
    model/three-gpp-channel-model.h
    model/three-gpp-spectrum-propagation-loss-model.h
    model/trace-fading-loss-model.h
    model/tv-spectrum-transmitter.h
    model/waveform-generator.h
    model/wifi-spectrum-value-helper.h
    test/spectrum-test.h
)

build_lib(
  LIBNAME spectrum
  SOURCE_FILES ${source_files}
  HEADER_FILES ${header_files}
  LIBRARIES_TO_LINK ${libpropagation}
                    ${libantenna}
  TEST_SOURCES
    test/spectrum-ideal-phy-test.cc
    test/spectrum-interference-test.cc
    test/spectrum-value-test.cc
    test/spectrum-waveform-generator-test.cc
    test/three-gpp-channel-test-suite.cc
    test/tv-helper-distribution-test.cc
    test/tv-spectrum-transmitter-test.cc
)

 可见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为例子(在上面代码里面已经粘贴过一次了,这里再粘贴一次):

build_lib(
  LIBNAME spectrum
  SOURCE_FILES ${source_files}
  HEADER_FILES ${header_files}
  LIBRARIES_TO_LINK ${libpropagation}
                    ${libantenna}
  TEST_SOURCES
    test/spectrum-ideal-phy-test.cc
    test/spectrum-interference-test.cc
    test/spectrum-value-test.cc
    test/spectrum-waveform-generator-test.cc
    test/three-gpp-channel-test-suite.cc
    test/tv-helper-distribution-test.cc
    test/tv-spectrum-transmitter-test.cc
)

加了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++,以上内容就够了。

<think>好的,我现在需要帮助用户解决在ns-3.37中安装aqua-sim-ng模块的问题。首先,我应该回忆一下ns-3模块安装流程,尤其是第三方模块的集成方式。用户提到参考了引用[1]和[2],其中引用[1]是关于VMware和NS3.37的基础安装,而引用[2]提到了卫星模块安装需要注意的目录结构和CMakeLists的存在,这可能与aqua-sim-ng的安装类似。 首先,aqua-sim-ng是一个用于水下传感器网络仿真的模块,可能需要从官方或第三方仓库获取。由于ns-3.37使用CMake构建系统,我需要确认aqua-sim-ng是否支持CMake,或者是否需要调整其目录结构以适应的构建系统。根据引用[2]中的经验,正确找到带有CMakeLists的文件夹是关键,否则编译会失败。 接下来,安装步骤可能包括以下几步: 1. 下载aqua-sim-ng的源代码,并检查其是否包含CMakeLists.txt文件。 2.模块放置在ns-3.37目录下的contrib文件夹中,或者按照模块的文档指示进行放置。 3. 修改ns-3.37根目录的CMakeLists.txt,添加对aqua-sim-ng的引用,确保在构建时包含该模块。 4. 运行cmake和make命令进行编译,处理可能出现的依赖问题。 需要特别注意的地方是,aqua-sim-ng可能依赖其他库或模块,比如UAN模块,用户需要确认这些依赖是否已经满足。另外,如果aqua-sim-ng原本是为旧版本ns-3(使用waf构建)设计的,可能需要调整代码或构建配置以适应CMake系统,这可能会比较复杂,用户可能需要查阅相关社区或文档的支持。 此外,用户可能遇到的常见问题包括路径错误、缺少依赖项、或CMake配置错误,应该给出相应的解决建议,比如检查日志、安装缺失的依赖包等。最后,测试安装是否成功,可以通过运行示例脚本或查看模块是否出现在已启用的模块列表中。</think>根据NS-3模块安装的通用流程和引用资料的提示,安装aqua-sim-ng模块可分为以下步骤: ### 步骤 1:获取aqua-sim-ng源码 从官方仓库克隆最代码: ```bash cd ns-3.37/contrib git clone https://github.com/Project25/aqua-sim-dev.git aqua-sim-ng ``` 需确认克隆后的目录包含$CMakeLists.txt$文件[^2] ### 步骤 2:配置模块集成 1. 在$ns-3.37/contrib$目录中检查是否生成$aqua-sim-ng$软链接 2. 修改根目录$CMakeLists.txt$,添加: ```cmake add_subdirectory(contrib/aqua-sim-ng) ``` ### 步骤 3:编译验证 ```bash ./ns3 configure --enable-examples --enable-tests ./ns3 build ``` 若出现"aqua-sim-ng module is now enabled"提示表示集成成功 ### 依赖处理注意 1. 需确保已安装$libxml2$和$sqlite3$: ```bash sudo apt-get install libxml2-dev sqlite3 ``` 2. 若遇到$UAN模块$依赖问题,需先激活$src/uan$模块
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值