A Discrete-Event Network Simulator
API
uan-ipv6-example.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Hossam Khader <hossamkhader@gmail.com>
18 */
19
20#include "ns3/core-module.h"
21#include "ns3/internet-module.h"
22#include "ns3/node-container.h"
23#include "ns3/mobility-helper.h"
24#include "ns3/mobility-model.h"
25#include "ns3/basic-energy-source-helper.h"
26#include "ns3/energy-source-container.h"
27#include "ns3/uan-helper.h"
28#include "ns3/uan-channel.h"
29#include "ns3/acoustic-modem-energy-model-helper.h"
30
31using namespace ns3;
32
42NS_LOG_COMPONENT_DEFINE ("UanIpv6Example");
43
44
45class UanExperiment
46{
47public:
49
54
58 void SetupEnergy ();
59
64
69
73 void SendPackets ();
74
82
88
92 void Prepare ();
93
97 void Teardown ();
98
99private:
101 std::map<Ptr<Node>, Ptr<Socket> > m_sockets;
102};
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;
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 InternetStackHelper internetStackHelper;
145 internetStackHelper.Install (m_nodes);
146
147 Ipv6AddressHelper ipv6AddressHelper;
148 ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
149 ipv6AddressHelper.Assign (netDeviceContainer);
150
151 node = m_nodes.Begin ();
152 while (node != m_nodes.End ())
153 {
154 (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
155 (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
156 (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
157 node++;
158 }
159}
160
161void
163{
164 Address srcAddress;
165 while (socket->GetRxAvailable () > 0)
166 {
167 Ptr<Packet> packet = socket->RecvFrom (srcAddress);
168 uint8_t energyReading;
169 packet->CopyData (&energyReading, 1);
170
171 if(Inet6SocketAddress::IsMatchingType (srcAddress))
172 {
173 NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " <<
174 Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
175 +energyReading << "%");
176 }
177 }
178}
179
180void
182{
184 while (node != m_nodes.End ())
185 {
186 m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
187 if((*node)->GetObject<Ipv6> () != NULL)
188 {
189 Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9);
190 m_sockets[*node]->Bind (ipv6_local);
191 }
192
193 m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
194 node++;
195 }
196}
197
198void
200{
201 Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
202
204 Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
205 node++;
206 while (node != m_nodes.End ())
207 {
208 uint8_t energy = ((*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 }
216 Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this);
217}
218
219void
221{
222 NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst );
223 Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9);
224 m_sockets[node]->SendTo (pkt, 0, ipv6_destination);
225}
226
227void
229{
230 m_nodes.Create (3);
232 SetupEnergy ();
235 SendPackets ();
236}
237
238void
240{
241 std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
242
243 for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
244 {
245 socket->second->Close ();
246 }
247}
248
249int
250main (int argc, char *argv[])
251{
252 CommandLine cmd (__FILE__);
253 cmd.Parse (argc, argv);
254
256 experiment.Prepare ();
257
258 Simulator::Stop (Days (50));
259 Simulator::Run ();
260 Simulator::Destroy ();
261
262 experiment.Teardown ();
263
264 return 0;
265}
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:91
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v)
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
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:41
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.
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:50
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
Get an address.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
Helper class used to assign positions and mobility models to nodes.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
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 Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) 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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
AttributeValue implementation for Time.
Definition: nstime.h:1308
UAN configuration helper.
Definition: uan-helper.h:41
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:212
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
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:205
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1220
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1228
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
channel
Definition: third.py:92
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89