|
|
| 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-application-helper.h" |
| 29 |
#include "ns3/uan-application.h" |
| 30 |
#include "ns3/ipv4-address-helper.h" |
| 31 |
#include "ns3/internet-stack-helper.h" |
| 32 |
#include "ns3/ipv4-interface.h" |
| 33 |
#include "ns3/arp-cache.h" |
| 34 |
|
| 35 |
using namespace ns3; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
* This example shows the usage of UanApplication to transfer data. |
| 40 |
* Two nodes are sending their remaining energy percentage (1 byte) |
| 41 |
* to a gateway node, that prints the received data. |
| 42 |
* The transmissions are scheduled at random times to avoid collisions |
| 43 |
* ARP parameters should be tuned, based on the network size |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
NS_LOG_COMPONENT_DEFINE ("UanApplicationExample"); |
| 48 |
|
| 49 |
void SetupPositions (); |
| 50 |
void SetupEnergy (); |
| 51 |
void SetupCommunications (); |
| 52 |
void PrintReceivedPacket (Address src, Buffer data); |
| 53 |
void SetupApplications (); |
| 54 |
void SendPacket (); |
| 55 |
void Run (); |
| 56 |
|
| 57 |
NodeContainer nodeContainer; |
| 58 |
|
| 59 |
void |
| 60 |
SetupPositions () |
| 61 |
{ |
| 62 |
MobilityHelper mobilityHelper; |
| 63 |
mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 64 |
mobilityHelper.Install (nodeContainer); |
| 65 |
nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0)); |
| 66 |
nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0)); |
| 67 |
nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0)); |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
SetupEnergy () |
| 72 |
{ |
| 73 |
BasicEnergySourceHelper energySourceHelper; |
| 74 |
energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); |
| 75 |
energySourceHelper.Install (nodeContainer); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
SetupCommunications () |
| 80 |
{ |
| 81 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
| 82 |
UanHelper uanHelper; |
| 83 |
NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel); |
| 84 |
EnergySourceContainer energySourceContainer; |
| 85 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 86 |
while (node != nodeContainer.End ()) |
| 87 |
{ |
| 88 |
energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0)); |
| 89 |
node++; |
| 90 |
} |
| 91 |
AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; |
| 92 |
acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); |
| 93 |
|
| 94 |
InternetStackHelper internet; |
| 95 |
internet.Install (nodeContainer); |
| 96 |
Ipv4AddressHelper ipv4; |
| 97 |
ipv4.SetBase ("10.0.0.0", "255.255.255.0"); |
| 98 |
ipv4.Assign (netDeviceContainer); |
| 99 |
|
| 100 |
node = nodeContainer.Begin (); |
| 101 |
while (node != nodeContainer.End ()) |
| 102 |
{ |
| 103 |
(*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetDeadTimeout (Hours (24)); |
| 104 |
(*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetAliveTimeout (Hours (12)); |
| 105 |
(*node)->GetObject<Ipv4L3Protocol> ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10)); |
| 106 |
node++; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
PrintReceivedPacket (Address src, Buffer data) |
| 112 |
{ |
| 113 |
std::cout << "Time:" << Simulator::Now ().GetHours () << "|" |
| 114 |
<< "Node:" << Ipv4Address::ConvertFrom (src) |
| 115 |
<< "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl; |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
SetupApplications () |
| 120 |
{ |
| 121 |
UanApplicationHelper uanApplicationHelper; |
| 122 |
ApplicationContainer applicationContainer; |
| 123 |
uanApplicationHelper.SetPort (9); |
| 124 |
applicationContainer = uanApplicationHelper.Install (nodeContainer); |
| 125 |
applicationContainer.Start (Seconds (0)); |
| 126 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 127 |
while (node != nodeContainer.End ()) |
| 128 |
{ |
| 129 |
Ptr<UanApplication> uanApplication = (*node)->GetApplication (0)->GetObject<UanApplication> (); |
| 130 |
uanApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket)); |
| 131 |
node++; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
SendPacket () |
| 137 |
{ |
| 138 |
Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> (); |
| 139 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 140 |
node++; |
| 141 |
while (node != nodeContainer.End ()) |
| 142 |
{ |
| 143 |
uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100; |
| 144 |
Ptr<UanApplication> uanApplication = (*node)->GetApplication (0)->GetObject<UanApplication> (); |
| 145 |
Buffer dataBuffer; |
| 146 |
dataBuffer.AddAtEnd (sizeof (uint8_t)); |
| 147 |
Buffer::Iterator i = dataBuffer.Begin (); |
| 148 |
double time = uniformRandomVariable->GetValue (0, 15); |
| 149 |
i.WriteU8 (energy); |
| 150 |
Simulator::Schedule (Seconds (time), &UanApplication::Send, uanApplication, Ipv4Address ("10.0.0.1"), dataBuffer); |
| 151 |
node++; |
| 152 |
} |
| 153 |
Simulator::Schedule (Minutes (5), &SendPacket); |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
Run () |
| 158 |
{ |
| 159 |
nodeContainer.Create (3); |
| 160 |
SetupPositions (); |
| 161 |
SetupEnergy (); |
| 162 |
SetupCommunications (); |
| 163 |
SetupApplications (); |
| 164 |
SendPacket (); |
| 165 |
} |
| 166 |
|
| 167 |
int |
| 168 |
main (int argc, char *argv[]) |
| 169 |
{ |
| 170 |
CommandLine cmd; |
| 171 |
cmd.Parse (argc, argv); |
| 172 |
Run (); |
| 173 |
Simulator::Stop (Days (20)); |
| 174 |
Simulator::Run (); |
| 175 |
Simulator::Destroy (); |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |