NS3网络仿真(4): DataRate属性

快乐虾

http://blog.csdn.net/lights_joy/

欢迎转载,但请保留作者信息


first.py中创建了一个点到点的信道,且配置了两个属性:


pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("2Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("100ms"))

在配置DataRate时,first.py传递了一个字符串”2Mbps”,这个字符串最后由下面的C++代码进行解析:

bool
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”顺藤摸瓜,可以发现其它几个设备属性:

TypeId 
SimpleNetDevice::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
    .SetParent ()
    .SetGroupName("Network") 
    .AddConstructor ()
    .AddAttribute ("ReceiveErrorModel",
                   "The receiver error model used to simulate packet loss",
                   PointerValue (),
                   MakePointerAccessor (&SimpleNetDevice::m_receiveErrorModel),
                   MakePointerChecker ())
    .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 ())
    .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;
}

只不过我们目前暂且不管这些属性。


修改DataRate的值,可以发现在NetAnim中最明显的表现就是用以表示数据包的箭头的长度,当DataRate2Mbps时:

NS3网络仿真(4): DataRate属性_第1张图片

DataRate变为200Kbps时就变成了:

NS3网络仿真(4): DataRate属性_第2张图片


差别还是很明显的!











你可能感兴趣的:(通信仿真,VS2013,ns3,python,网络仿真)