A Discrete-Event Network Simulator
API
uan-6lowpan-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 #include "ns3/sixlowpan-helper.h"
31 #include "ns3/sixlowpan-net-device.h"
32 
33 using namespace ns3;
34 
44 NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample");
45 
46 
48 {
49 public:
50  UanExperiment ();
51 
55  void SetupPositions ();
56 
60  void SetupEnergy ();
61 
65  void SetupCommunications ();
66 
70  void SetupApplications ();
71 
75  void SendPackets ();
76 
83  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst);
84 
89  void PrintReceivedPacket (Ptr<Socket> socket);
90 
94  void Prepare ();
95 
99  void Teardown ();
100 
101 private:
103  std::map<Ptr<Node>, Ptr<Socket> > m_sockets;
104 };
105 
106 
108 {
109 }
110 
111 void
113 {
114  MobilityHelper mobilityHelper;
115  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
116  mobilityHelper.Install (m_nodes);
117  m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
118  m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
119  m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
120 }
121 
122 void
124 {
125  BasicEnergySourceHelper energySourceHelper;
126  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
127  energySourceHelper.Install (m_nodes);
128 }
129 
130 void
132 {
133  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
134  UanHelper uanHelper;
135  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
136  EnergySourceContainer energySourceContainer;
137  NodeContainer::Iterator node = m_nodes.Begin ();
138  while (node != m_nodes.End ())
139  {
140  energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0));
141  node++;
142  }
143  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
144  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
145 
146  SixLowPanHelper sixLowPanHelper;
147  NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer);
148 
149  InternetStackHelper internetStackHelper;
150  internetStackHelper.Install (m_nodes);
151 
152  Ipv6AddressHelper ipv6AddressHelper;
153  ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64));
154  ipv6AddressHelper.Assign (sixlowpanNetDevices);
155 
156  node = m_nodes.Begin ();
157  while (node != m_nodes.End ())
158  {
159  (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
160  (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600)));
161  (*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000)));
162  node++;
163  }
164 }
165 
166 void
168 {
169  Address srcAddress;
170  while (socket->GetRxAvailable () > 0)
171  {
172  Ptr<Packet> packet = socket->RecvFrom (srcAddress);
173  uint8_t energyReading;
174  packet->CopyData (&energyReading, 1);
175 
176  if(Inet6SocketAddress::IsMatchingType (srcAddress))
177  {
178  NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " <<
179  Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " <<
180  +energyReading << "%");
181  }
182  }
183 }
184 
185 void
187 {
188  NodeContainer::Iterator node = m_nodes.Begin ();
189  while (node != m_nodes.End ())
190  {
191  m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
192  if((*node)->GetObject<Ipv6> () != NULL)
193  {
195  m_sockets[*node]->Bind (ipv6_local);
196  }
197 
198  m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
199  node++;
200  }
201 }
202 
203 void
205 {
206  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
207 
208  NodeContainer::Iterator node = m_nodes.Begin ();
209  Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress ();
210  node++;
211  while (node != m_nodes.End ())
212  {
213  uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
214 
215  Ptr<Packet> pkt = Create<Packet> (&energy, 1);
216 
217  double time = uniformRandomVariable->GetValue (0, 60);
218  Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
219  node++;
220  }
222 }
223 
224 void
226 {
227  NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst );
228  Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9);
229  m_sockets[node]->SendTo (pkt, 0, ipv6_destination);
230 }
231 
232 void
234 {
235  m_nodes.Create (3);
236  SetupPositions ();
237  SetupEnergy ();
238  SetupCommunications ();
239  SetupApplications ();
240  SendPackets ();
241 }
242 
243 void
245 {
246  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
247 
248  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
249  {
250  socket->second->Close ();
251  }
252 }
253 
254 int
255 main (int argc, char *argv[])
256 {
258  cmd.Parse (argc, argv);
259 
261  experiment.Prepare ();
262 
263  Simulator::Stop (Days (50));
264  Simulator::Run ();
266 
267  experiment.Teardown ();
268 
269  return 0;
270 }
UAN configuration helper.
Definition: uan-helper.h:40
tuple channel
Definition: third.py:85
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
static std::string PrintReceivedPacket(Address &from)
std::map< Ptr< Node >, Ptr< Socket > > m_sockets
send and receive sockets
Holds a vector of ns3::EnergySource pointers.
void Add(EnergySourceContainer container)
AttributeValue implementation for Boolean.
Definition: boolean.h:36
void SendSinglePacket(Ptr< Node > node, Ptr< Packet > pkt, Ipv6Address dst)
Send a packet from one of the nodes.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
IPv6 layer implementation.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Assign AcousticModemEnergyModel to uan devices.
NodeContainer m_nodes
UAN nodes.
tuple cmd
Definition: second.py:35
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:82
a polymophic address class
Definition: address.h:90
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Keep track of the current position and velocity of an object.
Ipv6Address GetAddress() const
Get the IPv6 address.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
AttributeValue implementation for Time.
Definition: nstime.h:1069
void Prepare()
Prepare the experiment.
Creates a BasicEnergySource object.
holds a vector of ns3::NetDevice pointers
void SetupEnergy()
Set the UAN nodes energy.
An Inet6 address class.
An implementation of the ICMPv6 protocol.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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:71
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void Teardown()
Teardown the experiment.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
EnergySourceContainer Install(Ptr< Node > node) const
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())
Ipv6InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
Get an address.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:991
void PrintReceivedPacket(Ptr< Socket > socket)
Print the received packet.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
Helper class to auto-assign global IPv6 unicast addresses.
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr stored in this container.
Helper class used to assign positions and mobility models to nodes.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
void Set(std::string name, const AttributeValue &v)
static bool IsMatchingType(const Address &addr)
If the address match.
Iterator Begin(void) const
Get an iterator which refers to the first pair in the container.
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
void SetupApplications()
Set the UAN nodes communication channels.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
void Parse(int argc, char *argv[])
Parse the program arguments.
This example shows the usage of UDP over 6LoWPAN to transfer data.
void SendPackets()
Send a packet from all the nodes.
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.
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:983
void SetupPositions()
Set the UAN nodes position.
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
Iterator Begin(void) const
Get an iterator which refers to the first EnergySource pointer in the container.
void SetupCommunications()
Set the UAN nodes communication channels.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:823
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
void experiment(bool enableCtsRts, std::string wifiManager)
Run single 10 seconds experiment.