NS3 设置属性和Type 系统

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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 系统全部内容,希望文章能够帮你解决NS3 设置属性和Type 系统所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。