Multicasting in NS2

Mutlicast routing in ns2 can be easily set using any of the following syntax

Method 1:
        set ns [new Simulator -multicast on]
Method 2:
        set ns [new Simulator]
        $ns multicast
when multicast mode is enabled, the nodes behave differently with the additional classifiers are created. A distribution tree kind of structure is created in the simulation when dealing with multicast routing.  NS supports three types of multicast routing: Centralized Multicast Routing(CM), Dense Mode (DM), Shared Tree Mode (ST) and Bi directional Shared Tree Support (BST).
Here is the method to specify the multicast routing in ns.
set cmc [$ns mrtproto CtrMcast]    # specify centralized multicast for all nodes;
# cmc is the handle for multicast protocol object;
$ns mrtproto DM                   # specify dense mode multicast for all nodes;
$ns mrtproto ST                  # specify shared tree mode to run on all nodes;

The agents in the protocol can be joining the group or leaving the group for multicasting. Two procedures are used join-group[] and leave-group[].
An example of a relatively simple multicast configuration is:
set ns [new Simulator -multicast on]; # enable multicast routing;
set group [Node allocaddr]  ; # allocate a multicast address;
set node0 [$ns node]         ;# create multicast capable nodes;
set node1 [$ns node]
$ns duplex-link $node0 $node1 1.5Mb 10ms DropTail

set tracef [open dm.tr w]
$ns trace-all $tracef

set namtracef [open dm.nam w]
$ns namtrace-all $namtracef

set mproto DM         ; # configure multicast protocol;
set mrthandle [$ns mrtproto $mproto]; # all nodes will contain multicast protocol agents;

set udp [new Agent/UDP]         ;# create a source agent at node 0;
$ns attach-agent $node0 $udp
set cbr [new Application/Traffic/CBR]       
$cbr attach-agent $udp
$udp set dst_addr_ $group
$udp set dst_port_ 0

set receiver [new Agent/LossMonitor];  # create a receiver agent at node 1;
$ns attach-agent $node1 $receiver
$ns at 0.3 "$node1 join-group $receiver $group"
$ns at 0.1 "$cbr start"

$ns at 5.0 "finish"

proc finish {} {
       global ns tracef namtracef
       exec nam dm.nam &
       close $tracef
       close $namtracef
       exit 0
}
$ns run
Sample Output :
The status bar shows that the node1 joins the group
Multicasting
Multicasting in Wired Networks 

There was no packet exchange which is discussed in the next topic
      


Centralized Multicast routing in ns2
#This example is to demonstrate the multicast routing protocol.
set ns [new Simulator -multicast on]
#Turn on Tracing
set tf [open output.tr w]
$ns trace-all $tf

# Turn on nam Tracing
set fd [open mcast.nam w]
$ns namtrace-all $fd

# Create 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]
set n6 [$ns node]
set n7 [$ns node]

# Create links with DropTail Queues
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link $n3 $n4 1.5Mb 10ms DropTail
$ns duplex-link $n3 $n7 1.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 1.5Mb 10ms DropTail
$ns duplex-link $n4 $n6 1.5Mb 10ms DropTail

# Routing protocol: say distance vector
#Protocols: CtrMcast, DM, ST, BST
#Dense Mode protocol is supported in this example
set mproto DM
set mrthandle [$ns mrtproto $mproto {}]

# Set two groups with group addresses
set group1 [Node allocaddr]
set group2 [Node allocaddr]

# UDP Transport agent for the traffic source for group1
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$udp0 set dst_addr_ $group1
$udp0 set dst_port_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp0

# Transport agent for the traffic source for group2
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
$udp1 set dst_addr_ $group2
$udp1 set dst_port_ 0
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp1

# Create receiver to accept the packets
set rcvr1 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 1.0 "$n5 join-group $rcvr1 $group1"
set rcvr2 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 1.5 "$n6 join-group $rcvr2 $group1"


set rcvr3 [new Agent/Null]
$ns attach-agent $n7 $rcvr3
$ns at 2.0 "$n7 join-group $rcvr3 $group1"

set rcvr4 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 2.5 "$n5 join-group $rcvr4 $group2"

set rcvr5 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 3.0 "$n6 join-group $rcvr5 $group2"

set rcvr6 [new Agent/Null]
$ns attach-agent $n7 $rcvr3

#The nodes are leaving the group at specified times
$ns at 3.5 "$n7 join-group $rcvr6 $group2"
$ns at 4.0 "$n5 leave-group $rcvr1 $group1"
$ns at 4.5 "$n6 leave-group $rcvr2 $group1"
$ns at 5.0 "$n7 leave-group $rcvr3 $group1"
$ns at 5.5 "$n5 leave-group $rcvr4 $group2"
$ns at 6.0 "$n6 leave-group $rcvr5 $group2"
$ns at 6.5 "$n7 leave-group $rcvr6 $group2"

# Schedule events

$ns at 0.5 "$cbr1 start"
$ns at 9.5 "$cbr1 stop"
$ns at 0.5 "$cbr2 start"
$ns at 9.5 "$cbr2 stop"

#post-processing

$ns at 10.0 "finish"
proc finish {} {
  global ns tf
   $ns flush-trace
   close $tf
   exec nam mcast.nam &
   exit 0
}

$ns set-animation-rate 3.0ms
$ns run
Sample Output Screenshots
Multicasting
Multicasting  in Wired Networks

The nodes are joining the group before the packet transfer and leaving the group at a specified time.
Multicasting
Multicasting  in Wired Networks



                                                                 

Popular posts from this blog

AWK Scripts for NS2 to process data from Trace Files

Xgraph

LEACH protocol installation in ns2 (ns-2.35)