前言序锦
本非通信人,却也来瞧瞧这通信的奇妙!由于SRTP项目选的是通信的项目,所以这段时间,也一直在接触与NS3相关的知识,进他就来和大家简单聊一下……
正文
- 首先我们需要在first.py文件中创建一个点到点的信道,并配置来两个属性:
PointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("10Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("100ms"))
- 上面的代码我们配置了DataRate的属性时,first.py传递来一个字符串“10Mbps”(同时给“Delay”属性值传递来一个字符串”100ms“,这是传入来一个延时,这个不作讨论),这个字符串最后通过C++代码来进行解析:
DataRate::DoParse (const std::string s, uint64_t *v)
{
NS_LOG_FUNCTION (s << v);
std::string::size_type n = s.find_first_not_of ("0123456789.");
if (n != std::string::npos)
{ // Found non-numeric
std::istringstream iss;
iss.str (s.substr (0, n));
double r;
iss >> r;
std::string trailer = s.substr (n, std::string::npos);
if (trailer == "bps")
{
// bit/s
*v = (uint64_t)r;
}
else if (trailer == "b/s")
{
// bit/s
*v = (uint64_t)r;
}
else if (trailer == "Bps")
{
// byte/s
*v = (uint64_t)(r * 8);
}
else if (trailer == "B/s")
{
// byte/s
*v = (uint64_t)(r * 8);
}
else if (trailer == "kbps")
{
// kilobits/s
*v = (uint64_t)(r * 1000);
}
else if (trailer == "kb/s")
{
// kilobits/s
*v = (uint64_t)(r * 1000);
}
else if (trailer == "Kbps")
{
// kilobits/s
*v = (uint64_t)(r * 1000);
}
else if (trailer == "Kb/s")
{
// kilobits/s
*v = (uint64_t)(r * 1000);
}
else if (trailer == "kBps")
{
// kiloByte/s
*v = (uint64_t)(r * 8000);
}
else if (trailer == "kB/s")
{
// KiloByte/s
*v = (uint64_t)(r * 8000);
}
else if (trailer == "KBps")
{
// kiloByte/s
*v = (uint64_t)(r * 8000);
}
else if (trailer == "KB/s")
{
// KiloByte/s
*v = (uint64_t)(r * 8000);
}
else if (trailer == "Kib/s")
{
// kibibit/s
*v = (uint64_t)(r * 1024);
}
else if (trailer == "KiB/s")
{
// kibibyte/s
*v = (uint64_t)(r * 8192);
}
else if (trailer == "Mbps")
{
// MegaBits/s
*v = (uint64_t)(r * 1000000);
}
else if (trailer == "Mb/s")
{
// MegaBits/s
*v = (uint64_t)(r * 1000000);
}
else if (trailer == "MBps")
{
// MegaBytes/s
*v = (uint64_t)(r * 8000000);
}
else if (trailer == "MB/s")
{
// MegaBytes/s
*v = (uint64_t)(r * 8000000);
}
else if (trailer == "Mib/s")
{
// MebiBits/s
*v = (uint64_t)(r * 1048576);
}
else if (trailer == "MiB/s")
{
// MebiByte/s
*v = (uint64_t)(r * 1048576 * 8);
}
else if (trailer == "Gbps")
{
// GigaBit/s
*v = (uint64_t)(r * 1000000000);
}
else if (trailer == "Gb/s")
{
// GigaBit/s
*v = (uint64_t)(r * 1000000000);
}
else if (trailer == "GBps")
{
// GigaByte/s
*v = (uint64_t)(r * 8*1000000000);
}
else if (trailer == "GB/s")
{
// GigaByte/s
*v = (uint64_t)(r * 8*1000000000);
}
else if (trailer == "Gib/s")
{
// GibiBits/s
*v = (uint64_t)(r * 1048576 * 1024);
}
else if (trailer == "GiB/s")
{
// GibiByte/s
*v = (uint64_t)(r * 1048576 * 1024 * 8);
}
else
{
return false;
}
return true;
}
std::istringstream iss;
iss.str (s);
iss >> *v;
return true;
}
从这段代码我们可以很明显的看出,NS3中速率的字符串的表达方式以及其意义
- 我们通过“DataRate”这个属性来看,可以发现其他几个设备的属性,在这里顺便讲一下:
SimpleNetDevice::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::SimpleNetDevice")
.SetParent<NetDevice> ()
.SetGroupName("Network")
.AddConstructor<SimpleNetDevice> ()
.AddAttribute ("ReceiveErrorModel",
"The receiver error model used to simulate packet loss",
PointerValue (),
MakePointerAccessor (&SimpleNetDevice::m_receiveErrorModel),
MakePointerChecker<ErrorModel> ())
.AddAttribute ("PointToPointMode",
"The device is configured in Point to Point mode",
BooleanValue (false),
MakeBooleanAccessor (&SimpleNetDevice::m_pointToPointMode),
MakeBooleanChecker ())
.AddAttribute ("TxQueue",
"A queue to use as the transmit queue in the device.",
StringValue ("ns3::DropTailQueue"),
MakePointerAccessor (&SimpleNetDevice::m_queue),
MakePointerChecker<Queue> ())
.AddAttribute ("DataRate",
"The default data rate for point to point links. Zero means infinite",
DataRateValue (DataRate ("0b/s")),
MakeDataRateAccessor (&SimpleNetDevice::m_bps),
MakeDataRateChecker ())
.AddTraceSource ("PhyRxDrop",
"Trace source indicating a packet has been dropped "
"by the device during reception",
MakeTraceSourceAccessor (&SimpleNetDevice::m_phyRxDropTrace),
"ns3::Packet::TracedCallback")
;
return tid;
}
这也是NS3仿真过程几个比较重要的属性,后面我们会有详细的介绍,这里找出来让大家一起去探讨……
- 通过NetAnim动态演示工具(后面我也会专门来谈一下NetAnim)我们来修改DataRate的属性值来找出异同:
- 当DataRate的的值为:10Mbps
- 当DataRate的值为:200Mbps
- 当DataRate的的值为:10Mbps
- 分析: 我们可以发现,在NetAnim中,最明显的能观察出来的就是用以表示数据包的箭头的长度,当DataRate的值修改为200Mbps时,箭头明显变长,是呈现一个正比例的方式增长的!
好了,今天的简单分享就到此为止了,想学NS3的童鞋们,我们可以一起学习哟,加油!!!