Visualizing a Network Simulaiton with NetAnim
Section outline
-
The Following is a simple ns-3 simulation that creates a point-to-point link between two nodes, runs a UDP echo client-server application, and generates a visualization file for NetAnim. Follow the comments to understand each step.
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/netanim-module.h" using namespace ns3; int main(int argc, char *argv[]) { // Create a container to hold our two nodes NodeContainer nodes; nodes.Create(2); // Create a point-to-point channel and configure its attributes PointToPointHelper channel; channel.SetDeviceAttribute("DataRate", StringValue("5Mbps")); channel.SetChannelAttribute("Delay", StringValue("2ms")); // Install the channel and devices on our nodes NetDeviceContainer netDevices; netDevices = channel.Install(nodes); // Install the Internet stack (TCP/IP) on our nodes InternetStackHelper ipStack; ipStack.Install(nodes); // Assign IP addresses to the devices Ipv4AddressHelper ipAddresses; ipAddresses.SetBase("192.168.1.0", "255.255.255.0"); Ipv4InterfaceContainer ipinterfaces = ipAddresses.Assign(netDevices); // Set up a UDP Echo Server on node 1, listening on port 9 UdpEchoServerHelper udpEchoServer(9); ApplicationContainer serverApps = udpEchoServer.Install(nodes.Get(1)); serverApps.Start(Seconds(1.0)); // Set up a UDP Echo Client on node 0 UdpEchoClientHelper udpEchoClient(ipinterfaces.GetAddress(1), 9); ApplicationContainer clientApps = udpEchoClient.Install(nodes.Get(0)); clientApps.Start(Seconds(2.0)); // Set up NetAnim to visualize the simulation AnimationInterface anim("SimpleNS3Simulation_NetAnimationOutput.xml"); anim.SetConstantPosition(nodes.Get(0), 0, 5); anim.SetConstantPosition(nodes.Get(1), 10, 5); // Run the simulation Simulator::Run(); Simulator::Destroy(); return 0; }
Step-by-Step Explanation
1. Setup and Topology
The script first sets up the fundamental elements of the simulation.
-
Header Files: Necessary modules for core functionality, networking, point-to-point links, the internet stack, applications, mobility, and NetAnim are included.
-
Node Creation: A
NodeContaineris used to create two virtual nodes, which act as the endpoints of our network. -
Channel and Device Configuration: A
PointToPointHelpersets up a communication channel with a specifiedDataRate(5 Mbps) andDelay(2 ms) and installs network devices on the nodes
2. Network Stack and Addressing
This section transforms the generic nodes into functional network devices.
-
IP Stack Installation: The
InternetStackHelperinstalls the full IP stack on both nodes, enabling them to use IP addresses and handle protocols like TCP and UDP. -
IP Address Assignment: An
Ipv4AddressHelperassigns IP addresses from the192.168.1.0/24subnet to the network devices, making them addressable.
3. Application Layer
This part configures the actual data traffic for the simulation.
-
UDP Echo Server: A
UdpEchoServerHelpersets up an application on the second node to listen for UDP packets on port 9. -
UDP Echo Client: A
UdpEchoClientHelpersets up a client on the first node to send packets to the server's IP address. -
Event Scheduling: Both applications are scheduled to start at different times. The server starts at 1 second, ensuring it's ready to receive before the client begins sending at 2 seconds.
4. Mobility and Visualization
This is a key addition to the basic simulation, allowing for a visual representation of the network.
-
Mobility Helper (Commented): The
MobilityHelpersection is included in your original code but is redundant and commented out.ConstantPositionMobilityModelis used to set static positions, but the subsequentNetAnimcalls override this. -
AnimationInterface: An
AnimationInterfaceobject is created, which will generate an XML file (SimpleNS3Simulation_NetAnimationOutput.xml) containing all the events needed for the NetAnim visualizer. -
Static Positioning: The
SetConstantPosition()function explicitly sets the coordinates for each node on the NetAnim canvas, with Node 0 at(0, 5)and Node 1 at(10, 5).
5. Start Running the Simulation
-
Running the Simulation:
Simulator::Run()starts the simulation, processing all scheduled events. -
Cleanup:
Simulator::Destroy()is a good practice to free up all memory and objects, ensuring a clean exit. -
Return Value: The
return 0;statement indicates a successful program execution.
-