Overview of VANET Project(2011) on NS2 Perspective Part 1 of 2 Jae-Myeong Lee Human-centric Mobile Computing Lab.
Today’s Agenda What is NS2? Introduction to Tcl Basic syntax and concepts of Tcl, oTcl, Tclcl NS2 Structure overview Our project’s structure
What is NS2? NS2 is abbreviation of Network Simulator 2. Open-sourced and free to use on research and education – Free in non-commercial use Simulates network communication Unix-based simulator – Simulation in MS-Windows, it can be run in Cygwin environment. No support for external plugin
What is NS2? (Cont.) Written in C++, compiled with g++ compiler Uses oTcl as main script language. – oTcl is object-oriented extension of Tcl script language. – Tcl, or Tool Command Language is lightweight script language. We used NS2 to simulate VANET network communication.
Introduction to Tcl NS2 uses Tcl as a main language. – You have to understand Tcl first, to know NS2. Tcl is pretty simple language, we can learn it in a moment. We are going to figure out basis of Tcl, oTcl, Tclcl.
Basic Tcl Comment: can comment with ‘#’ symbol – On a blank line: ‘#’ # blank line comment – After a command: ‘;#’ * ; is a command token. $ns run ;# run ns simulator Using Tcl Interpreter in user-interactive mode: – Can input command only. not values. – If there is a return value from command, it will be printed to screen. – $ tcl % 1 Error % puts “Hello world!” OK Using Tcl Interpreter in script(batch) mode: $ tcl filename.tcl Using variables with set command: – set x 10;# set x’s value to 10 – set x;# get x’s value
Basic Tcl (Cont.) Passing parameter as return value of another command – set y [set x];# x’s value is copied to variable y. A variable can be accessed by $(variable) notation. – In expression, $a means a’s value. – set cmd “puts”;# set cmd’s value as string “puts” – $cmd “hello”;# same to puts “hello”. – set y [expr $x];# set y’s value as x’s value. – puts “$x is ten”;# refer variable’s value in a string. “expr” command: evaluate the expression passed as argument – expr 1+1;# 2 is printed (in interactive mode)
Basic Tcl (Cont.) User-defined function – proc { } { …;# commands return ;# optional } – Example: proc pow {x n} { ;# power function with recursion if {$n == 1} { return $x } set part [pow $x [expr $n-1]] ;# recursive call return [expr $x*$part] } set y [pow 2 16];# now, y is puts $y
Basic Tcl (Cont.) ETC – Control flows if {condition} { action } else { action } while {condition} { action } for {initialization} {condition} {step} { action } Example of for statement: for {set i 0} {$i < 10} {incr i} { puts “$\i == $i” } ;# incr command increases a variable one. – Using file I/O set file [open “input.txt” r+] ;# open file handle (r+ means read/write/no creation) set line [gets $file] ;# read a line puts $line ;# print to screen puts –nonewline $file “hello!”;# if give a file handle to puts(), it will work in files. close $file ;# close file handle
Basic oTcl oTcl extends object-oriented features in existing Tcl syntax. – Create a class Class Person ;# just simple – Add new method to a class Person instproc init {age} {;# ”init” is constructor method $self instvar age_;# ready a public member variable to class ;# $self is same to this in C++ or Java. set age_ $age;# initialize member variable’s value } – Create a subclass: There are two method to do this. # Using Naming Convention (Parent/Child is a subclass of class Parent) Class Person/Child # Using –superclass directive Class Child –superclass Person – Creating a new instance object of a class (use new command) set myperson [new Person];# now, $myperson is instance of class Person
Basic oTcl (Cont.) – Setting and getting of member variable (extension of set command) $myperson set age_ 10 ;# set $myperson’s age_ to 10. puts [$myperson set age_] ;# get $myperson’s age_ and print it – Call a method $myperson greet;# just simple. ;# $instance_variable – Call superclass’ constructor function when initialize Example in our project: Agent/Traff instproc init args {;# notice that we didn’t put {} brackets around args ;# it means, $args is an array, which has arguments. $self next $args;# next method passes the argument to superclass’ constructor # (… do something …) }
Basic Tclcl Tclcl was made for cooperation of oTcl and C++. All oTcl objects are expressed in TclObject C++ class. TclObject’s methods – void TclObject::bind(const char *var, datatype *value); Bind a C++ variable as member variable, into oTcl language space. var is a member variable name, to use in oTcl. – int TclObject::invoke(const char *method, …); Invoke a method of oTcl object. – static TclObject *TclObject::New(const char *className); Create a new instance of specified class. – static TclObject *TclObject::lookup(const char *name); Lookup existing instance object, from oTcl space. Example: TclObject *ns = TclObject::lookup(“simulator_”); ns->invoke(“run”); // call $simulator_’s run (same to, $simulator_ run in oTcl)
NS2 Structure Overview NS2 is written in C++, which interacts with oTcl through Tclcl. Class Hierarchy in C++ TclObject NsObject Connector Queue Delay Agent Trace Classifier AddrClassifier McastClassifier Other Objects Tclcl Library NS2 Library NS2 Subclasses Other Objects in oTcl
NS2 Structure Overview (Cont.) Order of simulation – oTcl Interpreter interprets a Tcl script. $ ns myscript.tcl or $ ns (in user-interactive mode) – Tcl script will set up events and simulation parameters with Tcl codes. – Tcl script will calls Simulator::run(). Example code: set ns [new Simulator] $ns at 1 “puts \“Hello World!\”” ;# put a command into event queue $ns at 1.5 “exit” $ns run – Simulator::run() calls Scheduler::run() internally. Simulator instproc run {} { $self check-smac # … return [$scheduler_ run] } ;# on ns-2.34/tcl/lib/ns-lib.tcl
NS2 Structure Overview (Cont.) Source code directory path – Main NS2 classes are in ns-directory/common/*.cc ns-directory/common/*.h – Main NS2 Tcl scripts are in ns-directory/tcl/lib/*.tcl – Simulator and scheduler is defined in Simulator: ns-directory/common/simulator.cc ns-directory/common/simulator.h Scheduler: ns-directory/common/scheduler.cc ns-directory/common/scheduler.h ns-directory/tcl/lib/ns-scheduler.tcl
Our project’s structure No use existing event queue (scheduler). – The reason why: Event is not fixed and unknown in every simulation, because event is just depending on Quadstone Paramics. – We made new scheduler Object in oTcl space named TraffCalendarScheduler, which is inherited from CalendarScheduler – And also we made new Simulator object, named SimTraff. – TraffCalendarScheduler::run() waits for event from Q-Paramics. We will discuss it next week, in more details.
Thank you! Any questions?