NS Simulation: Scheduling Events



  • Simulation time

    • A similation system (such as NS) must have a built-in simulation clock - it represents the "clock" in the real world.
    • You can examine (read) the value of the simulation clock by using the now command in the Simulator class
    • Example::
        set  ns  [new Simulator]
      
        $ns  now   // Returns the current simulation time
      


  • Events
    • An event is an action made by a simulation entity
    • An event (action) in a simulation is represented a OTcl command !!!
    • Each event happens at a certain time and therefore, each event has an associated event time which is the time when the event will happen
    • In simulation, events are scheduled (to happen at some future time)
    • To schedule an event, use the at command in the Simulator class
    • Example::
        set  ns  [new Simulator]
      
        $ns  at  1.0  {puts "Point 1: Now = [$ns now]"}
      $ns at 8.0 {puts "Point 2: Now = [$ns now]"}
      $ns at 4.0 {puts "Point 3: Now = [$ns now]"} $ns run // Run simulation !
    • You will see the following output:
        Point 1: Now = 1
      Point 3: Now = 4
      Point 2: Now = 8

      Notice that Point 3 happens before Point 2

    • This is what is happening:
        set  ns  [new Simulator]
      
        $ns  at  1.0  {puts "Point 1: ..."}  // Schedule event at time 1.0
      $ns at 8.0 {puts "Point 2: ..."} // Schedule event at time 8.0
      $ns at 4.0 {puts "Point 3: ..."} // Schedule event at time 4.0 $ns run // Run simulation !

      So when the simulation is run, the events are "fired" in chronological order



  • Running the NS simulation
    • The run command in the Simulation class is used to run the network simuation.
    • Example:
          set  ns  [new Simulator]
      
          ... (set up simulation network)
      
          $ns run  // run simulation
      


  • Stopping an NS simulation
    • There is no "stop" command available.
    • To stop a running simulation, you must schedule a termination command before you start running the simulation.
    • The termination command is usually a Tcl procedure (to wrap up things)
    • Example:
          set  ns  [new Simulator]
      
          #Define the 'finish' procedure
      proc finish {} {
      exit 0
      } ... (set up simulation network) #Set simulation end time
      $ns at 100.0 "finish" $ns run // run simulation


  • A simple simulation....
    • To give you a taste of simulation, here is a NS script that simulates 2 person "talking" to each other:

        proc person1 {x} {
      global ns puts "Person 1:"
      puts " Hey, $x, time is [$ns now], it's your turn to say something" $ns at [expr [$ns now] + 0.4] "$x person1"
      } proc person2 {x} {
      global ns puts "Person 2:"
      puts " Hey, $x, time is [$ns now], it's your turn to say something" $ns at [expr [$ns now] + 0.6] "$x person2"
      } set ns [new Simulator] $ns at 0 "person1 person2" $ns at 4.5 "exit 0" $ns run

    • Example Program: (Demo above code)                                                
    • Output when you run this simulation:
      Person 1:
      Hey, person2, time is 0, it's your turn to say something
      Person 2:
      Hey, person1, time is 0.4, it's your turn to say something
      Person 1:
      Hey, person2, time is 1, it's your turn to say something
      Person 2:
      Hey, person1, time is 1.4, it's your turn to say something
      Person 1:
      Hey, person2, time is 2, it's your turn to say something
      Person 2:
      Hey, person1, time is 2.4, it's your turn to say something
      Person 1:
      Hey, person2, time is 3, it's your turn to say something
      Person 2:
      Hey, person1, time is 3.4, it's your turn to say something
      Person 1:
      Hey, person2, time is 4, it's your turn to say something
      Person 2:
      Hey, person1, time is 4.4, it's your turn to say something


  • Example NS Simulation Script
    • We will construct an NS simulation script that simulate the following network:

    • The following figure is a break down of the NS components that make up the above network:
    • Here is the NS (OTcl) Script that creates the above simulation:
        #Make a NS simulator
      set ns [new Simulator] # Define a 'finish' procedure
      proc finish {} {
      exit 0
      } # Create the nodes:
      set n0 [$ns node]
      set n1 [$ns node]
      set n2 [$ns node]
      set n3 [$ns node]
      set n4 [$ns node]
      set n5 [$ns node] # Create the links:
      $ns duplex-link $n0 $n2 2Mb 10ms DropTail
      $ns duplex-link $n1 $n2 2Mb 10ms DropTail
      $ns duplex-link $n2 $n3 0.3Mb 200ms DropTail
      $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
      $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail # Add a TCP sending module to node n0
      set tcp1 [new Agent/TCP/Reno]
      $ns attach-agent $n0 $tcp1 # Add a TCP receiving module to node n4
      set sink1 [new Agent/TCPSink]
      $ns attach-agent $n4 $sink1 # Direct traffic from "tcp1" to "sink1"
      $ns connect $tcp1 $sink1 # Setup a FTP traffic generator on "tcp1"
      set ftp1 [new Application/FTP]
      $ftp1 attach-agent $tcp1
      $ftp1 set type_ FTP (no necessary) # Schedule start/stop times
      $ns at 0.1 "$ftp1 start"
      $ns at 100.0 "$ftp1 stop" # Set simulation end time
      $ns at 125.0 "finish" (Will invoke "exit 0") # Run simulation !!!!
      $ns run

    • Example Program: (Demo above code)                                                


  • Problems with the above simulation
    • It simulates alright...
    • But without producing any data that we can examine !!!
    • What we still need to learn is how to add commands in the simulation program to output state variables that we are interested in !!!

