diff --git a/RELEASE_NOTES b/RELEASE_NOTES index b01c269e1..307593055 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -34,6 +34,7 @@ New user-visible features - (internet) Ipv6AddressHelper is now more pedantic but you can use more than one in a script. - (lte) UEs can now use IPv6 to send and receive traffic. - (uan) UAN module now supports IP stack +- (uan) Added some examples for running raw, IPv4, IPv6, and 6LoWPAN over UAN Bugs fixed ---------- diff --git a/src/uan/examples/uan-6lowpan-example.cc b/src/uan/examples/uan-6lowpan-example.cc new file mode 100644 index 000000000..fe9b9718f --- /dev/null +++ b/src/uan/examples/uan-6lowpan-example.cc @@ -0,0 +1,270 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Hossam Khader + */ + +#include "ns3/core-module.h" +#include "ns3/internet-module.h" +#include "ns3/node-container.h" +#include "ns3/mobility-helper.h" +#include "ns3/mobility-model.h" +#include "ns3/basic-energy-source-helper.h" +#include "ns3/energy-source-container.h" +#include "ns3/uan-helper.h" +#include "ns3/uan-channel.h" +#include "ns3/acoustic-modem-energy-model-helper.h" +#include "ns3/sixlowpan-helper.h" +#include "ns3/sixlowpan-net-device.h" + +using namespace ns3; + +/** + * + * This example shows the usage of UDP over 6LoWPAN to transfer data. + * Two nodes are sending their remaining energy percentage (1 byte) + * to a gateway node, that prints the received data. + * The transmissions are scheduled at random times to avoid collisions + * + */ + +NS_LOG_COMPONENT_DEFINE ("Uan6lowpanExample"); + + +class UanExperiment +{ +public: + UanExperiment (); + + /** + * Set the UAN nodes position + */ + void SetupPositions (); + + /** + * Set the UAN nodes energy + */ + void SetupEnergy (); + + /** + * Set the UAN nodes communication channels + */ + void SetupCommunications (); + + /** + * Set the UAN nodes communication channels + */ + void SetupApplications (); + + /** + * Send a packet from all the nodes + */ + void SendPackets (); + + /** + * Send a packet from one of the nodes + * \param node The sending node + * \param pkt The packet + * \param dst the destination + */ + void SendSinglePacket (Ptr node, Ptr pkt, Ipv6Address dst); + + /** + * Print the received packet + * \param socket The receiving socket + */ + void PrintReceivedPacket (Ptr socket); + + /** + * Prepare the experiment + */ + void Prepare (); + + /** + * Teardown the experiment + */ + void Teardown (); + +private: + NodeContainer m_nodes; //!< UAN nodes + std::map, Ptr > m_sockets; //!< send and receive sockets +}; + + +UanExperiment::UanExperiment () +{ +} + +void +UanExperiment::SetupPositions () +{ + MobilityHelper mobilityHelper; + mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobilityHelper.Install (m_nodes); + m_nodes.Get (0)->GetObject ()->SetPosition (Vector (0, 0, 0)); + m_nodes.Get (1)->GetObject ()->SetPosition (Vector (100, 0, 0)); + m_nodes.Get (2)->GetObject ()->SetPosition (Vector (-100, 0, 0)); +} + +void +UanExperiment::SetupEnergy () +{ + BasicEnergySourceHelper energySourceHelper; + energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); + energySourceHelper.Install (m_nodes); +} + +void +UanExperiment::SetupCommunications () +{ + Ptr channel = CreateObject (); + UanHelper uanHelper; + NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel); + EnergySourceContainer energySourceContainer; + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + energySourceContainer.Add ((*node)->GetObject ()->Get (0)); + node++; + } + AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; + acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); + + SixLowPanHelper sixLowPanHelper; + NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install (netDeviceContainer); + + InternetStackHelper internetStackHelper; + internetStackHelper.Install (m_nodes); + + Ipv6AddressHelper ipv6AddressHelper; + ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64)); + ipv6AddressHelper.Assign (sixlowpanNetDevices); + + node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + (*node)->GetObject ()->SetAttribute ("DAD", BooleanValue (false)); + (*node)->GetObject ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600))); + (*node)->GetObject ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000))); + node++; + } +} + +void +UanExperiment::PrintReceivedPacket (Ptr socket) +{ + Address srcAddress; + while (socket->GetRxAvailable () > 0) + { + Ptr packet = socket->RecvFrom (srcAddress); + uint8_t energyReading; + packet->CopyData (&energyReading, 1); + + if(Inet6SocketAddress::IsMatchingType (srcAddress)) + { + NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " << + Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " << + +energyReading << "%"); + } + } +} + +void +UanExperiment::SetupApplications () +{ + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory")); + if((*node)->GetObject () != NULL) + { + Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9); + m_sockets[*node]->Bind (ipv6_local); + } + + m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this)); + node++; + } +} + +void +UanExperiment::SendPackets () +{ + Ptr uniformRandomVariable = CreateObject (); + + NodeContainer::Iterator node = m_nodes.Begin (); + Ipv6Address dst = (*node)->GetObject ()->GetInterface (1)->GetAddress (1).GetAddress (); + node++; + while (node != m_nodes.End ()) + { + uint8_t energy = ((*node)->GetObject ()->Get (0)->GetEnergyFraction ()) * 100; + + Ptr pkt = Create (&energy, 1); + + double time = uniformRandomVariable->GetValue (0, 60); + Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst); + node++; + } + Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this); +} + +void +UanExperiment::SendSinglePacket (Ptr node, Ptr pkt, Ipv6Address dst) +{ + NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst ); + Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9); + m_sockets[node]->SendTo (pkt, 0, ipv6_destination); +} + +void +UanExperiment::Prepare () +{ + m_nodes.Create (3); + SetupPositions (); + SetupEnergy (); + SetupCommunications (); + SetupApplications (); + SendPackets (); +} + +void +UanExperiment::Teardown () +{ + std::map, Ptr >::iterator socket; + + for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++) + { + socket->second->Close (); + } +} + +int +main (int argc, char *argv[]) +{ + CommandLine cmd; + cmd.Parse (argc, argv); + + UanExperiment experiment; + experiment.Prepare (); + + Simulator::Stop (Days (50)); + Simulator::Run (); + Simulator::Destroy (); + + experiment.Teardown (); + + return 0; +} diff --git a/src/uan/examples/uan-ipv4-example.cc b/src/uan/examples/uan-ipv4-example.cc new file mode 100644 index 000000000..fa6052e7d --- /dev/null +++ b/src/uan/examples/uan-ipv4-example.cc @@ -0,0 +1,262 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Hossam Khader + */ + +#include "ns3/core-module.h" +#include "ns3/internet-module.h" +#include "ns3/node-container.h" +#include "ns3/mobility-helper.h" +#include "ns3/mobility-model.h" +#include "ns3/basic-energy-source-helper.h" +#include "ns3/energy-source-container.h" +#include "ns3/uan-helper.h" +#include "ns3/uan-channel.h" +#include "ns3/acoustic-modem-energy-model-helper.h" + +using namespace ns3; + +/** + * + * This example shows the usage of UDP over IPv4 to transfer data. + * Two nodes are sending their remaining energy percentage (1 byte) + * to a gateway node, that prints the received data. + * The transmissions are scheduled at random times to avoid collisions + * + */ + +NS_LOG_COMPONENT_DEFINE ("UanIpv4Example"); + + +class UanExperiment +{ +public: + UanExperiment (); + + /** + * Set the UAN nodes position + */ + void SetupPositions (); + + /** + * Set the UAN nodes energy + */ + void SetupEnergy (); + + /** + * Set the UAN nodes communication channels + */ + void SetupCommunications (); + + /** + * Set the UAN nodes communication channels + */ + void SetupApplications (); + + /** + * Send a packet from all the nodes + */ + void SendPackets (); + + /** + * Send a packet from one of the nodes + * \param node The sending node + * \param pkt The packet + * \param dst the destination + */ + void SendSinglePacket (Ptr node, Ptr pkt, Ipv4Address dst); + + /** + * Print the received packet + * \param socket The receiving socket + */ + void PrintReceivedPacket (Ptr socket); + + /** + * Prepare the experiment + */ + void Prepare (); + + /** + * Teardown the experiment + */ + void Teardown (); + +private: + NodeContainer m_nodes; //!< UAN nodes + std::map, Ptr > m_sockets; //!< send and receive sockets +}; + + +UanExperiment::UanExperiment () +{ +} + +void +UanExperiment::SetupPositions () +{ + MobilityHelper mobilityHelper; + mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobilityHelper.Install (m_nodes); + m_nodes.Get (0)->GetObject ()->SetPosition (Vector (0, 0, 0)); + m_nodes.Get (1)->GetObject ()->SetPosition (Vector (100, 0, 0)); + m_nodes.Get (2)->GetObject ()->SetPosition (Vector (-100, 0, 0)); +} + +void +UanExperiment::SetupEnergy () +{ + BasicEnergySourceHelper energySourceHelper; + energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); + energySourceHelper.Install (m_nodes); +} + +void +UanExperiment::SetupCommunications () +{ + Ptr channel = CreateObject (); + UanHelper uanHelper; + NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel); + EnergySourceContainer energySourceContainer; + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + energySourceContainer.Add ((*node)->GetObject ()->Get (0)); + node++; + } + AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; + acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); + + InternetStackHelper internetStackHelper; + internetStackHelper.Install (m_nodes); + + Ipv4AddressHelper ipv4AddressHelper; + ipv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.0"); + ipv4AddressHelper.Assign (netDeviceContainer); + node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + (*node)->GetObject ()->GetInterface (1)->GetArpCache ()->SetWaitReplyTimeout (Seconds (10)); + node++; + } +} + +void +UanExperiment::PrintReceivedPacket (Ptr socket) +{ + Address srcAddress; + while (socket->GetRxAvailable () > 0) + { + Ptr packet = socket->RecvFrom (srcAddress); + uint8_t energyReading; + packet->CopyData (&energyReading, 1); + + if(InetSocketAddress::IsMatchingType (srcAddress)) + { + NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " << + InetSocketAddress::ConvertFrom (srcAddress).GetIpv4 () << " | Energy: " << + +energyReading << "%"); + } + } +} + +void +UanExperiment::SetupApplications () +{ + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory")); + if((*node)->GetObject () != NULL) + { + InetSocketAddress ipv4_local = InetSocketAddress (Ipv4Address::GetAny (), 9); + m_sockets[*node]->Bind (ipv4_local); + } + + m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this)); + node++; + } +} + +void +UanExperiment::SendPackets () +{ + Ptr uniformRandomVariable = CreateObject (); + + NodeContainer::Iterator node = m_nodes.Begin (); + Ipv4Address dst = (*node)->GetObject ()->GetInterface (1)->GetAddress (0).GetLocal (); + node++; + while (node != m_nodes.End ()) + { + uint8_t energy = ((*node)->GetObject ()->Get (0)->GetEnergyFraction ()) * 100; + + Ptr pkt = Create (&energy, 1); + + double time = uniformRandomVariable->GetValue (0, 60); + Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst); + node++; + } + Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this); +} + +void +UanExperiment::SendSinglePacket (Ptr node, Ptr pkt, Ipv4Address dst) +{ + NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst ); + InetSocketAddress ipv4_destination = InetSocketAddress (dst, 9); + m_sockets[node]->SendTo (pkt, 0, ipv4_destination); +} + +void +UanExperiment::Prepare () +{ + m_nodes.Create (3); + SetupPositions (); + SetupEnergy (); + SetupCommunications (); + SetupApplications (); + SendPackets (); +} + +void +UanExperiment::Teardown () +{ + std::map, Ptr >::iterator socket; + + for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++) + { + socket->second->Close (); + } +} + +int +main (int argc, char *argv[]) +{ + CommandLine cmd; + cmd.Parse (argc, argv); + + UanExperiment experiment; + experiment.Prepare (); + + Simulator::Stop (Days (50)); + Simulator::Run (); + Simulator::Destroy (); + + experiment.Teardown (); + + return 0; +} diff --git a/src/uan/examples/uan-ipv6-example.cc b/src/uan/examples/uan-ipv6-example.cc new file mode 100644 index 000000000..3053a01d7 --- /dev/null +++ b/src/uan/examples/uan-ipv6-example.cc @@ -0,0 +1,265 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Hossam Khader + */ + +#include "ns3/core-module.h" +#include "ns3/internet-module.h" +#include "ns3/node-container.h" +#include "ns3/mobility-helper.h" +#include "ns3/mobility-model.h" +#include "ns3/basic-energy-source-helper.h" +#include "ns3/energy-source-container.h" +#include "ns3/uan-helper.h" +#include "ns3/uan-channel.h" +#include "ns3/acoustic-modem-energy-model-helper.h" + +using namespace ns3; + +/** + * + * This example shows the usage of UDP over IPv6 to transfer data. + * Two nodes are sending their remaining energy percentage (1 byte) + * to a gateway node, that prints the received data. + * The transmissions are scheduled at random times to avoid collisions + * + */ + +NS_LOG_COMPONENT_DEFINE ("UanIpv6Example"); + + +class UanExperiment +{ +public: + UanExperiment (); + + /** + * Set the UAN nodes position + */ + void SetupPositions (); + + /** + * Set the UAN nodes energy + */ + void SetupEnergy (); + + /** + * Set the UAN nodes communication channels + */ + void SetupCommunications (); + + /** + * Set the UAN nodes communication channels + */ + void SetupApplications (); + + /** + * Send a packet from all the nodes + */ + void SendPackets (); + + /** + * Send a packet from one of the nodes + * \param node The sending node + * \param pkt The packet + * \param dst the destination + */ + void SendSinglePacket (Ptr node, Ptr pkt, Ipv6Address dst); + + /** + * Print the received packet + * \param socket The receiving socket + */ + void PrintReceivedPacket (Ptr socket); + + /** + * Prepare the experiment + */ + void Prepare (); + + /** + * Teardown the experiment + */ + void Teardown (); + +private: + NodeContainer m_nodes; //!< UAN nodes + std::map, Ptr > m_sockets; //!< send and receive sockets +}; + + +UanExperiment::UanExperiment () +{ +} + +void +UanExperiment::SetupPositions () +{ + MobilityHelper mobilityHelper; + mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobilityHelper.Install (m_nodes); + m_nodes.Get (0)->GetObject ()->SetPosition (Vector (0, 0, 0)); + m_nodes.Get (1)->GetObject ()->SetPosition (Vector (100, 0, 0)); + m_nodes.Get (2)->GetObject ()->SetPosition (Vector (-100, 0, 0)); +} + +void +UanExperiment::SetupEnergy () +{ + BasicEnergySourceHelper energySourceHelper; + energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); + energySourceHelper.Install (m_nodes); +} + +void +UanExperiment::SetupCommunications () +{ + Ptr channel = CreateObject (); + UanHelper uanHelper; + NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel); + EnergySourceContainer energySourceContainer; + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + energySourceContainer.Add ((*node)->GetObject ()->Get (0)); + node++; + } + AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; + acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); + + InternetStackHelper internetStackHelper; + internetStackHelper.Install (m_nodes); + + Ipv6AddressHelper ipv6AddressHelper; + ipv6AddressHelper.SetBase (Ipv6Address ("2002::"), Ipv6Prefix (64)); + ipv6AddressHelper.Assign (netDeviceContainer); + + node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + (*node)->GetObject ()->SetAttribute ("DAD", BooleanValue (false)); + (*node)->GetObject ()->SetAttribute ("ReachableTime", TimeValue (Seconds (3600))); + (*node)->GetObject ()->SetAttribute ("RetransmissionTime", TimeValue (Seconds (1000))); + node++; + } +} + +void +UanExperiment::PrintReceivedPacket (Ptr socket) +{ + Address srcAddress; + while (socket->GetRxAvailable () > 0) + { + Ptr packet = socket->RecvFrom (srcAddress); + uint8_t energyReading; + packet->CopyData (&energyReading, 1); + + if(Inet6SocketAddress::IsMatchingType (srcAddress)) + { + NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " << + Inet6SocketAddress::ConvertFrom (srcAddress).GetIpv6 () << " | Energy: " << + +energyReading << "%"); + } + } +} + +void +UanExperiment::SetupApplications () +{ + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::UdpSocketFactory")); + if((*node)->GetObject () != NULL) + { + Inet6SocketAddress ipv6_local = Inet6SocketAddress (Ipv6Address::GetAny (), 9); + m_sockets[*node]->Bind (ipv6_local); + } + + m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this)); + node++; + } +} + +void +UanExperiment::SendPackets () +{ + Ptr uniformRandomVariable = CreateObject (); + + NodeContainer::Iterator node = m_nodes.Begin (); + Ipv6Address dst = (*node)->GetObject ()->GetInterface (1)->GetAddress (1).GetAddress (); + node++; + while (node != m_nodes.End ()) + { + uint8_t energy = ((*node)->GetObject ()->Get (0)->GetEnergyFraction ()) * 100; + + Ptr pkt = Create (&energy, 1); + + double time = uniformRandomVariable->GetValue (0, 60); + Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst); + node++; + } + Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this); +} + +void +UanExperiment::SendSinglePacket (Ptr node, Ptr pkt, Ipv6Address dst) +{ + NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst ); + Inet6SocketAddress ipv6_destination = Inet6SocketAddress (Ipv6Address::ConvertFrom (dst), 9); + m_sockets[node]->SendTo (pkt, 0, ipv6_destination); +} + +void +UanExperiment::Prepare () +{ + m_nodes.Create (3); + SetupPositions (); + SetupEnergy (); + SetupCommunications (); + SetupApplications (); + SendPackets (); +} + +void +UanExperiment::Teardown () +{ + std::map, Ptr >::iterator socket; + + for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++) + { + socket->second->Close (); + } +} + +int +main (int argc, char *argv[]) +{ + CommandLine cmd; + cmd.Parse (argc, argv); + + UanExperiment experiment; + experiment.Prepare (); + + Simulator::Stop (Days (50)); + Simulator::Run (); + Simulator::Destroy (); + + experiment.Teardown (); + + return 0; +} diff --git a/src/uan/examples/uan-raw-example.cc b/src/uan/examples/uan-raw-example.cc new file mode 100644 index 000000000..017ce31bb --- /dev/null +++ b/src/uan/examples/uan-raw-example.cc @@ -0,0 +1,257 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Hossam Khader + */ + +#include "ns3/core-module.h" +#include "ns3/internet-module.h" +#include "ns3/node-container.h" +#include "ns3/mobility-helper.h" +#include "ns3/mobility-model.h" +#include "ns3/basic-energy-source-helper.h" +#include "ns3/energy-source-container.h" +#include "ns3/uan-helper.h" +#include "ns3/uan-channel.h" +#include "ns3/acoustic-modem-energy-model-helper.h" +#include "ns3/packet-socket-helper.h" +#include "ns3/packet-socket-address.h" + +using namespace ns3; + +/** + * + * This example shows the usage of raw packets transfer data. + * Two nodes are sending their remaining energy percentage (1 byte) + * to a gateway node, that prints the received data. + * The transmissions are scheduled at random times to avoid collisions + * + */ + +NS_LOG_COMPONENT_DEFINE ("UanRawExample"); + + +class UanExperiment +{ +public: + UanExperiment (); + + /** + * Set the UAN nodes position + */ + void SetupPositions (); + + /** + * Set the UAN nodes energy + */ + void SetupEnergy (); + + /** + * Set the UAN nodes communication channels + */ + void SetupCommunications (); + + /** + * Set the UAN nodes communication channels + */ + void SetupApplications (); + + /** + * Send a packet from all the nodes + */ + void SendPackets (); + + /** + * Send a packet from one of the nodes + * \param node The sending node + * \param pkt The packet + * \param dst the destination + */ + void SendSinglePacket (Ptr node, Ptr pkt, Mac8Address dst); + + /** + * Print the received packet + * \param socket The receiving socket + */ + void PrintReceivedPacket (Ptr socket); + + /** + * Prepare the experiment + */ + void Prepare (); + + /** + * Teardown the experiment + */ + void Teardown (); + +private: + NodeContainer m_nodes; //!< UAN nodes + std::map, Ptr > m_sockets; //!< send and receive sockets +}; + + +UanExperiment::UanExperiment () +{ +} + +void +UanExperiment::SetupPositions () +{ + MobilityHelper mobilityHelper; + mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobilityHelper.Install (m_nodes); + m_nodes.Get (0)->GetObject ()->SetPosition (Vector (0, 0, 0)); + m_nodes.Get (1)->GetObject ()->SetPosition (Vector (100, 0, 0)); + m_nodes.Get (2)->GetObject ()->SetPosition (Vector (-100, 0, 0)); +} + +void +UanExperiment::SetupEnergy () +{ + BasicEnergySourceHelper energySourceHelper; + energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000)); + energySourceHelper.Install (m_nodes); +} + +void +UanExperiment::SetupCommunications () +{ + Ptr channel = CreateObject (); + UanHelper uanHelper; + NetDeviceContainer netDeviceContainer = uanHelper.Install (m_nodes, channel); + EnergySourceContainer energySourceContainer; + NodeContainer::Iterator node = m_nodes.Begin (); + while (node != m_nodes.End ()) + { + energySourceContainer.Add ((*node)->GetObject ()->Get (0)); + node++; + } + AcousticModemEnergyModelHelper acousticModemEnergyModelHelper; + acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer); +} + +void +UanExperiment::PrintReceivedPacket (Ptr socket) +{ + Address srcAddress; + while (socket->GetRxAvailable () > 0) + { + Ptr packet = socket->RecvFrom (srcAddress); + PacketSocketAddress packetSocketAddress = PacketSocketAddress::ConvertFrom (srcAddress); + srcAddress = packetSocketAddress.GetPhysicalAddress (); + uint8_t energyReading; + packet->CopyData (&energyReading, 1); + + if(Mac8Address::IsMatchingType (srcAddress)) + { + NS_LOG_UNCOND ( "Time: " << Simulator::Now ().GetHours () << "h" << " | Node: " << + Mac8Address::ConvertFrom (srcAddress) << " | Energy: " << + +energyReading << "%"); + } + } +} + +void +UanExperiment::SetupApplications () +{ + NodeContainer::Iterator node = m_nodes.Begin (); + PacketSocketHelper packetSocketHelper; + while (node != m_nodes.End ()) + { + packetSocketHelper.Install (*node); + PacketSocketAddress socketAddress; + socketAddress.SetSingleDevice ((*node)->GetDevice (0)->GetIfIndex ()); + socketAddress.SetProtocol (0); + m_sockets[*node] = Socket::CreateSocket (*node, TypeId::LookupByName ("ns3::PacketSocketFactory")); + m_sockets[*node]->Bind (); + m_sockets[*node]->Connect (socketAddress); + m_sockets[*node]->SetRecvCallback (MakeCallback (&UanExperiment::PrintReceivedPacket, this)); + node++; + } +} + +void +UanExperiment::SendPackets () +{ + Ptr uniformRandomVariable = CreateObject (); + + NodeContainer::Iterator node = m_nodes.Begin (); + Mac8Address dst = Mac8Address::ConvertFrom ((*node)->GetDevice (0)->GetAddress ()); + node++; + while (node != m_nodes.End ()) + { + uint8_t energy = ((*node)->GetObject ()->Get (0)->GetEnergyFraction ()) * 100; + + Ptr pkt = Create (&energy, 1); + + double time = uniformRandomVariable->GetValue (0, 60); + Simulator::Schedule (Seconds (time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst); + node++; + } + Simulator::Schedule (Hours (2), &UanExperiment::SendPackets, this); +} + +void +UanExperiment::SendSinglePacket (Ptr node, Ptr pkt, Mac8Address dst) +{ + NS_LOG_UNCOND ( Simulator::Now ().GetHours () << "h" << " packet sent to " << dst ); + PacketSocketAddress socketAddress; + socketAddress.SetSingleDevice (node->GetDevice (0)->GetIfIndex ()); + socketAddress.SetPhysicalAddress (dst); + socketAddress.SetProtocol (0); + m_sockets[node]->SendTo (pkt, 0, socketAddress); +} + +void +UanExperiment::Prepare () +{ + m_nodes.Create (3); + SetupPositions (); + SetupEnergy (); + SetupCommunications (); + SetupApplications (); + SendPackets (); +} + +void +UanExperiment::Teardown () +{ + std::map, Ptr >::iterator socket; + + for (socket = m_sockets.begin (); socket != m_sockets.end (); socket++) + { + socket->second->Close (); + } +} + +int +main (int argc, char *argv[]) +{ + CommandLine cmd; + cmd.Parse (argc, argv); + + UanExperiment experiment; + experiment.Prepare (); + + Simulator::Stop (Days (50)); + Simulator::Run (); + Simulator::Destroy (); + + experiment.Teardown (); + + return 0; +} diff --git a/src/uan/examples/wscript b/src/uan/examples/wscript index 554f7642f..165c9b0e4 100644 --- a/src/uan/examples/wscript +++ b/src/uan/examples/wscript @@ -6,3 +6,15 @@ def build(bld): obj = bld.create_ns3_program('uan-rc-example', ['internet', 'mobility', 'stats', 'applications', 'uan']) obj.source = 'uan-rc-example.cc' + + obj = bld.create_ns3_program ('uan-raw-example', ['internet', 'mobility', 'stats', 'uan']) + obj.source = 'uan-raw-example.cc' + + obj = bld.create_ns3_program ('uan-ipv4-example', ['internet', 'mobility', 'stats', 'uan']) + obj.source = 'uan-ipv4-example.cc' + + obj = bld.create_ns3_program ('uan-ipv6-example', ['internet', 'mobility', 'stats', 'uan']) + obj.source = 'uan-ipv6-example.cc' + + obj = bld.create_ns3_program ('uan-6lowpan-example', ['internet', 'mobility', 'stats', 'uan', 'sixlowpan']) + obj.source = 'uan-6lowpan-example.cc'