Wednesday, June 19, 2013

My First NS-2 Teaching Experiance

Network Simulator NS-2 is a tough one to understand for beginners in the field of network simulation. Due to its credibility in the research community most of the researchers look forward to implement and realize their ideas on this simulator. I am no exception for this. As a major task of learning simulation and emulation tools in this semester, I have started learning and practicing this simulation tool along with TinyOs, nesC and TOSSIM. Unfortunately, I got an opportunity to conduct a mini workshop on NS-2 to my peers for three days. This workshop has conducted during 17-19 June 2010. However, I am very glad to work on chalking out required lesson plan for three day to conduct workshop smoothly. Finally, I came out with the following agenda.


Day 1 (17.06.2013)

Introduction to Simulation
           Discrete Event System Simulation
Simulation Tools available for computer network simulation
          Available tools, their pros and cons
Review of Network Protocol Stacks
          ISO-OSI, TCP/IP
NS-2 Installation and setup with Eclipse
           TCL scripting
           Tcl and OTcl Basics
           Writing TCL Scripts
NS-2 Trace formats (Wired and Wireless)
Introduction to AWK programming
          AWK basics
          Writing AWK scripts to analyze Trace formats
Network Simulator NS-2 Basics
Packets, Packet Headers and Header Formats

Day 2 (18.06.2013)

Recap of Day 1
Creating User Defined Agents in NS-2
           Working with TclObject, TclClass, and Agent classes
           Writing Tcl Script for using user defined agent
Simulating Wireless Networks
           Writing Tcl Script for simulating wireless networks
Introduction to STL in C++
           Utility classes (Map, Vector and Iterater classes)
Writing User Defined Routing Algorithms in NS-2
          Review – Packet, Packet Header and Header Formats
          Review – Agents and related classes
          Working with Timers
Writing Tcl Script for simulating user defined routing algorithm

Day 3 (19.06.2013)

Project – Implementing Greedy Pare meter Stateless Routing (GPSR Algorithm)
Conclusion


During this workshop my colleges (co-researchers)  have gained an insight in learning this tough tool. In addition, I have exposed to various search direction to use the simulators to the extent possible and my weak areas to strength up in network simulation with the help of their interaction and discussions.
I request the readers of this post to make comment on the course content for improvements, enhancements, suggestions, schedules, etc ( any other word in synonym) which greatly improve the credibility of teaching NS-2.  

Error Setting Color to a node dynamically - Solution

In ns-2.35, while setting node colors dynamically I faced the following error.


 _o14 color blue: can't read "attr_(COLOR)": no such variable
    while executing
"eval list "n -t [format "%.15g" [$ns now]] -s $id_ -S COLOR -c $color -o $attr_(COLOR) -i $color -I $attr_(LCOLOR)""
    invoked from within
"if [$ns is-started] {

$ns puts-nam-config  [eval list "n -t [format "%.15g" [$ns now]] -s $id_ -S COLOR -c $color -o $attr_(COLOR) -i $color -I $attr..."
    (procedure "_o14" line 6)
    (Node color line 6)
    invoked from within
"_o14 color blue"

The above solution can be rectified by 

1. Set a default node color to all nodes at the time of creating nodes.

2. Make sure that node color need to be set with 

$ns at

Example

set ns [new Simulator]

for {set i 0} {$i < $val(nn)} {incr i} {
 set node_($i) [$ns node] 
 $node_($i) color blue   ;# setting up a default color
}

$ns at 0.1 "$node_(0) color green"
$ns at 0.1 "$node_(0) color red"



Sunday, June 16, 2013

AWK to analyze trace files

In this post I am going to present analyzing NS-2 trace file with AWK script. For this, let consider the trace file obtained from the post "Creating Broadcast agents in NS-2". AWK script consists of three blocks BEGIN - where global variables are placed, logic - which are enclosed in simple flower braces ({}) for writing the logic and END - to print the results obtained by running logic.

As the output of simulation are placed in the trace file in column wise. We need to read the column to analyse the results.

let name the following file as bcast.awk


BEGIN {
rsent[4] = 0; #array to hold router sent packets
msent[4] = 0; #array to hold mac sent packets
rrecv[4] = 0; #array to hold router receive packets
mrecv[4] = 0; #array to hold mac receive packets
}
{
#column 1 of trace file representing event such as send, receive etc
event = $1;
#column 3 of trace file represent node number
node = $3;
#column 4 of trace file represent level - Router (RTR), Mac (MAC) etc
level = $4;
# increment the packet count if event is send and level is RTR
if(event == "s" && level == "RTR") {
if(node == "_0_")
rsent[0]++;
else if(node == "_1_")
rsent[1]++;
else if(node == "_2_")
rsent[2]++;
else if(node == "_3_")
rsent[3]++;
}
# increment the packet count if event is send and level is MAC
if(event == "s" && level == "MAC") {
if(node == "_0_")
msent[0]++;
else if(node == "_1_")
msent[1]++;
else if(node == "_2_")
msent[2]++;
else if(node == "_3_")
msent[3]++;
}
# increment the packet count if event is receive and level is RTR
if(event == "r" && level == "RTR") {
if(node == "_0_")
rrecv[0]++;
else if(node == "_1_")
rrecv[1]++;
else if(node == "_2_")
rrecv[2]++;
else if(node == "_3_")
rrecv[3]++;
}
# increment the packet count if event is receive and level is MAC
if(event == "r" && level == "MAC") {
if(node == "_0_")
mrecv[0]++;
else if(node == "_1_")
mrecv[1]++;
else if(node == "_2_")
mrecv[2]++;
else if(node == "_3_")
mrecv[3]++;
}
}
END {
# Print the result
for(i=0;i<4 b="" i="">
printf ("Routing Packets Sent by Node %d : %d \n",i,rsent[i]);
}
for(i=0;i<4 b="" i="">
printf ("Routing Packets Received by Node %d : %d \n",i,rrecv[i]);
}
for(i=0;i<4 b="" i="">
printf ("Mac Packets Sent by Node %d : %d \n",i,msent[i]);
}
for(i=0;i<4 b="" i="">
printf ("Mac Packets Received by Node %d : %d \n",i,mrecv[i]);
}
}

Save and close file

run the file as follows

$ awk -f bcast.awk out.tr

and see the result


Saturday, June 15, 2013

Creating Broad Cast Agent in NS-2 - Continued....

This post is with reference to post title "Creating Broad Cast Agent in NS-2", I am going to present small modification to the existing one. In the previous post all the nodes will broadcast the beacon messages  periodically. A modification to this, the problem situation is as follows: There is a base station which   transmit a beacon message periodically so that all other client nodes has to receive the beacon message. In this situation we need to make a node as base station and all other nodes as clients.

Here are the modifications to the existing code.

In the SBAgent::command method definition, modify code as

if(argc==2){
if(strcmp(argv[1],"start")==0){
my_addr_ = addr();
return TCL_OK;
}
if(strcmp(argv[1],"base-station")==0){
btimer_.resched((double)0.1);
my_addr_ = addr();
return TCL_OK;
}
}

  and Recompile the code.

./configure
make
sudo make install 

In the tcl script add the line which indicated in RED under node definition section



#===================================
#        Nodes Definition      
#===================================

for {set i 0} {$i < $val(nn)} {incr i} {
 set node_($i) [$ns node]
}

for {set i 0} {$i < $val(nn)} {incr i} {
 set u_($i) [new Agent/SB]
 $ns attach-agent $node_($i) $u_($i)
}

$ns at 0.0 "[$node_(0) set ragent_] base-station"


Save and run the file