NS3 设置属性和Type 系统
2021/10/31 6:10:48
本文主要是介绍NS3 设置属性和Type 系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
NS3的属性设置比较奇怪,其内部的类按照网络进行划分为node, application,channel, net device, topology helpers, 这些类型后面,每个都跟着不同类型的实现,在实际使用中经常会看到这样的属性设置:
pointToPoint.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (bps)));
通过字符串去设置属性,这些属性还没在官方文档找到很具体的说明,但实际调用效果是把Device的DataRate设置为后面的bps
展示一下调用堆栈
void PointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1) { m_deviceFactory.Set (n1, v1); }
设置是通过factory进行参数保存
void ObjectFactory::Set (std::string name, const AttributeValue &value) { NS_LOG_FUNCTION (this << name << &value); if (name == "") { return; } struct TypeId::AttributeInformation info; // 通过m_tid 查找到该属性,并进行check if (!m_tid.LookupAttributeByName (name, &info)) { NS_FATAL_ERROR ("Invalid attribute set (" << name << ") on " << m_tid.GetName ()); return; } Ptr<AttributeValue> v = info.checker->CreateValidValue (value); if (v == 0) { NS_FATAL_ERROR ("Invalid value for attribute set (" << name << ") on " << m_tid.GetName ()); return; } m_parameters.Add (name, info.checker, value.Copy ()); }
其中最重要的就是这个m_tid变量,它是一个 TypeId m_tid;
类型变量, 对实际对象的相关属性进行了仿射,比如说Device, 在AddAttribute中的Accessor就是这个字符串属性所对应的变量和函数,所以要找attribute,直接在找到类就能看到有什么属性能设置。对于不熟悉的属性,进一步也能通过官方API文档找到相关的类,查看描述
TypeId PointToPointNetDevice::GetTypeId (void) { static TypeId tid = TypeId ("ns3::PointToPointNetDevice") .SetParent<NetDevice> () .SetGroupName ("PointToPoint") .AddConstructor<PointToPointNetDevice> () .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit", UintegerValue (DEFAULT_MTU), MakeUintegerAccessor (&PointToPointNetDevice::SetMtu, &PointToPointNetDevice::GetMtu), MakeUintegerChecker<uint16_t> ()) .AddAttribute ("Address", "The MAC address of this device.", Mac48AddressValue (Mac48Address ("ff:ff:ff:ff:ff:ff")), MakeMac48AddressAccessor (&PointToPointNetDevice::m_address), MakeMac48AddressChecker ()) .AddAttribute ("DataRate", "The default data rate for point to point links", DataRateValue (DataRate ("32768b/s")), MakeDataRateAccessor (&PointToPointNetDevice::m_bps), MakeDataRateChecker ()) .... }
可是这个实际对象类型是什么呢? ObjectFactory::Set()中只是保存这些参数到m_parameters
中, factory创建实际对象的时候才会赋值这些属性,所以只需要找到factory.create()的对象类型就可以找到这些类了, 比如下面的PointToPointNetDevice;
Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
这篇关于NS3 设置属性和Type 系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现