Tuesday 7 January 2014

Packet delivery ratio calculation in ns2 using awk

Packet delivery ratio calculation in ns2 using awk

Solution

BEGIN {

        sendLine = 0;

        recvLine = 0;

        fowardLine = 0;

}



$0 ~/^s.* AGT/ {

        sendLine ++ ;

}



$0 ~/^r.* AGT/ {

        recvLine ++ ;

}



$0 ~/^f.* RTR/ {

        fowardLine ++ ;

}


$0 ~/^D.* cbr/ {

        dropLine ++ ;

}

END {

        printf "PacketDelivery Ratio:%.4f \n",(recvLine/sendLine);
}

How to Count RERR,RREP,RREQ and hello packets  in ns2 from trace file?





Solution

use the awk scrpt like below,

    BEGIN {
   
            rreq = 0;
   
           rrep = 0;
   
            hello = 0;
             rerr =0;
   
    }
   
   
   
    $0 ~/^s.*(REQUEST)/ {
   
            rreq ++ ;
   
    }
   
   
   
    $0 ~/^s.*(REPLY)/ {
   
            rrep ++ ;
   
    }
   
   
   
    $0 ~/^s.*(HELLO)/ {
   
            hello ++ ;
   
    }
   
   
    $0 ~/^s.*(ERROR)/ {
   
            rerr++ ;
   
    }
   
    END {
   
            printf "Request Count:%d \n",rreq;
        printf "Reply Count:%d \n",rrep;
        printf "Hello Count:%d \n",hello;
        printf "Rerr Count:%d \n",rerr;
   
   
    }
    }

Execute the code by typing: **awk -f "file.awk" "tracefile"**
 

How to find neighbor list in ns2

 

Solution:



# neighbor node calculation

    set nbr [open Neighbor w]

    puts $nbr "\t\t\t\t\tNeighbor Detail"

    puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

    puts $nbr "\tNode\t\tNb node\t\tNode-Xpos\tNode-Ypos\tNb-Xpos\t\tNb-Ypos\t\tDistance(d)"

    puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

    close $nbr

    proc distance { n1 n2 nd1 nd2} {

      global c n bnd src dst j0 j1

      set a 0

      set nbr [open Neighbor a]

      set x1 [expr int([$n1 set X_])]

      set y1 [expr int([$n1 set Y_])]

     set x2 [expr int([$n2 set X_])]

      set y2 [expr int([$n2 set Y_])]

      set d [expr int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]

      if {$d<300} {

        if {$nd2!=$nd1} {

          puts $nbr "\t$nd1\t\t$nd2\t\t$x1\t\t$y1\t\t$x2\t\t$y2\t\t$d"

        }
        }



      close $nbr
    }



    U can use above function to print neighbor node's of all nodes in ns2.
 call the function"distance" to print neighbor list
    for example,

    for {set i 0} {$i <$val(nn)} {incr i} {
          for {set j 0} {$j <$val(nn)} {incr j} {
   
            $ns at 10.002 "distance $n($i) $n($j) $i $j"
          }
        }

    here val(nn) denotes number of nodes.