首页 > 代码库 > NS3网络仿真(4): DataRate属性

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<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;
}

仅仅只是我们眼下暂且无论这些属性。


改动DataRate的值。能够发如今NetAnim中最明显的表现就是用以表示数据包的箭头的长度,当DataRate2Mbps时:

技术分享

DataRate变为200Kbps时就变成了:

技术分享


区别还是非常明显的!









??

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