A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-raw-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/packet-socket-address.h"
17#include "ns3/packet-socket-helper.h"
18#include "ns3/uan-channel.h"
19#include "ns3/uan-helper.h"
20
21using namespace ns3;
22
23/**
24 * \ingroup uan
25 *
26 * This example shows the usage of raw packets transfer data.
27 * Two nodes are sending their remaining energy percentage (1 byte)
28 * to a gateway node, that prints the received data.
29 * The transmissions are scheduled at random times to avoid collisions
30 *
31 */
32
33NS_LOG_COMPONENT_DEFINE("UanRawExample");
34
35class UanExperiment
36{
37 public:
39
40 /**
41 * Set the UAN nodes position
42 */
44
45 /**
46 * Set the UAN nodes energy
47 */
49
50 /**
51 * Set the UAN nodes communication channels
52 */
54
55 /**
56 * Set the UAN nodes communication channels
57 */
59
60 /**
61 * Send a packet from all the nodes
62 */
64
65 /**
66 * Send a packet from one of the nodes
67 * \param node The sending node
68 * \param pkt The packet
69 * \param dst the destination
70 */
72
73 /**
74 * Print the received packet
75 * \param socket The receiving socket
76 */
78
79 /**
80 * Prepare the experiment
81 */
82 void Prepare();
83
84 /**
85 * Teardown the experiment
86 */
87 void Teardown();
88
89 private:
90 NodeContainer m_nodes; //!< UAN nodes
91 std::map<Ptr<Node>, Ptr<Socket>> m_sockets; //!< send and receive sockets
92};
93
95{
96}
97
98void
100{
101 MobilityHelper mobilityHelper;
102 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
103 mobilityHelper.Install(m_nodes);
104 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
105 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
106 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
107}
108
109void
111{
112 BasicEnergySourceHelper energySourceHelper;
113 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
114 energySourceHelper.Install(m_nodes);
115}
116
117void
119{
121 UanHelper uanHelper;
122 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
123 energy::EnergySourceContainer energySourceContainer;
124 auto node = m_nodes.Begin();
125 while (node != m_nodes.End())
126 {
127 energySourceContainer.Add((*node)->GetObject<energy::EnergySourceContainer>()->Get(0));
128 node++;
129 }
130 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
131 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
132}
133
134void
136{
137 Address srcAddress;
138 while (socket->GetRxAvailable() > 0)
139 {
140 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
141 PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom(srcAddress);
142 srcAddress = packetSocketAddress.GetPhysicalAddress();
143 uint8_t energyReading;
144 packet->CopyData(&energyReading, 1);
145
146 if (Mac8Address::IsMatchingType(srcAddress))
147 {
148 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
149 << " | Node: " << Mac8Address::ConvertFrom(srcAddress)
150 << " | Energy: " << +energyReading << "%");
151 }
152 }
153}
154
155void
157{
158 auto node = m_nodes.Begin();
159 PacketSocketHelper packetSocketHelper;
160 while (node != m_nodes.End())
161 {
162 packetSocketHelper.Install(*node);
163 PacketSocketAddress socketAddress;
164 socketAddress.SetSingleDevice((*node)->GetDevice(0)->GetIfIndex());
165 socketAddress.SetProtocol(0);
166 m_sockets[*node] =
167 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::PacketSocketFactory"));
168 m_sockets[*node]->Bind();
169 m_sockets[*node]->Connect(socketAddress);
170 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
171 node++;
172 }
173}
174
175void
177{
179
180 auto node = m_nodes.Begin();
181 Mac8Address dst = Mac8Address::ConvertFrom((*node)->GetDevice(0)->GetAddress());
182 node++;
183 while (node != m_nodes.End())
184 {
185 uint8_t energy =
186 ((*node)->GetObject<energy::EnergySourceContainer>()->Get(0)->GetEnergyFraction()) *
187 100;
188
189 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
190
191 double time = uniformRandomVariable->GetValue(0, 60);
192 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
193 node++;
194 }
196}
197
198void
200{
201 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
202 << " packet sent to " << dst);
203 PacketSocketAddress socketAddress;
204 socketAddress.SetSingleDevice(node->GetDevice(0)->GetIfIndex());
205 socketAddress.SetPhysicalAddress(dst);
206 socketAddress.SetProtocol(0);
207 m_sockets[node]->SendTo(pkt, 0, socketAddress);
208}
209
210void
212{
213 m_nodes.Create(3);
215 SetupEnergy();
218 SendPackets();
219}
220
221void
223{
224 for (auto socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
225 {
226 socket->second->Close();
227 }
228}
229
230int
231main(int argc, char* argv[])
232{
233 CommandLine cmd(__FILE__);
234 cmd.Parse(argc, argv);
235
238
242
243 experiment.Teardown();
244
245 return 0;
246}
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
A class used for addressing MAC8 MAC's.
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
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
an address for a packet socket
Address GetPhysicalAddress() const
Get the destination address.
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
static PacketSocketAddress ConvertFrom(const Address &address)
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
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