zoukankan      html  css  js  c++  java
  • NS3网络仿真(6): 总线型网络

    快乐虾

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

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


    NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模拟了一个总线型网络及CSMA协议。

    # // Default Network Topology
    # //
    # //       10.1.1.0
    # // n0 -------------- n1   n2   n3   n4
    # //    point-to-point  |    |    |    |
    # //                    ================
    # //                      LAN 10.1.2.0
    

    仅仅只是NS3提供的演示样例代码是C++的,我们用Python改写它。


    first.py一样,我们先构造n0-n1之间的点对点连接:

    # 构建点对点连接
    p2pNodes = ns.network.NodeContainer()
    p2pNodes.Create (2)
    
    pointToPoint = ns.point_to_point.PointToPointHelper()
    pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps"))
    pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms"))
    p2pDevices = pointToPoint.Install (p2pNodes)
    

    再构造n1-n4之间的总线型连接:

    # 构建总线连接
    nCsma = 3
    
    csmaNodes = ns.network.NodeContainer()
    csmaNodes.Add (p2pNodes.Get (1))
    csmaNodes.Create (nCsma)
    
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps"))
    csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560)))
    csmaDevices = csma.Install (csmaNodes)
    

    接下来为每一个节点安装协议栈。这里须要注意的是每一个节点仅仅能安装一次,这也是这里使用p2pNodes.Get(0)的原因:

    # 安装协议栈
    stack = ns.internet.InternetStackHelper()
    stack.Install (p2pNodes.Get(0))
    stack.Install (csmaNodes)
    

    接下来为每一个节点配置IP

    # 配置IP
    address = ns.internet.Ipv4AddressHelper()
    address.SetBase (
        ns.network.Ipv4Address("10.1.1.0"), 
        ns.network.Ipv4Mask("255.255.255.0"))
    p2pInterfaces = address.Assign (p2pDevices)
    
    address.SetBase (
        ns.network.Ipv4Address("10.1.2.0"), 
        ns.network.Ipv4Mask("255.255.255.0"))
    csmaInterfaces = address.Assign (csmaDevices)
    

    接着配置要模拟执行的Echo服务和客户端:

    # 配置应用程序
    echoServer = ns.applications.UdpEchoServerHelper (9)
    
    serverApps = echoServer.Install (csmaNodes.Get (nCsma))
    serverApps.Start (ns.core.Seconds (1.0))
    serverApps.Stop (ns.core.Seconds (20.0))
    
    echoClient = ns.applications.UdpEchoClientHelper (csmaInterfaces.GetAddress (nCsma), 9)
    echoClient.SetAttribute ("MaxPackets", ns.core.UintegerValue (5))
    echoClient.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (1.0)))
    echoClient.SetAttribute ("PacketSize", ns.core.UintegerValue (1024))
    
    clientApps = echoClient.Install (p2pNodes.Get (0))
    clientApps.Start (ns.core.Seconds (2.0))
    clientApps.Stop (ns.core.Seconds (20.0))
    
    

    因为我们使用了两个网段,因而须要配置路由:

    # 全局路由管理器依据节点产生 的链路通告为每一个节点建立路由表
    ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
    

    此后我们就能够開始仿真了:

    # 開始仿真
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    

    此时我们的脚本还没有不论什么输出,我们让NS3生成能够在NetAnim中使用的xml文件,在run前加入以下的脚本:

    anim = ns.netanim.AnimationInterface('second.xml')
    anim.SetConstantPosition(p2pNodes.Get(0), 10, 10)
    anim.SetConstantPosition(csmaNodes.Get(0), 30, 10)
    anim.SetConstantPosition(csmaNodes.Get(1), 40, 10)
    anim.SetConstantPosition(csmaNodes.Get(2), 50, 10)
    anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)
    

    执行此脚本后能够在项目路径下生成second.xml文件,在NetAnim下打开:


     


    再让NS3保存模拟的数据包:

    pointToPoint.EnablePcapAll ("second");
    csma.EnablePcap ("second", csmaDevices.Get (1), True)
    


    etherreal打开生成的pcap文件,有点问题:


    生成的数据包校验错误。校验码全为0。这是因为NS3没有打开Checksum的缘故,在脚本中加上一行:


    ns.core.GlobalValue.Bind("ChecksumEnabled",ns.core.BooleanValue (True))


    再执行此脚本:












    
  • 相关阅读:
    【卡西欧Fx-5800p系列教程】Pol()和Rec()正反算妙用
    《关于2013年全国测量人员最低工资标准的通知》
    易语言测量编程工具教程篇
    价值300元的《测量宝典》
    业界常用的和不常用cad快捷键
    角度格式批量转化弧度—易语言
    Web.config中 mode="RemoteOnly" 跟mode="On" 区别
    Oracle 批量修改某个用户下表的表空间
    JQuery radio单选框应用
    RadioButtonFor控件
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6724823.html
Copyright © 2011-2022 走看看