A Discrete-Event Network Simulator
API
uan-raw-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/packet-socket-helper.h"
31 #include "ns3/packet-socket-address.h"
32 
33 using namespace ns3;
34 
44 NS_LOG_COMPONENT_DEFINE ("UanRawExample");
45 
46 
47 class UanExperiment
48 {
49 public:
51 
55  void SetupPositions ();
56 
60  void SetupEnergy ();
61 
66 
71 
75  void SendPackets ();
76 
83  void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Mac8Address dst);
84 
90 
94  void Prepare ();
95 
99  void Teardown ();
100 
101 private:
102  NodeContainer m_nodes;
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 
147 void
149 {
150  Address srcAddress;
151  while (socket->GetRxAvailable () > 0)
152  {
153  Ptr<Packet> packet = socket->RecvFrom (srcAddress);
154  PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress);
155  srcAddress = packetSocketAddress.GetPhysicalAddress ();
156  uint8_t energyReading;
157  packet->CopyData (&energyReading, 1);
158 
159  if(Mac8Address::IsMatchingType (srcAddress))
160  {
161  NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " <<
162  Mac8Address::ConvertFrom (srcAddress) << " | Energy: " <<
163  +energyReading << "%");
164  }
165  }
166 }
167 
168 void
170 {
171  NodeContainer::Iterator node = m_nodes.Begin ();
172  PacketSocketHelper packetSocketHelper;
173  while (node != m_nodes.End ())
174  {
175  packetSocketHelper.Install (*node);
176  PacketSocketAddress socketAddress;
177  socketAddress.SetSingleDevice ((*node)->GetDevice (0)->GetIfIndex ());
178  socketAddress.SetProtocol (0);
179  m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::PacketSocketFactory"));
180  m_sockets[*node]->Bind ();
181  m_sockets[*node]->Connect (socketAddress);
182  m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this));
183  node++;
184  }
185 }
186 
187 void
189 {
190  Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> ();
191 
192  NodeContainer::Iterator node = m_nodes.Begin ();
193  Mac8Address dst = Mac8Address::ConvertFrom ((*node)->GetDevice (0)->GetAddress ());
194  node++;
195  while (node != m_nodes.End ())
196  {
197  uint8_t energy = ((*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 
208 void
210 {
211  NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst );
212  PacketSocketAddress socketAddress;
213  socketAddress.SetSingleDevice (node->GetDevice (0)->GetIfIndex ());
214  socketAddress.SetPhysicalAddress (dst);
215  socketAddress.SetProtocol (0);
216  m_sockets[node]->SendTo (pkt, 0, socketAddress);
217 }
218 
219 void
221 {
222  m_nodes.Create (3);
223  SetupPositions ();
224  SetupEnergy ();
225  SetupCommunications ();
226  SetupApplications ();
227  SendPackets ();
228 }
229 
230 void
232 {
233  std::map<Ptr<Node>, Ptr<Socket> >::iterator socket;
234 
235  for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++)
236  {
237  socket->second->Close ();
238  }
239 }
240 
241 int
242 main (int argc, char *argv[])
243 {
244  CommandLine cmd (__FILE__);
245  cmd.Parse (argc, argv);
246 
248  experiment.Prepare ();
249 
250  Simulator::Stop (Days (50));
251  Simulator::Run ();
253 
254  experiment.Teardown ();
255 
256  return 0;
257 }
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::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::Mac8Address
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:43
SetPosition
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89
ns3::Mac8Address::ConvertFrom
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
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::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::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::PacketSocketAddress
an address for a packet socket
Definition: packet-socket-address.h:39
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::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::PacketSocketHelper::Install
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Definition: packet-socket-helper.cc:37
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::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.
ns3::Node::GetDevice
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
ns3::Mac8Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:63
second.cmd
cmd
Definition: second.py:35
ns3::Days
Time Days(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1265
ns3::PacketSocketAddress::GetPhysicalAddress
Address GetPhysicalAddress(void) const
Get the destination address.
Definition: packet-socket-address.cc:78
UanExperiment::SetupPositions
void SetupPositions()
Set the UAN nodes position.
ns3::PacketSocketAddress::SetSingleDevice
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Definition: packet-socket-address.cc:46
NS_LOG_UNCOND
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
Definition: log-macros-enabled.h:269
ns3::PacketSocketHelper
Give ns3::PacketSocket powers to ns3::Node.
Definition: packet-socket-helper.h:32
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::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::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::PacketSocketAddress::SetPhysicalAddress
void SetPhysicalAddress(const Address address)
Set the destination address.
Definition: packet-socket-address.cc:53
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
UanExperiment
This example shows the usage of UDP over 6LoWPAN to transfer data.
Definition: uan-6lowpan-example.cc:48
ns3::PacketSocketAddress::SetProtocol
void SetProtocol(uint16_t protocol)
Set the protocol.
Definition: packet-socket-address.cc:33
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.
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::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
ns3::NetDevice::GetIfIndex
virtual uint32_t GetIfIndex(void) const =0
UanExperiment::SetupEnergy
void SetupEnergy()
Set the UAN nodes energy.
ns3::PacketSocketAddress::ConvertFrom
static PacketSocketAddress ConvertFrom(const Address &address)
Definition: packet-socket-address.cc:106
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