|
|
|
|
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/sixlowpan-helper.h" |
| 31 |
#include "ns3/sixlowpan-net-device.h" |
| 32 |
|
| 33 |
using namespace ns3; |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
* This example shows the usage of UDP over 6LoWPAN to transfer data. |
| 38 |
* Two nodes are sending their remaining energy percentage (1 byte) |
| 39 |
* to a gateway node, that prints the received data. |
| 40 |
* The transmissions are scheduled at random times to avoid collisions |
| 41 |
* |
| 42 |
*/ |
| 43 |
|
| 44 |
NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample"); |
| 45 |
|
| 46 |
|
| 47 |
class UanExperiment |
| 48 |
{ |
| 49 |
public: |
| 50 |
UanExperiment (); |
| 51 |
|
| 52 |
/** |
| 53 |
* Set the UAN nodes position |
| 54 |
*/ |
| 55 |
void SetupPositions (); |
| 56 |
|
| 57 |
/** |
| 58 |
* Set the UAN nodes energy |
| 59 |
*/ |
| 60 |
void SetupEnergy (); |
| 61 |
|
| 62 |
/** |
| 63 |
* Set the UAN nodes communication channels |
| 64 |
*/ |
| 65 |
void SetupCommunications (); |
| 66 |
|
| 67 |
/** |
| 68 |
* Set the UAN nodes communication channels |
| 69 |
*/ |
| 70 |
void SetupApplications (); |
| 71 |
|
| 72 |
/** |
| 73 |
* Send a packet from all the nodes |
| 74 |
*/ |
| 75 |
void SendPackets (); |
| 76 |
|
| 77 |
/** |
| 78 |
* Send a packet from one of the nodes |
| 79 |
* \param sock The sending socket |
| 80 |
* \param pkt The packet |
| 81 |
* \param dst the destination |
| 82 |
*/ |
| 83 |
void SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst); |
| 84 |
|
| 85 |
void PrintReceivedPacket (Ptr<Socket> socket); |
| 86 |
|
| 87 |
/** |
| 88 |
* Prepare the experiment |
| 89 |
*/ |
| 90 |
void Prepare (); |
| 91 |
|
| 92 |
/** |
| 93 |
* Teardown the experiment |
| 94 |
*/ |
| 95 |
void Teardown (); |
| 96 |
|
| 97 |
private: |
| 98 |
NodeContainer m_nodes; //!< UAN nodes |
| 99 |
std::map<Ptr<Node>, Ptr<Socket> > m_sockets; //!< send and receive sockets |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
UanExperiment::UanExperiment () |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
UanExperiment::SetupPositions () |
| 109 |
{ |
| 110 |
MobilityHelper mobilityHelper; |
| 111 |
mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 112 |
mobilityHelper.Install (m_nodes); |
| 113 |
m_nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0)); |
| 114 |
m_nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0)); |
| 115 |
m_nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0)); |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
UanExperiment::SetupEnergy () |
| 120 |
{ |
| 121 |
BasicEnergySourceHelper energySourceHelper; |
| 122 |
energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); |
| 123 |
energySourceHelper.Install (m_nodes); |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
UanExperiment::SetupCommunications () |
| 128 |
{ |
| 129 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
| 130 |
UanHelper uanHelper; |
| 131 |
NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel); |
| 132 |
EnergySourceContainer energySourceContainer; |
| 133 |
NodeContainer::Iterator node = m_nodes.Begin (); |
| 134 |
while (node != m_nodes.End ()) |
| 135 |
{ |
| 136 |
energySourceContainer.Add ((*node)->GetObject<EnergySourceContainer> ()->Get (0)); |
| 137 |
node++; |
| 138 |
} |
| 139 |
AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; |
| 140 |
acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); |
| 141 |
|
| 142 |
SixLowPanHelper sixLowPanHelper; |
| 143 |
NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer); |
| 144 |
|
| 145 |
InternetStackHelper internetStackHelper; |
| 146 |
internetStackHelper.Install (m_nodes); |
| 147 |
|
| 148 |
Ipv6AddressHelper ipv6AddressHelper; |
| 149 |
ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64)); |
| 150 |
ipv6AddressHelper.Assign (sixlowpanNetDevices); |
| 151 |
|
| 152 |
node = m_nodes.Begin (); |
| 153 |
while (node != m_nodes.End ()) |
| 154 |
{ |
| 155 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false)); |
| 156 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600))); |
| 157 |
(*node)->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000))); |
| 158 |
node++; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void |
| 163 |
UanExperiment::PrintReceivedPacket (Ptr<Socket> socket) |
| 164 |
{ |
| 165 |
Address srcAddress; |
| 166 |
while (socket->GetRxAvailable () > 0) |
| 167 |
{ |
| 168 |
Ptr<Packet> packet = socket->RecvFrom (srcAddress); |
| 169 |
uint8_t energyReading; |
| 170 |
packet->CopyData (&energyReading, 1); |
| 171 |
|
| 172 |
if(Inet6SocketAddress::IsMatchingType (srcAddress)) |
| 173 |
{ |
| 174 |
std::cout << "Time: " << Simulator::Now ().GetDays () << " | " << "Node: " << |
| 175 |
Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " << |
| 176 |
+energyReading << "%" << std::endl; |
| 177 |
|
| 178 |
NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetDays () << " | Node: " << |
| 179 |
Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " << |
| 180 |
+energyReading << "%"); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
UanExperiment::SetupApplications () |
| 187 |
{ |
| 188 |
NodeContainer::Iterator node = m_nodes.Begin (); |
| 189 |
while (node != m_nodes.End ()) |
| 190 |
{ |
| 191 |
m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory")); |
| 192 |
if((*node)->GetObject<Ipv6> () != NULL) |
| 193 |
{ |
| 194 |
Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9); |
| 195 |
m_sockets[*node]->Bind (ipv6_local); |
| 196 |
std::cout << "socket created" << std::endl; |
| 197 |
} |
| 198 |
|
| 199 |
m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this)); |
| 200 |
node++; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
UanExperiment::SendPackets () |
| 206 |
{ |
| 207 |
Ptr<UniformRandomVariable> uniformRandomVariable = CreateObject<UniformRandomVariable> (); |
| 208 |
|
| 209 |
NodeContainer::Iterator node = m_nodes.Begin (); |
| 210 |
Ipv6Address dst = (*node)->GetObject<Ipv6L3Protocol> ()->GetInterface (1)->GetAddress (1).GetAddress (); |
| 211 |
node++; |
| 212 |
while (node != m_nodes.End ()) |
| 213 |
{ |
| 214 |
uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100; |
| 215 |
|
| 216 |
Ptr<Packet> pkt = Create<Packet> (&energy, 1); |
| 217 |
|
| 218 |
double time = uniformRandomVariable->GetValue (0, 60); |
| 219 |
Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst); |
| 220 |
node++; |
| 221 |
} |
| 222 |
Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this); |
| 223 |
} |
| 224 |
|
| 225 |
void |
| 226 |
UanExperiment::SendSinglePacket (Ptr<Node> node, Ptr<Packet> pkt, Ipv6Address dst) |
| 227 |
{ |
| 228 |
std::cout << Simulator::Now ().As (Time::MIN) << " packet sent to " << dst << std::endl; |
| 229 |
Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9); |
| 230 |
m_sockets[node]->SendTo (pkt, 0, ipv6_destination); |
| 231 |
} |
| 232 |
|
| 233 |
void |
| 234 |
UanExperiment::Prepare () |
| 235 |
{ |
| 236 |
m_nodes.Create (3); |
| 237 |
SetupPositions (); |
| 238 |
SetupEnergy (); |
| 239 |
SetupCommunications (); |
| 240 |
SetupApplications (); |
| 241 |
SendPackets (); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
UanExperiment::Teardown () |
| 246 |
{ |
| 247 |
std::map<Ptr<Node>, Ptr<Socket> >::iterator socket; |
| 248 |
|
| 249 |
for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++) |
| 250 |
{ |
| 251 |
socket->second->Close (); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
int |
| 256 |
main (int argc, char *argv[]) |
| 257 |
{ |
| 258 |
CommandLine cmd; |
| 259 |
cmd.Parse (argc, argv); |
| 260 |
|
| 261 |
UanExperiment experiment; |
| 262 |
experiment.Prepare (); |
| 263 |
|
| 264 |
Simulator::Stop (Days (30)); |
| 265 |
Simulator::Run (); |
| 266 |
Simulator::Destroy (); |
| 267 |
|
| 268 |
experiment.Teardown (); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |