How to Implement Secondary User Routing in NS2

To implement Secondary User Routing in NS2 has a series of steps to follow and it is usually denotes to replicate Cognitive Radio Networks (CRNs) or networks in which secondary users (SUs) enthusiastically access the spectrum when the Primary Users (PUs) are not using it. In this scenario, secondary users must make sure that their interference does not interfere with primary users, and they need to select routes dynamically based on spectrum availability.

We develop Cognitive Radio Networks (CRNs) for your projects; ns2project.com implement your projects in best outcomes drop us all your needs for best results.

Below are given procedure to implementing Secondary User Routing in NS2:

Key Steps to Implement Secondary User Routing:

  1. Set up primary and secondary users in the simulation.
  2. Simulate spectrum availability based on the existence or absence of primary users.
  3. Route selection for secondary users based on the dynamic availability of channels.
  4. Traffic setup for secondary users to validate the routing mechanism.

Step-by-Step Implementation:

  1. NS2 Setup for Cognitive Radio Networks

We initiate by configuring NS2 with a combination of primary users (PUs) and secondary users (SUs). We will setup primary users to occupy certain channels and secondary users to route their traffic on channels that are not occupied by primary users.

# Create an NS2 simulator instance

set ns [new Simulator]

# Open trace files to log the simulation

set tracefile [open “secondary_user_routing.tr” w]

set namfile [open “secondary_user_routing.nam” w]

$ns trace-all $tracefile

$ns namtrace-all-wireless $namfile 1000 1000

# Define the wireless channel for communication

set chan_1_ [new Channel/WirelessChannel]

 

# Define topography for wireless communication

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Define the propagation and network parameters

set prop [new Propagation/TwoRayGround]   ;# Propagation model

set netif [new Phy/WirelessPhy]           ;# Wireless physical layer

set mac [new Mac/802_11]                  ;# MAC protocol

set ll [new LL]                           ;# Link layer

set ifq [new Queue/DropTail/PriQueue]     ;# Priority queue for scheduling

set ant [new Antenna/OmniAntenna]         ;# Omni-directional antenna

# Configure node settings (for both primary and secondary users)

$ns node-config -llType $ll \

-macType $mac \

-ifqType $ifq \

-ifqLen 50 \

-antType $ant \

-propType $prop \

-phyType $netif \

-channel $chan_1_ \

-topoInstance $topo

  1. Defining Primary and Secondary Users

In a cognitive radio network, Primary Users (PUs) have the right to access the spectrum, and Secondary Users (SUs) can only use the spectrum when it is unoccupied by the PUs. In this sample, we will describe a few PUs and SUs.

Example: Define Primary and Secondary Users

# Define Primary Users (PUs)

set pu1 [$ns node]

set pu2 [$ns node]

# Define Secondary Users (SUs)

set su1 [$ns node]

set su2 [$ns node]

set su3 [$ns node]

# Position the nodes

$pu1 set X_ 200

$pu1 set Y_ 200

$pu2 set X_ 600

$pu2 set Y_ 600

$su1 set X_ 100

$su1 set Y_ 100

$su2 set X_ 300

$su2 set Y_ 300

$su3 set X_ 500

$su3 set Y_ 500

  1. Simulating Spectrum Availability

To replicate spectrum availability, we assume that PUs occupy the particular channels at certain times. SUs must sense the spectrum and control which channel to use for communication.

Example: Simulate Primary User Occupying the Channel

# Define channel occupancy for PUs

proc occupy_channel {pu start_time stop_time} {

global ns

$ns at $start_time “puts Primary User $pu occupies the channel”

$ns at $stop_time “puts Primary User $pu releases the channel”

}

# PU1 occupies the channel from 1.0 to 10.0 seconds

occupy_channel $pu1 1.0 10.0

  1. Routing for Secondary Users Based on Spectrum Sensing

Secondary users will transmit their traffic on available channels when the primary users are not occupying them. We replicate this by dynamically prioritizing the routes for the secondary users.

Example: Implementing Dynamic Routing for Secondary Users

We can execute this by manually validating the availability of channels and selects an alternative route when the primary user occupies the channel.

# Procedure to select a route for secondary users based on spectrum availability

proc check_channel_availability {su src dest pu start_time stop_time} {

global ns

# Check if the primary user is occupying the channel

if {[ns now] < $start_time || [ns now] > $stop_time} {

puts “Secondary User $su can use the channel from $src to $dest”

$ns at 1.0 “$su start_communication $src $dest”

} else {

puts “Primary User occupies the channel. SU $su cannot communicate.”

$ns at 1.0 “check_alternative_route $su $src $dest”

}

}

# Example of starting communication between su1 and su2 when PU1 is not using the channel

$ns at 2.0 “check_channel_availability $su1 $su1 $su2 $pu1 1.0 10.0”

In this setup, the secondary user validates whether the primary user inhabits the channel already sending the traffic. If the channel is occupied, the secondary user will effort to identify an alternative route.

  1. Setting up Traffic for Secondary Users

Now we mimic the secondary users transmitting data when the spectrum is available. We can use UDP traffic to mimic video or real-time streaming.

Example: UDP Traffic for Secondary Users

# Create UDP agents and CBR traffic for secondary user communication

set udp_su1 [new Agent/UDP]

set udp_su2 [new Agent/Null]

# Attach the agents to secondary user nodes

$ns attach-agent $su1 $udp_su1

$ns attach-agent $su2 $udp_su2

# Connect the UDP agent on su1 to the null agent on su2

$ns connect $udp_su1 $udp_su2

# Create CBR traffic to simulate continuous data transmission between SUs

set cbr_su [new Application/Traffic/CBR]

$cbr_su attach-agent $udp_su1

$cbr_su set packetSize_ 512        ;# Packet size for SU traffic

$cbr_su set rate_ 500Kb            ;# Data rate for SU traffic

# Start and stop traffic based on spectrum availability

$ns at 3.0 “$cbr_su start”

$ns at 12.0 “$cbr_su stop”

  1. Running the Simulation

Describe the simulation duration and execute the simulation.

# Set the simulation end time

$ns at 30.0 “finish”

# Finish procedure to close trace files and end simulation

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exit 0

}

# Run the simulation

$ns run

  1. Analysing the Results

After the simulation executes, we can evaluate the trace files and network animation (NAM) to see how the secondary users enthusiastically adjust to spectrum availability:

  • Throughput: Extent how much data the secondary users successfully transmitted when the channel was available.
  • Latency: Check the delay in secondary user communication owing to spectrum sensing and dynamic routing.
  • Packet Loss: Regulate if any packets were dropped when secondary users had to shift routes because of primary user interference.

In this given module, we had explicitly focussed the novel information on how to execute the secondary user routing that were executed using ns2 tool that is used to select the best path based on the spectrum availability. If you need more information regarding this process we will explain it based on your needs.