A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-ipv4-example.cc
Go to the documentation of this file.
1/*
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Author: Hossam Khader <hossamkhader@gmail.com>
17 */
18
19#include "ns3/acoustic-modem-energy-model-helper.h"
20#include "ns3/basic-energy-source-helper.h"
21#include "ns3/core-module.h"
22#include "ns3/energy-source-container.h"
23#include "ns3/internet-module.h"
24#include "ns3/mobility-helper.h"
25#include "ns3/mobility-model.h"
26#include "ns3/node-container.h"
27#include "ns3/uan-channel.h"
28#include "ns3/uan-helper.h"
29
30using namespace ns3;
31
32/**
33 * \ingroup uan
34 *
35 * This example shows the usage of UDP over IPv4 to transfer data.
36 * Two nodes are sending their remaining energy percentage (1 byte)
37 * to a gateway node, that prints the received data.
38 * The transmissions are scheduled at random times to avoid collisions
39 *
40 */
41
42NS_LOG_COMPONENT_DEFINE("UanIpv4Example");
43
44class UanExperiment
45{
46 public:
48
49 /**
50 * Set the UAN nodes position
51 */
53
54 /**
55 * Set the UAN nodes energy
56 */
58
59 /**
60 * Set the UAN nodes communication channels
61 */
63
64 /**
65 * Set the UAN nodes communication channels
66 */
68
69 /**
70 * Send a packet from all the nodes
71 */
73
74 /**
75 * Send a packet from one of the nodes
76 * \param node The sending node
77 * \param pkt The packet
78 * \param dst the destination
79 */
81
82 /**
83 * Print the received packet
84 * \param socket The receiving socket
85 */
87
88 /**
89 * Prepare the experiment
90 */
91 void Prepare();
92
93 /**
94 * Teardown the experiment
95 */
96 void Teardown();
97
98 private:
99 NodeContainer m_nodes; //!< UAN nodes
100 std::map<Ptr<Node>, Ptr<Socket>> m_sockets; //!< send and receive sockets
101};
102
104{
105}
106
107void
109{
110 MobilityHelper mobilityHelper;
111 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
112 mobilityHelper.Install(m_nodes);
113 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
114 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
115 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
116}
117
118void
120{
121 BasicEnergySourceHelper energySourceHelper;
122 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
123 energySourceHelper.Install(m_nodes);
124}
125
126void
128{
129 Ptr<UanChannel> channel = CreateObject<UanChannel>();
130 UanHelper uanHelper;
131 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
132 EnergySourceContainer energySourceContainer;
133 auto node = m_nodes.Begin();
134 while (node != m_nodes.End())
135 {
136 energySourceContainer.Add((*node)->GetObject<EnergySourceContainer>()->Get(0));
137 node++;
138 }
139 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
140 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
141
142 InternetStackHelper internetStackHelper;
143 internetStackHelper.Install(m_nodes);
144
145 Ipv4AddressHelper ipv4AddressHelper;
146 ipv4AddressHelper.SetBase("10.0.0.0", "255.255.255.0");
147 ipv4AddressHelper.Assign(netDeviceContainer);
148 node = m_nodes.Begin();
149 while (node != m_nodes.End())
150 {
151 (*node)->GetObject<Ipv4L3Protocol>()->GetInterface(1)->GetArpCache()->SetWaitReplyTimeout(
152 Seconds(10));
153 node++;
154 }
155}
156
157void
159{
160 Address srcAddress;
161 while (socket->GetRxAvailable() > 0)
162 {
163 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
164 uint8_t energyReading;
165 packet->CopyData(&energyReading, 1);
166
167 if (InetSocketAddress::IsMatchingType(srcAddress))
168 {
169 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
170 << " | Node: "
171 << InetSocketAddress::ConvertFrom(srcAddress).GetIpv4()
172 << " | Energy: " << +energyReading << "%");
173 }
174 }
175}
176
177void
179{
180 auto node = m_nodes.Begin();
181 while (node != m_nodes.End())
182 {
183 m_sockets[*node] =
184 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::UdpSocketFactory"));
185 if ((*node)->GetObject<Ipv4>())
186 {
188 m_sockets[*node]->Bind(ipv4_local);
189 }
190
191 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
192 node++;
193 }
194}
195
196void
198{
199 Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable>();
200
201 auto node = m_nodes.Begin();
202 Ipv4Address dst =
203 (*node)->GetObject<Ipv4L3Protocol>()->GetInterface(1)->GetAddress(0).GetLocal();
204 node++;
205 while (node != m_nodes.End())
206 {
207 uint8_t energy =
208 ((*node)->GetObject<EnergySourceContainer>()->Get(0)->GetEnergyFraction()) * 100;
209
210 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
211
212 double time = uniformRandomVariable->GetValue(0, 60);
213 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
214 node++;
215 }
217}
218
219void
221{
222 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
223 << " packet sent to " << dst);
224 InetSocketAddress ipv4_destination = InetSocketAddress(dst, 9);
225 m_sockets[node]->SendTo(pkt, 0, ipv4_destination);
226}
227
228void
230{
231 m_nodes.Create(3);
233 SetupEnergy();
236 SendPackets();
237}
238
239void
241{
242 for (auto socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
243 {
244 socket->second->Close();
245 }
246}
247
248int
249main(int argc, char* argv[])
250{
251 CommandLine cmd(__FILE__);
252 cmd.Parse(argc, argv);
253
255 experiment.Prepare();
256
260
261 experiment.Teardown();
262
263 return 0;
264}
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:101
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
Parse command-line arguments.
Definition: command-line.h:232
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Holds a vector of ns3::EnergySource pointers.
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
void Add(EnergySourceContainer container)
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.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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:522
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
UAN configuration helper.
Definition: uan-helper.h:42
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:145
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:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1283
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1295
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:706
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88