NS3网络仿真之:DataRate属性

1 篇文章 1 订阅
1 篇文章 0 订阅

前言序锦


本非通信人,却也来瞧瞧这通信的奇妙!由于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
      这里写图片描述
  • 分析: 我们可以发现,在NetAnim中,最明显的能观察出来的就是用以表示数据包的箭头的长度,当DataRate的值修改为200Mbps时,箭头明显变长,是呈现一个正比例的方式增长的!

好了,今天的简单分享就到此为止了,想学NS3的童鞋们,我们可以一起学习哟,加油!!!

好的,这里给您提供一个简单的NS-3仿真代码示例: ```c++ #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("FirstNS3Script"); int main (int argc, char *argv[]) { // 初始化日志记录 LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); // 创建节点 NodeContainer nodes; nodes.Create (2); // 定义网络设备 PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); // 连接两个节点 NetDeviceContainer devices; devices = pointToPoint.Install (nodes); // 安装网络协议栈 InternetStackHelper stack; stack.Install (nodes); // 分配IP地址 Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign (devices); // 安装服务器应用程序 UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); // 安装客户端应用程序 UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); // 启动仿真器 Simulator::Run (); Simulator::Destroy (); return 0; } ``` 这个例子创建了两个节点,并通过点对点连接它们。然后,安装了一个UDP回显服务器应用程序和一个UDP回显客户端应用程序,以测试连接是否正常工作。在仿真期间,客户端应用程序将向服务器应用程序发送一个数据包,并在1秒内等待服务器应用程序响应。仿真器将运行10秒钟,最终结束。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HuaCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值