To implement a peer-to-peer (P2P) topology in ns3, we need to set up a network where each node can communicate directly with every other node.
Here are the steps to achieve this using ns3.
Steps for implementation
Step 1: Install ns-3
Make sure that ns3 is installed in the computer. If not, install it.
Step 2: Create a New Simulation Script
For your simulation, create a new C++ script.
Step 3: Include Necessary Headers
include the necessary ns3 headers in your script.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
Step 4: Set Up the P2P Topology
Below is an example to set up a P2P topology with four nodes where each node is connected to every other node.
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“P2PTopology”);
int main (int argc, char *argv[])
{
// Configure command line parameters
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (4); // Create 4 nodes
// Create point-to-point links
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
// Install point-to-point devices and assign IP addresses
NetDeviceContainer devices;
Ipv4AddressHelper address;
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
for (uint32_t j = i + 1; j < nodes.GetN (); ++j)
{
NodeContainer pair (nodes.Get (i), nodes.Get (j));
NetDeviceContainer devicePair = pointToPoint.Install (pair);
devices.Add (devicePair);
std::ostringstream subnet;
subnet << “10.” << i << “.” << j << “.0”;
address.SetBase (subnet.str ().c_str (), “255.255.255.0”);
address.Assign (devicePair);
}
}
// Install the internet stack
InternetStackHelper stack;
stack.Install (nodes);
// Set up applications (e.g., UDP echo server and client)
uint16_t port = 9; // Port number for applications
// Install UDP Echo Server on node 0
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Install UDP Echo Client on node 1 to communicate with node 0
UdpEchoClientHelper echoClient (Ipv4Address (“10.0.1.1”), port); // Adjust the IP address based on assigned addresses
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Run simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 5: Build and Run the Simulation
Save the script as p2p-topology.cc and build the script using waf, then run the simulation.
./waf configure –enable-examples
./waf build
./waf –run scratch/p2p-topology
Explanation of the script
- Node Creation: For the P2P network, creates four nodes.
- Point-to-Point Links: With specified data rate and delay attributes, configures point-to-point links.
- Network Configuration: To each pair of nodes, assigns IP addresses and set up point-to-point devices.
- Internet Stack: On all nodes, installs the internet stack.
- Applications: On one node 0, sets up a UDP echo server and a UDP echo client on node 1 to demonstrate communication.
On the whole, we had a performance analysis on the implementation of peer-to-peer topology by setting up a network where each node can communicate directly with every other node. Also, we provide a detailed explanation on Peer-To-Peer Topology for your project.