A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-raw-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/packet-socket-address.h"
28#include "ns3/packet-socket-helper.h"
29#include "ns3/uan-channel.h"
30#include "ns3/uan-helper.h"
31
32using namespace ns3;
33
34/**
35 * \ingroup uan
36 *
37 * This example shows the usage of raw packets transfer data.
38 * Two nodes are sending their remaining energy percentage (1 byte)
39 * to a gateway node, that prints the received data.
40 * The transmissions are scheduled at random times to avoid collisions
41 *
42 */
43
44NS_LOG_COMPONENT_DEFINE("UanRawExample");
45
46class UanExperiment
47{
48 public:
50
51 /**
52 * Set the UAN nodes position
53 */
55
56 /**
57 * Set the UAN nodes energy
58 */
60
61 /**
62 * Set the UAN nodes communication channels
63 */
65
66 /**
67 * Set the UAN nodes communication channels
68 */
70
71 /**
72 * Send a packet from all the nodes
73 */
75
76 /**
77 * Send a packet from one of the nodes
78 * \param node The sending node
79 * \param pkt The packet
80 * \param dst the destination
81 */
83
84 /**
85 * Print the received packet
86 * \param socket The receiving socket
87 */
89
90 /**
91 * Prepare the experiment
92 */
93 void Prepare();
94
95 /**
96 * Teardown the experiment
97 */
98 void Teardown();
99
100 private:
101 NodeContainer m_nodes; //!< UAN nodes
102 std::map<Ptr<Node>, Ptr<Socket>> m_sockets; //!< send and receive sockets
103};
104
106{
107}
108
109void
111{
112 MobilityHelper mobilityHelper;
113 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
114 mobilityHelper.Install(m_nodes);
115 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
116 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
117 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
118}
119
120void
122{
123 BasicEnergySourceHelper energySourceHelper;
124 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
125 energySourceHelper.Install(m_nodes);
126}
127
128void
130{
131 Ptr<UanChannel> channel = CreateObject<UanChannel>();
132 UanHelper uanHelper;
133 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
134 EnergySourceContainer energySourceContainer;
135 auto node = m_nodes.Begin();
136 while (node != m_nodes.End())
137 {
138 energySourceContainer.Add((*node)->GetObject<EnergySourceContainer>()->Get(0));
139 node++;
140 }
141 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
142 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
143}
144
145void
147{
148 Address srcAddress;
149 while (socket->GetRxAvailable() > 0)
150 {
151 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
152 PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom(srcAddress);
153 srcAddress = packetSocketAddress.GetPhysicalAddress();
154 uint8_t energyReading;
155 packet->CopyData(&energyReading, 1);
156
157 if (Mac8Address::IsMatchingType(srcAddress))
158 {
159 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
160 << " | Node: " << Mac8Address::ConvertFrom(srcAddress)
161 << " | Energy: " << +energyReading << "%");
162 }
163 }
164}
165
166void
168{
169 auto node = m_nodes.Begin();
170 PacketSocketHelper packetSocketHelper;
171 while (node != m_nodes.End())
172 {
173 packetSocketHelper.Install(*node);
174 PacketSocketAddress socketAddress;
175 socketAddress.SetSingleDevice((*node)->GetDevice(0)->GetIfIndex());
176 socketAddress.SetProtocol(0);
177 m_sockets[*node] =
178 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::PacketSocketFactory"));
179 m_sockets[*node]->Bind();
180 m_sockets[*node]->Connect(socketAddress);
181 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
182 node++;
183 }
184}
185
186void
188{
189 Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable>();
190
191 auto node = m_nodes.Begin();
192 Mac8Address dst = Mac8Address::ConvertFrom((*node)->GetDevice(0)->GetAddress());
193 node++;
194 while (node != m_nodes.End())
195 {
196 uint8_t energy =
197 ((*node)->GetObject<EnergySourceContainer>()->Get(0)->GetEnergyFraction()) * 100;
198
199 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
200
201 double time = uniformRandomVariable->GetValue(0, 60);
202 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
203 node++;
204 }
206}
207
208void
210{
211 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
212 << " packet sent to " << dst);
213 PacketSocketAddress socketAddress;
214 socketAddress.SetSingleDevice(node->GetDevice(0)->GetIfIndex());
215 socketAddress.SetPhysicalAddress(dst);
216 socketAddress.SetProtocol(0);
217 m_sockets[node]->SendTo(pkt, 0, socketAddress);
218}
219
220void
222{
223 m_nodes.Create(3);
225 SetupEnergy();
228 SendPackets();
229}
230
231void
233{
234 for (auto socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
235 {
236 socket->second->Close();
237 }
238}
239
240int
241main(int argc, char* argv[])
242{
243 CommandLine cmd(__FILE__);
244 cmd.Parse(argc, argv);
245
247 experiment.Prepare();
248
252
253 experiment.Teardown();
254
255 return 0;
256}
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
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:44
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:56
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:65
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
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.
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:1326
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1290
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1302
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:704
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88