A Discrete-Event Network Simulator
API
uan-ipv4-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 
31 using namespace ns3;
32 
42 NS_LOG_COMPONENT_DEFINE ("UanIpv4Example");
43 
44 
45 class UanExperiment
46 {
47 public:
49 
53  void SetupPositions ();
54 
58  void SetupEnergy ();
59 
64 
69 
73  void SendPackets ();
74 
81  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv4Address dst);
82 
88 
92  void Prepare ();
93 
97  void Teardown ();
98 
99 private:
100  NodeContainer m_nodes;
101  std::map<Ptr<Node>, Ptr<Socket> > m_sockets;
102 };
103 
104 
106 {
107 }
108 
109 void
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 
120 void
122 {
123  BasicEnergySourceHelper energySourceHelper;
124  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
125  energySourceHelper.Install (m_nodes);
126 }
127 
128 void
130 {
131  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
132  UanHelper uanHelper;
133  NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel);
134  EnergySourceContainer energySourceContainer;
135  NodeContainer::Iterator 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  InternetStackHelper internetStackHelper;
145  internetStackHelper.Install (m_nodes);
146 
147  Ipv4AddressHelper ipv4AddressHelper;
148  ipv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.0");
149  ipv4AddressHelper.Assign (netDeviceContainer);
150  node = m_nodes.Begin ();
151  while (node != m_nodes.End ())
152  {
153  (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10));
154  node++;
155  }
156 }
157 
158 void
160 {
161  Address srcAddress;
162  while (socket->GetRxAvailable () > 0)
163  {
164  Ptr<Packet> packet = socket->RecvFrom (srcAddress);
165  uint8_t energyReading;
166  packet->CopyData (&energyReading, 1);
167 
168  if(InetSocketAddress::IsMatchingType (srcAddress))
169  {
170  NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " <<
171  InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 () << " | Energy: " <<
172  +energyReading << "%");
173  }
174  }
175 }
176 
177 void
179 {
180  NodeContainer::Iterator node = m_nodes.Begin ();
181  while (node != m_nodes.End ())
182  {
183  m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
184  if((*node)->GetObject<Ipv4> () != NULL)
185  {
187  m_sockets[*node]->Bind (ipv4_local);
188  }
189 
190  m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
191  node++;
192  }
193 }
194 
195 void
197 {
198  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
199 
200  NodeContainer::Iterator node = m_nodes.Begin ();
201  Ipv4Address dst = (*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetAddress (0).GetLocal ();
202  node++;
203  while (node != m_nodes.End ())
204  {
205  uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
206 
207  Ptr<Packet> pkt = Create<Packet> (&energy, 1);
208 
209  double time = uniformRandomVariable->GetValue (0, 60);
210  Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
211  node++;
212  }
214 }
215 
216 void
218 {
219  NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst );
220  InetSocketAddress ipv4_destination = InetSocketAddress (dst, 9);
221  m_sockets[node]->SendTo (pkt, 0, ipv4_destination);
222 }
223 
224 void
226 {
227  m_nodes.Create (3);
228  SetupPositions ();
229  SetupEnergy ();
230  SetupCommunications ();
231  SetupApplications ();
232  SendPackets ();
233 }
234 
235 void
237 {
238  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
239 
240  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
241  {
242  socket->second->Close ();
243  }
244 }
245 
246 int
247 main (int argc, char *argv[])
248 {
249  CommandLine cmd (__FILE__);
250  cmd.Parse (argc, argv);
251 
253  experiment.Prepare ();
254 
255  Simulator::Stop (Days (50));
256  Simulator::Run ();
258 
259  experiment.Teardown ();
260 
261  return 0;
262 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::DeviceEnergyModelHelper::Install
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
Definition: energy-model-helper.cc:91
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
UanExperiment::SetupCommunications
void SetupCommunications()
Set the UAN nodes communication channels.
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::InetSocketAddress::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: inet-socket-address.cc:103
SetPosition
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::MobilityHelper::SetMobilityModel
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())
Definition: mobility-helper.cc:79
ns3::MobilityHelper::Install
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Definition: mobility-helper.cc:130
UanExperiment::PrintReceivedPacket
void PrintReceivedPacket(Ptr< Socket > socket)
Print the received packet.
ns3::UanHelper
UAN configuration helper.
Definition: uan-helper.h:41
third.channel
channel
Definition: third.py:92
ns3::Packet::CopyData
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::EnergySourceContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first EnergySource pointer in the container.
Definition: energy-source-container.cc:70
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::InetSocketAddress::ConvertFrom
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Definition: inet-socket-address.cc:126
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
UanExperiment::Prepare
void Prepare()
Prepare the experiment.
ns3::BasicEnergySourceHelper::Set
void Set(std::string name, const AttributeValue &v)
Definition: basic-energy-source-helper.cc:36
UanExperiment::SendPackets
void SendPackets()
Send a packet from all the nodes.
ns3::EnergySourceContainer
Holds a vector of ns3::EnergySource pointers.
Definition: energy-source-container.h:45
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ipv4
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
ns3::EnergySourceHelper::Install
EnergySourceContainer Install(Ptr< Node > node) const
Definition: energy-model-helper.cc:35
ns3::Ptr< Node >
ns3::EnergySourceContainer::Get
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
Definition: energy-source-container.cc:88
UanExperiment::Teardown
void Teardown()
Teardown the experiment.
experiment
void experiment(std::string queue_disc_type)
Definition: cobalt-vs-codel.cc:74
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::BasicEnergySourceHelper
Creates a BasicEnergySource object.
Definition: basic-energy-source-helper.h:35
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
ns3::Ipv4InterfaceContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first pair in the container.
Definition: ipv4-interface-container.cc:41
ns3::Socket::RecvFrom
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.
second.cmd
cmd
Definition: second.py:35
ns3::Days
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1265
UanExperiment::SetupPositions
void SetupPositions()
Set the UAN nodes position.
NS_LOG_UNCOND
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
Definition: log-macros-enabled.h:269
ns3::MakeCallback
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:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::NodeContainer::Iterator
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-container.h:42
UanExperiment::UanExperiment
UanExperiment()
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::AcousticModemEnergyModelHelper
Assign AcousticModemEnergyModel to uan devices.
Definition: acoustic-modem-energy-model-helper.h:38
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::Ipv4L3Protocol
Implement the IPv4 layer.
Definition: ipv4-l3-protocol.h:81
ns3::MobilityModel
Keep track of the current position and velocity of an object.
Definition: mobility-model.h:40
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4InterfaceAddress::GetLocal
Ipv4Address GetLocal(void) const
Get the local address.
Definition: ipv4-interface-address.cc:74
UanExperiment
This example shows the usage of UDP over 6LoWPAN to transfer data.
Definition: uan-6lowpan-example.cc:48
ns3::Socket::CreateSocket
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
ns3::Hours
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1273
ns3::Socket::GetRxAvailable
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
ns3::Ipv4L3Protocol::GetAddress
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
Definition: ipv4-l3-protocol.cc:1168
UanExperiment::SetupApplications
void SetupApplications()
Set the UAN nodes communication channels.
ns3::UanHelper::Install
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
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
ns3::EnergySourceContainer::Add
void Add(EnergySourceContainer container)
Definition: energy-source-container.cc:94
UanExperiment::SetupEnergy
void SetupEnergy()
Set the UAN nodes energy.
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Definition: random-variable-stream.cc:182
UanExperiment::SendSinglePacket
void SendSinglePacket(Ptr< Node > node, Ptr< Packet > pkt, Ipv6Address dst)
Send a packet from one of the nodes.
Definition: uan-6lowpan-example.cc:225
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43