To implement a mobile sink in ns3, we need to create a scenario. In that scenario one or more mobile nodes should act as sinks, that collects data from other nodes and moves around the network. By this method we can reduce energy consumption on static sensor nodes using mobile sink that collects the data that particularly used in wireless sensor networks (WSNs). The following steps will guide on implementing Mobile sink location in ns3.
Step-by-step guide to implement Mobile sink location:
Step 1: Set Up the ns3 Environment
- Install ns3: Make sure that ns3 is installed on the system.
sudo apt-get update
sudo apt-get install ns3
Create a New ns-3 Project: Create a directory for the new project within the ns-3 workspace.
cd ns-3
mkdir scratch/mobile-sink-project
Step 2: Define the Mobile Sink Node
- Create a New Simulation Script: Create a new script in the scratch directory to implement the simulation scenario.
// mobile-sink-project.cc
#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/mobility-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE(“MobileSinkProject”);
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet;
while ((packet = socket->Recv())) {
NS_LOG_UNCOND(“Received one packet!”);
}
}
void SetupPacketReceive(Ptr<Node> node, Ipv4Address address) {
TypeId tid = TypeId::LookupByName(“ns3::UdpSocketFactory”);
Ptr<Socket> sinkSocket = Socket::CreateSocket(node, tid);
InetSocketAddress local = InetSocketAddress(address, 80);
sinkSocket->Bind(local);
sinkSocket->SetRecvCallback(MakeCallback(&ReceivePacket));
}
int main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(10);
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(5.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(5),
“LayoutType”, StringValue(“RowFirst”));
mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),
“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=0.5]”),
“PositionAllocator”, StringValue(“ns3::GridPositionAllocator”));
mobility.Install(nodes);
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper(aodv);
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(nodes);
// Set up the sink node
Ptr<Node> sinkNode = nodes.Get(0);
Ipv4Address sinkAddress = interfaces.GetAddress(0);
SetupPacketReceive(sinkNode, sinkAddress);
// Install applications
OnOffHelper onOffHelper(“ns3::UdpSocketFactory”, Address());
onOffHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));
onOffHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));
for (uint32_t i = 1; i < nodes.GetN(); ++i) {
AddressValue remoteAddress(InetSocketAddress(sinkAddress, 80));
onOffHelper.SetAttribute(“Remote”, remoteAddress);
ApplicationContainer tempApp = onOffHelper.Install(nodes.Get(i));
tempApp.Start(Seconds(1.0));
tempApp.Stop(Seconds(10.0));
}
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream(“mobile-sink.tr”));
pointToPoint.EnablePcapAll(“mobile-sink”);
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Compile the Script: Compile the script using the waf build system.
./waf build
Run the Simulation: Run the simulation script and observe the results.
./waf –run scratch/mobile-sink-project
Step 3: Implement Mobility for the Sink Node
- Define Mobility Model: Use the MobilityHelper to define the mobility model for the sink node. In this example, we use the RandomWaypointMobilityModel.
Step 4: Set Up Data Collection
- Set Up Packet Reception: Define a callback function to handle packet reception at the sink node. This function will be called whenever the sink node receives a packet.
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet;
while ((packet = socket->Recv())) {
NS_LOG_UNCOND(“Received one packet!”);
}
}
Bind the Socket: Bind a socket to the sink node’s address and set the receive callback.
void SetupPacketReceive(Ptr<Node> node, Ipv4Address address) {
TypeId tid = TypeId::LookupByName(“ns3::UdpSocketFactory”);
Ptr<Socket> sinkSocket = Socket::CreateSocket(node, tid);
InetSocketAddress local = InetSocketAddress(address, 80);
sinkSocket->Bind(local);
sinkSocket->SetRecvCallback(MakeCallback(&ReceivePacket));
}
Step 5: Generate Traffic
- Install Applications: Use the OnOffHelper to generate traffic from other nodes to the sink node.
OnOffHelper onOffHelper(“ns3::UdpSocketFactory”, Address());
onOffHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));
onOffHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));
for (uint32_t i = 1; i < nodes.GetN(); ++i) {
AddressValue remoteAddress(InetSocketAddress(sinkAddress, 80));
onOffHelper.SetAttribute(“Remote”, remoteAddress);
ApplicationContainer tempApp = onOffHelper.Install(nodes.Get(i));
tempApp.Start(Seconds(1.0));
tempApp.Stop(Seconds(10.0));
}
Finally, we all get to know how to implement Mobile sink location by setting up the environment and mobile sink node, implementing mobility for the sink node, setting up data collection and generating the traffic we can analyse the results in ns3.
We’ve got experience working on Mobile Sink Location in ns3simulation across the board. Shoot us a message for further guidance on your implementation. We can provide you with networking comparison analysis. Our team has successfully completed projects on energy consumption of static sensor nodes using mobile sink.