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> ();