A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-6lowpan-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/sixlowpan-helper.h"
28#include "ns3/sixlowpan-net-device.h"
29#include "ns3/uan-channel.h"
30#include "ns3/uan-helper.h"
31
32using namespace ns3;
33
43NS_LOG_COMPONENT_DEFINE("Uan6lowpanExample");
44
46{
47 public:
49
53 void SetupPositions();
54
58 void SetupEnergy();
59
64
68 void SetupApplications();
69
73 void SendPackets();
74
82
88
92 void Prepare();
93
97 void Teardown();
98
99 private:
101 std::map<Ptr<Node>, Ptr<Socket>> m_sockets;
102};
103
105{
106}
107
108void
110{
111 MobilityHelper mobilityHelper;
112 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
113 mobilityHelper.Install(m_nodes);
114 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
115 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
116 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
117}
118
119void
121{
122 BasicEnergySourceHelper energySourceHelper;
123 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
124 energySourceHelper.Install(m_nodes);
125}
126
127void
129{
130 Ptr<UanChannel> channel = CreateObject<UanChannel>();
131 UanHelper uanHelper;
132 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
133 EnergySourceContainer energySourceContainer;
135 while (node != m_nodes.End())
136 {
137 energySourceContainer.Add((*node)->GetObject<EnergySourceContainer>()->Get(0));
138 node++;
139 }
140 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
141 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
142
143 SixLowPanHelper sixLowPanHelper;
144 NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install(netDeviceContainer);
145
146 InternetStackHelper internetStackHelper;
147 internetStackHelper.Install(m_nodes);
148
149 Ipv6AddressHelper ipv6AddressHelper;
150 ipv6AddressHelper.SetBase(Ipv6Address("2002::"), Ipv6Prefix(64));
151 ipv6AddressHelper.Assign(sixlowpanNetDevices);
152
153 node = m_nodes.Begin();
154 while (node != m_nodes.End())
155 {
156 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
157 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("ReachableTime",
158 TimeValue(Seconds(3600)));
159 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("RetransmissionTime",
160 TimeValue(Seconds(1000)));
161 node++;
162 }
163}
164
165void
167{
168 Address srcAddress;
169 while (socket->GetRxAvailable() > 0)
170 {
171 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
172 uint8_t energyReading;
173 packet->CopyData(&energyReading, 1);
174
176 {
177 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
178 << " | Node: "
179 << Inet6SocketAddress::ConvertFrom(srcAddress).GetIpv6()
180 << " | Energy: " << +energyReading << "%");
181 }
182 }
183}
184
185void
187{
189 while (node != m_nodes.End())
190 {
191 m_sockets[*node] =
192 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::UdpSocketFactory"));
193 if ((*node)->GetObject<Ipv6>())
194 {
196 m_sockets[*node]->Bind(ipv6_local);
197 }
198
199 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
200 node++;
201 }
202}
203
204void
206{
207 Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable>();
208
210 Ipv6Address dst =
211 (*node)->GetObject<Ipv6L3Protocol>()->GetInterface(1)->GetAddress(1).GetAddress();
212 node++;
213 while (node != m_nodes.End())
214 {
215 uint8_t energy =
216 ((*node)->GetObject<EnergySourceContainer>()->Get(0)->GetEnergyFraction()) * 100;
217
218 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
219
220 double time = uniformRandomVariable->GetValue(0, 60);
221 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
222 node++;
223 }
225}
226
227void
229{
230 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
231 << " packet sent to " << dst);
233 m_sockets[node]->SendTo(pkt, 0, ipv6_destination);
234}
235
236void
238{
239 m_nodes.Create(3);
241 SetupEnergy();
244 SendPackets();
245}
246
247void
249{
250 std::map<Ptr<Node>, Ptr<Socket>>::iterator socket;
251
252 for (socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
253 {
254 socket->second->Close();
255 }
256}
257
258int
259main(int argc, char* argv[])
260{
261 CommandLine cmd(__FILE__);
262 cmd.Parse(argc, argv);
263
265 experiment.Prepare();
266
270
271 experiment.Teardown();
272
273 return 0;
274}
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:100
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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 implementation of the ICMPv6 protocol.
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
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...
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
Ipv6Address GetAddress() const
Get the IPv6 address.
IPv6 layer implementation.
Ipv6InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override
Get an address.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
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.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
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:471
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
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
AttributeValue implementation for Time.
Definition: nstime.h:1423
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:840
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:1336
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1300
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1312
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:702