http://www.mathcs.emory.edu/~cheung/Courses/558-old/Syllabus/90-NS/2-NS-Prog/events.html






NS Simulation: Scheduling Events (examples inside)的更多相关文章

  1. NS Simulation Basic

    这个网站上的一系列讲解NS2的内容真的是深入浅出,看完立刻豁然开朗.所以就接连转了几篇. Scheduling Events那篇里的例子特别好,看完就懂了. http://www.mathcs.emo ...

  2. Studying TCP's Throughput and Goodput using NS

    Studying TCP's Throughput and Goodput using NS What is Throughput Throughput is the amount of data r ...

  3. Studying TCP's Congestion Window using NS

    Studying TCP's Congestion Window using NS How to obtain TCP's CWND value The most important value th ...

  4. 18 Tar Command Examples in Linux

    FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...

  5. iOS 第三方库、插件、知名博客总结

    iOS 第三方库.插件.知名博客总结 用到的组件 1.通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FMDB 本地数据库组件 SDWebImage 多个缩略图 ...

  6. zepto.js 源码解析

    http://www.runoob.com/w3cnote/zepto-js-source-analysis.html Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...

  7. zepto源码注解

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  8. Zepto源码注释

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  9. Learn clojure in Y minutes

    Learn X in Y minutes Where X=clojure Get the code: learnclojure.clj Clojure is a Lisp family languag ...

随机推荐

  1. Linux-文件目录类命令

    l 文件目录类 pwd 指令 基本语法 pwd (功能描述:显示当前工作目录的绝对路径) 应用实例 案例:显示当前工作目录的绝对路径 ls指令 基本语法 ls [选项] [目录或是文件] 常用选项 - ...

  2. (转)linux基本变量学习:位置变量$0 、$1 shift轮替、本地变量、环境变量、特殊变量

    原文:http://blog.51cto.com/woyaoxuelinux/1863045 shell: 弱类型编程语言 强:变量在使用前,必须事先声明,甚至还需要初始化: 弱:变量用时声明,甚至不 ...

  3. 【随笔】设置title标题图标为自定义图片

    第一步: 利用图标工具(有很多)制作图标文件(favicon.ico)上传到网站所在的服务器的根目录下,这个文件必须是16*16大小的图标文件. 第二步: 在<head></head ...

  4. 存储器的保护(二)——《x86汇编语言:从实模式到保护模式》读书笔记19

    接着上一篇博文说. 5.代码段执行时的保护 每个代码段都有自己的段界限.同栈段一个道理,有效界限和G位相关. G=0:有效界限 = 描述符中的段界限 G=1:有效界限 = 描述符中的段界限值 * 0x ...

  5. Java入门系列-24-实现网络通信

    互联网上那么多设备,java 是如何与其他设备通信的呢?这次的内容是网络通信的基础,有了它咱们才能上网页.玩游戏.视频聊天. Socket 客户端套接字 Socket 客户端套接字,用于连接互联网提供 ...

  6. ASP.NET Core中使用自定义路由

    上一篇文章<ASP.NET Core中使用默认MVC路由>提到了如何使用默认的MVC路由配置,通过这个配置,我们就可以把请求路由到Controller和Action,通常情况下我们使用默认 ...

  7. mysql之调优概论

    一  简介 咱们先不说cpu的频率,内存的大小(这个和索引一样重要,但不是本文讨论的内容),硬盘的寻道时间.想起mysql的调优,最起码的必须知道explain执行计划,慢sql日志,老旧的profi ...

  8. 【idea快捷键】

    IntelliJ Idea 常用快捷键列表 idea也是可以切换到eclipse风格的快捷键方式的  在keymap中切换即可 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...

  9. 【eclipse安装黑色主题】

    eclipse Luna Service Release 2 (4.4.2)版本的自带了黑色的主题,切换下即可: 切换主题以后还需要修改下字体的主题: http://www.eclipsecolort ...

  10. 一分钟认识:Cucumber框架

    一分钟认识:Cucumber框架(一) 转自:https://www.cnblogs.com/dami520/p/3168864.html 个人感觉这个框架非常适合UI自动化测试,他不仅能把用例中的测 ...