A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-ipv4-example.cc
Go to the documentation of this file.
1/*
2 *
3 * SPDX-License-Identifier: GPL-2.0-only
4 *
5 * Author: Hossam Khader <hossamkhader@gmail.com>
6 */
7
8#include "ns3/acoustic-modem-energy-model-helper.h"
9#include "ns3/basic-energy-source-helper.h"
10#include "ns3/core-module.h"
11#include "ns3/energy-source-container.h"
12#include "ns3/internet-module.h"
13#include "ns3/mobility-helper.h"
14#include "ns3/mobility-model.h"
15#include "ns3/node-container.h"
16#include "ns3/uan-channel.h"
17#include "ns3/uan-helper.h"
18
19using namespace ns3;
20using namespace ns3::energy;
21
22/**
23 * \ingroup uan
24 *
25 * This example shows the usage of UDP over IPv4 to transfer data.
26 * Two nodes are sending their remaining energy percentage (1 byte)
27 * to a gateway node, that prints the received data.
28 * The transmissions are scheduled at random times to avoid collisions
29 *
30 */
31
32NS_LOG_COMPONENT_DEFINE("UanIpv4Example");
33
34class UanExperiment
35{
36 public:
38
39 /**
40 * Set the UAN nodes position
41 */
43
44 /**
45 * Set the UAN nodes energy
46 */
48
49 /**
50 * Set the UAN nodes communication channels
51 */
53
54 /**
55 * Set the UAN nodes communication channels
56 */
58
59 /**
60 * Send a packet from all the nodes
61 */
63
64 /**
65 * Send a packet from one of the nodes
66 * \param node The sending node
67 * \param pkt The packet
68 * \param dst the destination
69 */
71
72 /**
73 * Print the received packet
74 * \param socket The receiving socket
75 */
77
78 /**
79 * Prepare the experiment
80 */
81 void Prepare();
82
83 /**
84 * Teardown the experiment
85 */
86 void Teardown();
87
88 private:
89 NodeContainer m_nodes; //!< UAN nodes
90 std::map<Ptr<Node>, Ptr<Socket>> m_sockets; //!< send and receive sockets
91};
92
94{
95}
96
97void
99{
100 MobilityHelper mobilityHelper;
101 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
102 mobilityHelper.Install(m_nodes);
103 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
104 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
105 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
106}
107
108void
110{
111 BasicEnergySourceHelper energySourceHelper;
112 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
113 energySourceHelper.Install(m_nodes);
114}
115
116void
118{
120 UanHelper uanHelper;
121 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
122 EnergySourceContainer energySourceContainer;
123 auto node = m_nodes.Begin();
124 while (node != m_nodes.End())
125 {
126 energySourceContainer.Add((*node)->GetObject<EnergySourceContainer>()->Get(0));
127 node++;
128 }
129 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
130 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
131
132 InternetStackHelper internetStackHelper;
133 internetStackHelper.Install(m_nodes);
134
135 Ipv4AddressHelper ipv4AddressHelper;
136 ipv4AddressHelper.SetBase("10.0.0.0", "255.255.255.0");
137 ipv4AddressHelper.Assign(netDeviceContainer);
138 node = m_nodes.Begin();
139 while (node != m_nodes.End())
140 {
141 (*node)->GetObject<Ipv4L3Protocol>()->GetInterface(1)->GetArpCache()->SetWaitReplyTimeout(
142 Seconds(10));
143 node++;
144 }
145}
146
147void
149{
150 Address srcAddress;
151 while (socket->GetRxAvailable() > 0)
152 {
153 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
154 uint8_t energyReading;
155 packet->CopyData(&energyReading, 1);
156
157 if (InetSocketAddress::IsMatchingType(srcAddress))
158 {
159 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
160 << " | Node: "
161 << InetSocketAddress::ConvertFrom(srcAddress).GetIpv4()
162 << " | Energy: " << +energyReading << "%");
163 }
164 }
165}
166
167void
169{
170 auto node = m_nodes.Begin();
171 while (node != m_nodes.End())
172 {
173 m_sockets[*node] =
174 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::UdpSocketFactory"));
175 if ((*node)->GetObject<Ipv4>())
176 {
178 m_sockets[*node]->Bind(ipv4_local);
179 }
180
181 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
182 node++;
183 }
184}
185
186void
188{
190
191 auto node = m_nodes.Begin();
192 Ipv4Address dst =
193 (*node)->GetObject<Ipv4L3Protocol>()->GetInterface(1)->GetAddress(0).GetLocal();
194 node++;
195 while (node != m_nodes.End())
196 {
197 uint8_t energy =
198 ((*node)->GetObject<EnergySourceContainer>()->Get(0)->GetEnergyFraction()) * 100;
199
200 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
201
202 double time = uniformRandomVariable->GetValue(0, 60);
203 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
204 node++;
205 }
207}
208
209void
211{
212 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
213 << " packet sent to " << dst);
214 InetSocketAddress ipv4_destination = InetSocketAddress(dst, 9);
215 m_sockets[node]->SendTo(pkt, 0, ipv4_destination);
216}
217
218void
220{
221 m_nodes.Create(3);
223 SetupEnergy();
226 SendPackets();
227}
228
229void
231{
232 for (auto socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
233 {
234 socket->second->Close();
235 }
236}
237
238int
239main(int argc, char* argv[])
240{
241 CommandLine cmd(__FILE__);
242 cmd.Parse(argc, argv);
243
246
250
251 experiment.Teardown();
252
253 return 0;
254}
This example shows the usage of UDP over 6LoWPAN to transfer data.
void Teardown()
Teardown the experiment.
void PrintReceivedPacket(Ptr< Socket > socket)
Print the received packet.
NodeContainer m_nodes
UAN nodes.
void Prepare()
Prepare the experiment.
void SendSinglePacket(Ptr< Node > node, Ptr< Packet > pkt, Ipv6Address dst)
Send a packet from one of the nodes.
void SendPackets()
Send a packet from all the nodes.
void SetupCommunications()
Set the UAN nodes communication channels.
void SetupPositions()
Set the UAN nodes position.
std::map< Ptr< Node >, Ptr< Socket > > m_sockets
send and receive sockets
void SetupApplications()
Set the UAN nodes communication channels.
void SetupEnergy()
Set the UAN nodes energy.
Assign AcousticModemEnergyModel to uan devices.
a polymophic address class
Definition address.h:90
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
Parse command-line arguments.
energy::DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< energy::EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
energy::EnergySourceContainer Install(Ptr< Node > node) const
an Inet address class
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
Ipv4Address GetLocal() const
Get the local address.
Implement the IPv4 layer.
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
UAN configuration helper.
Definition uan-helper.h:31
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Holds a vector of ns3::EnergySource pointers.
void Add(EnergySourceContainer container)
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
void experiment(std::string queue_disc_type)
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time Days(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1272
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1284
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
channel
Definition third.py:77