How to Implement Flat Topology in NS2
To implement the flat topology in Network Simulator 2 (ns2), this topology is a network infrastructure that has nodes is at the same hierarchical level, means that every node interacts directly with other nodes without any centralized control or hierarchical structure. In this topology, nodes have similar status and the network is not break down into any layers or sub-levels. This variant of topology is typical in ad-hoc networks with all nodes are peers and can interact with one another.
The Flat Topology can be executed by designing numerous nodes and linking them to each other in a peer-to-peer manner, without any hierarchical structure in ns2.
The provided set up will help you implement the flat topology in ns2:
Steps to Implement Flat Topology in NS2:
- Set Up the Nodes:
- Develop numerous nodes indicating devices or hosts in the network.
- Connect the Nodes:
- In a flat topology, all nodes can interact with one another. You can either link the nodes directly (using duplex links) or set up them as wireless nodes to replicate an ad-hoc network.
- Simulate Traffic Flow:
- Simulate communication in the flat network by producing traffic amongst nodes.
Example of Flat Topology Implementation in NS2:
Below is an example script that demonstrates how to set up a Flat Topology in NS2 using 6 nodes linked in a peer-to-peer manner.
# Create a new simulator
set ns [new Simulator]
# Open trace file for output
set tracefile [open out.tr w]
$ns trace-all $tracefile
# Define nodes in the flat topology (6 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 duplex links between nodes in a flat, peer-to-peer structure
$ns duplex-link $n0 $n1 10Mb 5ms DropTail
$ns duplex-link $n0 $n2 10Mb 5ms DropTail
$ns duplex-link $n1 $n3 10Mb 5ms DropTail
$ns duplex-link $n2 $n4 10Mb 5ms DropTail
$ns duplex-link $n3 $n4 10Mb 5ms DropTail
$ns duplex-link $n4 $n5 10Mb 5ms DropTail
# Define TCP agents for communication
# Traffic from n0 to n5
set tcp0 [new Agent/TCP]
set sink0 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n5 $sink0
$ns connect $tcp0 $sink0
# Simulate FTP traffic from n0 to n5
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 “$ftp0 start”
$ns at 3.0 “$ftp0 stop”
# Traffic from n2 to n3
set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $tcp1
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1
# Simulate FTP traffic from n2 to n3
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 4.0 “$ftp1 start”
$ns at 6.0 “$ftp1 stop”
# End the simulation after 10 seconds
$ns at 10.0 “finish”
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation
$ns run
Explanation of the Script:
- Nodes:
- n0, n1, n2, n3, n4, and n5 signify the nodes in the flat topology. These nodes are peers, meaning they are all at the same level and can interact with one another without any hierarchy.
- Links:
- Duplex links are generated amongst the nodes in a peer-to-peer structure. For instance, n0 is linked to n1 and n2, n1 is linked to n3, and so on.
- The link bandwidth is set to 10Mb, and the delay is set to 5ms.
- Traffic Simulation:
- Use the TCP agents to replicate communication amongst nodes.
- FTP (File Transfer Protocol) produces traffic amongst nodes. For instance, n0 delivers data to n5, and n2 sends data to n3.
- The beginning and end times for traffic are controlled to imitate communication at various times.
- Simulation Duration:
- The simulation executes for 10 seconds, with traffic flowing amongst nodes during this period.
Post-Simulation Analysis:
- Trace File Analysis:
- The trace file (out.tr) will contain details about packet transmission, delivery, delay, and throughput. Analyze how traffic flows in the flat topology by assessing the trace file.
- NAM Visualization:
- NAM (Network Animator) is used to visualize the flat topology. The peer-to-peer connections amongst nodes and the flow of traffic amidst them will be visible.
- Performance Metrics:
- Compute network performance metrics includes delay, throughput, and packet loss to measure the performance of the flat topology.
Example of Flat Wireless Topology in NS2:
In a wireless flat topology, nodes communicate directly with one another devoid of a central access point, alike to an ad-hoc network. Here’s how you can simulate a flat wireless topology in NS2:
# Create a new simulator
set ns [new Simulator]
# Set up the wireless channel
set chan [new Channel/WirelessChannel]
# Define a propagation model
set prop [new Propagation/TwoRayGround]
# Define topography
set topo [new Topography]
$topo load_flatgrid 500 500
# Define node configuration
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType $prop \
-phyType Phy/WirelessPhy \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
# Create wireless nodes (6 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 node positions (X, Y coordinates)
$n0 set X_ 100 ; $n0 set Y_ 200
$n1 set X_ 150 ; $n1 set Y_ 200
$n2 set X_ 200 ; $n2 set Y_ 250
$n3 set X_ 250 ; $n3 set Y_ 300
$n4 set X_ 300 ; $n4 set Y_ 350
$n5 set X_ 350 ; $n5 set Y_ 400
# Define TCP agents for communication
# Traffic from n0 to n5
set tcp0 [new Agent/TCP]
set sink0 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n5 $sink0
$ns connect $tcp0 $sink0
# Simulate FTP traffic from n0 to n5
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 “$ftp0 start”
$ns at 3.0 “$ftp0 stop”
# Traffic from n2 to n3
set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $tcp1
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1
# Simulate FTP traffic from n2 to n3
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 4.0 “$ftp1 start”
$ns at 6.0 “$ftp1 stop”
# End the simulation after 10 seconds
$ns at 10.0 “finish”
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation
$ns run
Explanation of the Wireless Flat Topology:
- Wireless Nodes:
- Six wireless nodes (n0, n1, n2, n3, n4, and n5) are generated, and their positions are stated.
- Routing Protocol:
- The ad-hoc routing protocol AODV is used, permitting nodes to discover routes dynamically in the flat wireless network.
- Traffic Simulation:
- The same traffic generation and TCP agents are used as in the wired flat topology, replicating communication amongst wireless nodes.
Enhancing the Simulation:
- Adding More Nodes:
- Replicate a larger flat network by attaching more nodes.
- Simulating Mobility:
- Allow random motion for nodes by using $n($i) random-motion 1 to simulate mobile nodes in a flat ad-hoc network.
- Changing Traffic Types:
- Exchange TCP with UDP or simulate various traffic patterns like CBR (Constant Bit Rate).
- Varying Link Parameters:
- Experiment with various link parameters like bandwidth, delay, and queue types (DropTail, RED) to learn how performance varies.
Overall, we were guided you to obtain the knowledge regarding the implementation of Flat topology using ns2 simulation tool with snippet codes. This topology is very helpful in ad hoc network due to this simulation generate several nodes and linked with one another and you can also extend it for further enhancements.
We help with Flat Topology in NS2 implementation. Our team offers the best support for your topics. You can count on our developers to finish your work on time.