|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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/core-module.h" |
| 20 |
#include "ns3/node-container.h" |
| 21 |
#include "ns3/mobility-helper.h" |
| 22 |
#include "ns3/mobility-model.h" |
| 23 |
#include "ns3/basic-energy-source-helper.h" |
| 24 |
#include "ns3/energy-source-container.h" |
| 25 |
#include "ns3/uan-helper.h" |
| 26 |
#include "ns3/uan-channel.h" |
| 27 |
#include "ns3/acoustic-modem-energy-model-helper.h" |
| 28 |
#include "ns3/uan-raw-application-helper.h" |
| 29 |
#include "ns3/uan-raw-application.h" |
| 30 |
|
| 31 |
using namespace ns3; |
| 32 |
|
| 33 |
/** |
| 34 |
* |
| 35 |
* This example shows the usage of UanRawApplication to transfer data. |
| 36 |
* Two nodes are sending their remaining energy percentage (1 byte) |
| 37 |
* to a gateway node, that prints the received data. |
| 38 |
* The transmissions are scheduled at random times to avoid collisions |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
NS_LOG_COMPONENT_DEFINE ("UanRawApplicationExample"); |
| 43 |
|
| 44 |
void SetupPositions (); |
| 45 |
void SetupEnergy (); |
| 46 |
void SetupCommunications (); |
| 47 |
void PrintReceivedPacket (Address src, Buffer data); |
| 48 |
void SetupApplications (); |
| 49 |
void SendPacket (); |
| 50 |
void Run (); |
| 51 |
|
| 52 |
NodeContainer nodeContainer; |
| 53 |
|
| 54 |
void |
| 55 |
SetupPositions () |
| 56 |
{ |
| 57 |
MobilityHelper mobilityHelper; |
| 58 |
mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 59 |
mobilityHelper.Install (nodeContainer); |
| 60 |
nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0)); |
| 61 |
nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0)); |
| 62 |
nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0)); |
| 63 |
} |
| 64 |
|
| 65 |
void |
| 66 |
SetupEnergy () |
| 67 |
{ |
| 68 |
BasicEnergySourceHelper energySourceHelper; |
| 69 |
energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); |
| 70 |
energySourceHelper.Install (nodeContainer); |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
SetupCommunications () |
| 75 |
{ |
| 76 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
| 77 |
UanHelper uanHelper; |
| 78 |
NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel); |
| 79 |
EnergySourceContainer energySourceContainer; |
| 80 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 81 |
while (node != nodeContainer.End ()) |
| 82 |
{ |
| 83 |
energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0)); |
| 84 |
node++; |
| 85 |
} |
| 86 |
AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; |
| 87 |
acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
PrintReceivedPacket (Address src, Buffer data) |
| 92 |
{ |
| 93 |
std::cout << "Time:" << Simulator::Now ().GetHours () << "|" |
| 94 |
<< "Node:" << (int)(UanAddress::ConvertFrom (src).GetAsInt ()) |
| 95 |
<< "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl; |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
SetupApplications () |
| 100 |
{ |
| 101 |
UanRawApplicationHelper uanRawApplicationHelper; |
| 102 |
ApplicationContainer applicationContainer; |
| 103 |
applicationContainer = uanRawApplicationHelper.Install (nodeContainer); |
| 104 |
applicationContainer.Start (Seconds (0)); |
| 105 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 106 |
while (node != nodeContainer.End ()) |
| 107 |
{ |
| 108 |
Ptr<UanRawApplication> uanRawApplication = (*node)->GetApplication (0)->GetObject<UanRawApplication> (); |
| 109 |
uanRawApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket)); |
| 110 |
node++; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
SendPacket () |
| 116 |
{ |
| 117 |
Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> (); |
| 118 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 119 |
node++; |
| 120 |
while (node != nodeContainer.End ()) |
| 121 |
{ |
| 122 |
uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100; |
| 123 |
Ptr<UanRawApplication> uanRawApplication = (*node)->GetApplication (0)->GetObject<UanRawApplication> (); |
| 124 |
Buffer dataBuffer; |
| 125 |
dataBuffer.AddAtEnd (sizeof (uint8_t)); |
| 126 |
Buffer::Iterator i = dataBuffer.Begin (); |
| 127 |
double time = uniformRandomVariable->GetValue (0, 15); |
| 128 |
i.WriteU8 (energy); |
| 129 |
Simulator::Schedule (Seconds (time), &UanRawApplication::Send, uanRawApplication, UanAddress (0), dataBuffer); |
| 130 |
node++; |
| 131 |
} |
| 132 |
Simulator::Schedule (Minutes (5), &SendPacket); |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
Run () |
| 137 |
{ |
| 138 |
Packet::EnablePrinting (); |
| 139 |
nodeContainer.Create (3); |
| 140 |
SetupPositions (); |
| 141 |
SetupEnergy (); |
| 142 |
SetupCommunications (); |
| 143 |
SetupApplications (); |
| 144 |
SendPacket (); |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
main (int argc, char *argv[]) |
| 149 |
{ |
| 150 |
CommandLine cmd; |
| 151 |
cmd.Parse (argc, argv); |
| 152 |
Run (); |
| 153 |
Simulator::Stop (Days (20)); |
| 154 |
Simulator::Run (); |
| 155 |
Simulator::Destroy (); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |