|
|
|
|
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/udp-application-helper.h" |
| 31 |
#include "ns3/udp-application.h" |
| 32 |
#include "ns3/sixlowpan-helper.h" |
| 33 |
#include "ns3/sixlowpan-net-device.h" |
| 34 |
|
| 35 |
using namespace ns3; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
* This example shows the usage of UdpApplication 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 |
* |
| 44 |
*/ |
| 45 |
|
| 46 |
NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample"); |
| 47 |
|
| 48 |
void SetupPositions (); |
| 49 |
void SetupEnergy (); |
| 50 |
void SetupCommunications (); |
| 51 |
void PrintReceivedPacket (Address src, Buffer data); |
| 52 |
void SetupApplications (); |
| 53 |
void SendPacket (); |
| 54 |
void Run (); |
| 55 |
|
| 56 |
NodeContainer nodeContainer; |
| 57 |
|
| 58 |
void |
| 59 |
SetupPositions () |
| 60 |
{ |
| 61 |
MobilityHelper mobilityHelper; |
| 62 |
mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 63 |
mobilityHelper.Install (nodeContainer); |
| 64 |
nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0)); |
| 65 |
nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0)); |
| 66 |
nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0)); |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
SetupEnergy () |
| 71 |
{ |
| 72 |
BasicEnergySourceHelper energySourceHelper; |
| 73 |
energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); |
| 74 |
energySourceHelper.Install (nodeContainer); |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
SetupCommunications () |
| 79 |
{ |
| 80 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
| 81 |
UanHelper uanHelper; |
| 82 |
NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel); |
| 83 |
EnergySourceContainer energySourceContainer; |
| 84 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 85 |
while (node != nodeContainer.End ()) |
| 86 |
{ |
| 87 |
energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0)); |
| 88 |
node++; |
| 89 |
} |
| 90 |
AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; |
| 91 |
acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); |
| 92 |
|
| 93 |
SixLowPanHelper sixLowPanHelper; |
| 94 |
NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer); |
| 95 |
|
| 96 |
InternetStackHelper internetStackHelper; |
| 97 |
internetStackHelper.Install (nodeContainer); |
| 98 |
|
| 99 |
Ipv6AddressHelper ipv6AddressHelper; |
| 100 |
ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64)); |
| 101 |
ipv6AddressHelper.Assign (sixlowpanNetDevices); |
| 102 |
|
| 103 |
node = nodeContainer.Begin (); |
| 104 |
while (node != nodeContainer.End ()) |
| 105 |
{ |
| 106 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false)); |
| 107 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600))); |
| 108 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000))); |
| 109 |
node++; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
PrintReceivedPacket (Address src, Buffer data) |
| 115 |
{ |
| 116 |
std::cout << "Time:" << Simulator::Now ().GetDays () << "|" |
| 117 |
<< "Node:" << Ipv6Address::ConvertFrom (src) |
| 118 |
<< "|Energy:" << (int)(data.Begin ().ReadU8 ()) << "%" << std::endl; |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
SetupApplications () |
| 123 |
{ |
| 124 |
UdpApplicationHelper udpApplicationHelper; |
| 125 |
ApplicationContainer applicationContainer; |
| 126 |
udpApplicationHelper.SetPort (9); |
| 127 |
applicationContainer = udpApplicationHelper.Install (nodeContainer); |
| 128 |
applicationContainer.Start (Seconds (0)); |
| 129 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 130 |
while (node != nodeContainer.End ()) |
| 131 |
{ |
| 132 |
Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> (); |
| 133 |
udpApplication->SetPacketReceivedCallback (MakeCallback<void, Address, Buffer>(&PrintReceivedPacket)); |
| 134 |
node++; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
SendPacket () |
| 140 |
{ |
| 141 |
Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> (); |
| 142 |
NodeContainer::Iterator node = nodeContainer.Begin (); |
| 143 |
Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress (); |
| 144 |
node++; |
| 145 |
while (node != nodeContainer.End ()) |
| 146 |
{ |
| 147 |
uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100; |
| 148 |
Ptr<UdpApplication> udpApplication = (*node)->GetApplication (0)->GetObject<UdpApplication> (); |
| 149 |
Buffer dataBuffer; |
| 150 |
dataBuffer.AddAtEnd (sizeof (uint8_t)); |
| 151 |
Buffer::Iterator i = dataBuffer.Begin (); |
| 152 |
i.WriteU8 (energy); |
| 153 |
double time = uniformRandomVariable->GetValue (0, 60); |
| 154 |
Simulator::Schedule (Seconds (time), &UdpApplication::Send, udpApplication, dst, dataBuffer); |
| 155 |
node++; |
| 156 |
} |
| 157 |
Simulator::Schedule (Hours (2), &SendPacket); |
| 158 |
} |
| 159 |
|
| 160 |
void |
| 161 |
Run () |
| 162 |
{ |
| 163 |
nodeContainer.Create (3); |
| 164 |
SetupPositions (); |
| 165 |
SetupEnergy (); |
| 166 |
SetupCommunications (); |
| 167 |
SetupApplications (); |
| 168 |
SendPacket (); |
| 169 |
} |
| 170 |
|
| 171 |
int |
| 172 |
main (int argc, char *argv[]) |
| 173 |
{ |
| 174 |
CommandLine cmd; |
| 175 |
cmd.Parse (argc, argv); |
| 176 |
Run (); |
| 177 |
Simulator::Stop (Days (50)); |
| 178 |
Simulator::Run (); |
| 179 |
Simulator::Destroy (); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |