NS3(Network Simulator)ns3

NS3 TypeId说明以及使用

2017-06-25  本文已影响284人  shawn168

介绍

ns3的TypeId的设计非常巧妙,基本上每一个模块类都有TypeId属性。

TypeId将每一个模块类的属性和跟踪源集合在一起,非常方便的进行属性值的设定和跟踪源的回调函数的设定。

可以参看NS3 Attribute和Config内容翻译

定义

TypeId
WifiPhy::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::WifiPhy")
    .SetParent<Object> ()
    .SetGroupName ("Wifi")
    .AddAttribute ("Frequency",
                   "The operating center frequency (MHz)",
                   UintegerValue (0),
                   MakeUintegerAccessor (&WifiPhy::GetFrequency,
                                         &WifiPhy::SetFrequency),
                   MakeUintegerChecker<uint32_t> ())
    .AddTraceSource ("PhyTxBegin",
                     "Trace source indicating a packet "
                     "has begun transmitting over the channel medium",
                     MakeTraceSourceAccessor (&WifiPhy::m_phyTxBeginTrace),
                     "ns3::Packet::TracedCallback")
  ;
  return tid;
}

上面代码
位置

/wifi/model/wifi-phy.cc

方法说明

SetParent:设置WifiPhy类的父类

SetGroupName :设置WifiPhy类所在模块组

AddAttribute :添加属性

AddTraceSource :添加跟踪源

另外的常用的一个方法:

AddConstructor:添加构造器

使用

//设置属性
m_wifiphy->SetAttribute ("Frequency", TimeValue (m_frequency));

//设置属性,返回bool值说明是否成功
m_wifiphy->SetAttributeFailSafe("Frequency", DoubleValue (m_lookAroundRate));

//设置跟踪源回调
m_wifiphy->TraceConnectWithoutContext ("PhyTxBegin",
                                          MakeCallback (&TcpSocketBase::UpdateCwnd, this));

//解除跟踪源回调
m_wifiphy->TraceDisconnectWithoutContext ("PhyTxBegin", 
                                          MakeCallback (&HierarchicalMobilityModel::ChildChanged, this));

这些代码只是示例,大家能看明白怎么使用就行了。

这些方法所在的位置是:/core/model/object-base.cc


另外:

NS3中很少使用上面的方式设置属性值的,最常用的是通过Config类完成:

Config::Set ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Phy/$ns3::YansWifiPhy/ChannelNumber", 
             UintegerValue(40));

*是通配符

NodeList是一个类,每创建一个Node对象,都会保存NodeList中。

DeviceList也是一个类,每创建一个NetDevice对象都会保存在DeviceList中。

*表示全部,0 1 2 这些数字表示第几个Node对象或者NetDevice对象

$ns3::WifiNetDevice表示对象类型

Phy表示$ns3::WifiNetDevice这个对象的属性。这个属性在类WifiNetDevice或者其父类里面的TypeId中有定义。

$ns3::YansWifiPhy](#)表示Phy这个属性的类型值

ChannelNumber表示$ns3::YansWifiPhy这个对象的属性。这个属性可以在类YansWifiPhy或者其父类里面的TypeId里面有定义。

上一篇下一篇

猜你喜欢

热点阅读