/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ // Generic Network topology (could contains more nodes) // // n0 n1 // +---------+ +---------+ // | UDP | | UDP | // +---------+ +---------+ // | IPv6 | | IPv6 | // +---------+ +---------+ // | 6LoWPAN | | 6LoWPAN | // +---------+ +---------+ // | CSMA | | CSMA | // +---------+ +---------+ // | | // ================ #include #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/csma-module.h" #include "ns3/applications-module.h" #include "ns3/ipv6-static-routing-helper.h" #include "ns3/ipv6-routing-table-entry.h" #include "ns3/ipv4-global-routing-helper.h" #include "ns3/ipv4-static-routing-helper.h" #include "ns3/sixlowpan-module.h" #include "ns3/netanim-module.h" #include "ns3/mobility-module.h" #include "ns3/lte-module.h" #include "ns3/lte-enb-net-device.h" #include "ns3/lte-helper.h" #include "ns3/point-to-point-helper.h" #include "ns3/config-store.h" #include "ns3/epc-helper.h" #include "ns3/network-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("Sixlowpan"); //Function for printing information about transmitted packets void TxCallback(Ptr tp) { double time=Simulator::Now().GetSeconds(); uint32_t packetSize=tp->GetSize(); uint64_t uid=tp->GetUid(); std::cout << "Udp packet sent at " << time << "s" << " with application data of " << packetSize << " bytes and uid=" << uid << std::endl; } //Function for printing information about received packets void RxCallback(Ptr rp, const Address &ad) { double time=Simulator::Now().GetSeconds(); uint32_t packetSize=rp->GetSize(); uint64_t uid=rp->GetUid(); std::cout << "Udp packet received at " << time << "s" << " with application data of " << packetSize << " bytes and uid=" << uid << std::endl; } //Main function int main (int argc, char** argv) { bool verbose = true; std::string animFile = "sixlowpan.xml"; //Vytvoření souboru pro NetAnim (zobrazeni simulace) CommandLine cmd; cmd.AddValue ("verbose", "Povoleni logovani dulezitych informaci", verbose); cmd.Parse (argc, argv); if (verbose) { //LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL); //Detailed log function LogComponentEnable("Sixlowpan", LOG_LEVEL_ALL); } NS_LOG_INFO ("Running simulation ..."); Packet::EnablePrinting (); Packet::EnableChecking (); NS_LOG_INFO ("Creating nodes ... OK"); //Definition of number of nodes NodeContainer net; net.Create(10); Ptr MTCGNode = net.Get(9); InternetStackHelper internetv6; internetv6.Install (net); NS_LOG_INFO ("Creating communication channel ... OK"); //Definition of communication channel CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); NetDeviceContainer zarizeni = csma.Install (net); csma.SetDeviceAttribute ("Mtu", UintegerValue (150)); //Definition of 6LoWPAN SixLowPanHelper sixlowpan; sixlowpan.SetDeviceAttribute ("ForceEtherType", BooleanValue (true) ); NetDeviceContainer six = sixlowpan.Install (zarizeni); //Definition of mobility NS_LOG_INFO ("Node arrangement ... OK"); MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (1.0), "MinY", DoubleValue (1.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (6.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (net); AnimationInterface::SetConstantPosition (MTCGNode, 6, 20); NS_LOG_INFO ("IPv6 addressing ... OK"); Ipv6AddressHelper ipv6; ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64)); Ipv6InterfaceContainer deviceInterfaces; deviceInterfaces = ipv6.Assign (six); Ipv6StaticRoutingHelper ipv6RoutingHelper; Ipv6Address MTCGAddress = deviceInterfaces.GetAddress(9, 1); // Definition of OnOff application. uint16_t port=9; for (int k = 1; k < 10; k++) { OnOffHelper onoff("ns3::UdpSocketFactory", Address (Inet6SocketAddress(deviceInterfaces.GetAddress(k,1), port))); onoff.SetAttribute ("PacketSize", UintegerValue (70)); ApplicationContainer apps = onoff.Install(net.Get(1)); apps.Start (Seconds (5.0)); apps.Stop (Seconds (15.0)); PacketSinkHelper sink("ns3::UdpSocketFactory", Address (Inet6SocketAddress(deviceInterfaces.GetAddress(k,1), port))); ApplicationContainer clientApps = sink.Install(net.Get(k)); clientApps.Start (Seconds (1.0)); clientApps.Stop (Seconds (16.0)); } /* NS_LOG_INFO ("Data traffic definition ... OK"); //Data traffic definition UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install(net.Get (9)); serverApps.Start (Seconds (0.0)); serverApps.Stop (Seconds (10.0)); uint16_t echoPort = 9; UdpEchoClientHelper echoClientHelper (Ipv6Address (MTCGAddress), echoPort); echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (10)); echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (0.1))); echoClientHelper.SetAttribute ("PacketSize", UintegerValue (70)); ApplicationContainer pingApps; echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.001))); for (int k = 0; k < 9; k++) { pingApps.Add (echoClientHelper.Install (net.Get (k))); } */ Config::ConnectWithoutContext("/NodeList/0/ApplicationList/0/$ns3::OnOffApplication/Tx", MakeCallback(&TxCallback)); Config::ConnectWithoutContext("/NodeList/1/ApplicationList/0/$ns3::PacketSink/Rx", MakeCallback(&RxCallback)); NS_LOG_INFO ("Creating tracing files ... OK"); //Tracing outputs AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("sixlowpan.tr")); csma.EnablePcapAll (std::string ("sixlowpan"), true); //NetAnim AnimationInterface anim (animFile); anim.EnablePacketMetadata (true); anim.EnablePacketMetadata (true); anim.UpdateNodeDescription(MTCGNode, "MTCG Node"); //Simulation settings Simulator::Stop (Seconds (100)); NS_LOG_INFO ("Simulation run"); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Simulation stop"); }