To implement the token ring topology in ns3 is used to make sure that only node can transfer the data at a time that has to generate the network where each node is connected in a ring, and using token passing mechanism. Here we deliver the brief procedure on how to implement the token ring topology in ns3;
Step-by-Step Implementation:
Step 1: Install ns3
- Make certain ns3 is installed in the computer.
Step 2: Create a New Simulation Script
- Create a new C++ script for your simulation.
Step 3: Include Necessary Headers
- In your script, include the necessary ns-3 headers:
#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”
#include “ns3/packet-sink-helper.h”
Step 4: Set Up the Token Ring Topology
In this direction, we provide the sample on how to setup the token ring topology with four nodes:
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“TokenRingTopology”);
class TokenRingApplication : public Application
{
public:
TokenRingApplication ();
virtual ~TokenRingApplication ();
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate, uint32_t nodeId, uint32_t nNodes);
private:
virtual void StartApplication (void);
virtual void StopApplication (void);
void ScheduleTx (void);
void SendPacket (void);
void ReceivePacket (Ptr<Socket> socket);
Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
DataRate m_dataRate;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetsSent;
uint32_t m_nodeId;
uint32_t m_nNodes;
bool m_token;
};
TokenRingApplication::TokenRingApplication ()
: m_socket (0),
m_peer (),
m_packetSize (0),
m_nPackets (0),
m_dataRate (0),
m_sendEvent (),
m_running (false),
m_packetsSent (0),
m_nodeId (0),
m_nNodes (0),
m_token (false)
{
}
TokenRingApplication::~TokenRingApplication ()
{
m_socket = 0;
}
void
TokenRingApplication::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate, uint32_t nodeId, uint32_t nNodes)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
m_nodeId = nodeId;
m_nNodes = nNodes;
}
void
TokenRingApplication::StartApplication (void)
{
m_running = true;
m_packetsSent = 0;
m_socket->Bind ();
m_socket->Connect (m_peer);
m_socket->SetRecvCallback (MakeCallback (&TokenRingApplication::ReceivePacket, this));
if (m_nodeId == 0)
{
m_token = true;
ScheduleTx ();
}
}
void
TokenRingApplication::StopApplication (void)
{
m_running = false;
if (m_sendEvent.IsRunning ())
{
Simulator::Cancel (m_sendEvent);
}
if (m_socket)
{
m_socket->Close ();
}
}
void
TokenRingApplication::ScheduleTx (void)
{
if (m_running && m_token)
{
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
m_sendEvent = Simulator::Schedule (tNext, &TokenRingApplication::SendPacket, this);
}
}
void
TokenRingApplication::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_socket->Send (packet);
if (++m_packetsSent < m_nPackets)
{
ScheduleTx ();
}
else
{
m_token = false;
Address nextAddress (InetSocketAddress (Ipv4Address::ConvertFrom (m_peer).Get () + 1, 9));
TokenRingApplication::Setup (m_socket, nextAddress, m_packetSize, m_nPackets, m_dataRate, m_nodeId, m_nNodes);
}
}
void
TokenRingApplication::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet = socket->Recv ();
m_token = true;
ScheduleTx ();
}
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 for the Token Ring network
// Create point-to-point links to form a ring
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
Ipv4AddressHelper address;
Ipv4InterfaceContainer interfaces;
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
NodeContainer pair (nodes.Get (i), nodes.Get ((i + 1) % nodes.GetN ()));
NetDeviceContainer devicePair = pointToPoint.Install (pair);
devices.Add (devicePair);
std::ostringstream subnet;
subnet << “10.1.” << i << “.0”;
address.SetBase (subnet.str ().c_str (), “255.255.255.0”);
interfaces.Add (address.Assign (devicePair));
}
// Install the internet stack
InternetStackHelper stack;
stack.Install (nodes);
// Create and install TokenRing applications
uint32_t packetSize = 1024; // Packet size in bytes
uint32_t numPackets = 1; // Number of packets to send
DataRate dataRate (“1Mbps”); // Data rate
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (i), TcpSocketFactory::GetTypeId ());
Address nextAddress (InetSocketAddress (interfaces.GetAddress ((i + 1) % nodes.GetN ()), 9));
Ptr<TokenRingApplication> app = CreateObject<TokenRingApplication> ();
app->Setup (ns3TcpSocket, nextAddress, packetSize, numPackets, dataRate, i, nodes.GetN ());
nodes.Get (i)->AddApplication (app);
app->SetStartTime (Seconds (1.0));
app->SetStopTime (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 token-ring-topology.cc.
- Build the script using waf:
./waf configure –enable-examples
./waf build
- Run the simulation:
./waf –run scratch/token-ring-topology
Explanation of the Script
- Node Creation: Creates four nodes for the Token Ring network.
- Point-to-Point Links: Configures point-to-point links to form a ring topology, ensuring each node is connected to the next, with the last node connecting back to the first.
- Internet Stack: Installs the internet stack on all nodes.
- IP Addressing: Assigns IP addresses to the network links.
- Token Ring Application: Defines a custom application class to implement token passing logic. Each node can only transmit data when it has the token.
- Applications: Sets up the Token Ring applications on each node, where node 0 starts with the token.
Finally, we discussed and provide the information about how to implement the token ring topology in ns3 simulation tool and further us also delivers all kinds of information for token ring topology in ns3.
If you encounter difficulties in implementing Token Ring Topology in ns3 even after studying it, please do not hesitate to contact us at ns3simulation.com. Our developers are ready to provide you with effective solutions.