#include #include #include #include "ns3/core-module.h" #include "ns3/helper-module.h" #include "ns3/node-module.h" #include "ns3/common-module.h" #include "ns3/simulator-module.h" #include "ns3/ssid.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("AdhocNet"); void PhyRxOkTrace (std::string context, Ptr packet, double snr, WifiMode mode, enum WifiPreamble preamble) { std::cout << Simulator::Now ().GetSeconds () << ": " << context << ": PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl; } void PhyRxErrorTrace (std::string context, Ptr packet, double snr) { std::cout << Simulator::Now ().GetSeconds () << ": " << context << ": PHYRXERROR snr=" << snr << " " << *packet << std::endl; } int main (int argc, char *argv[]) { LogComponentEnableAll(LOG_PREFIX_TIME); uint32_t nNodes = 5; double xyDist = 65; Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2304")); Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2304")); NodeContainer wifiNodes; wifiNodes.Create (nNodes); WifiHelper wifi = WifiHelper::Default (); wifi.SetStandard (WIFI_PHY_STANDARD_80211b); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifib-11mbs")); NqosWifiMacHelper mac = NqosWifiMacHelper::Default (); Ssid ssid = Ssid ("test-ssid-test"); mac.SetType ("ns3::AdhocWifiMac", "Ssid", SsidValue (ssid)); YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); phy.Set ("EnergyDetectionThreshold", DoubleValue (-87.0)); phy.Set ("CcaMode1Threshold", DoubleValue (-90.0)); phy.Set ("TxGain", DoubleValue (1.0)); phy.Set ("RxGain", DoubleValue (1.0)); phy.Set ("RxNoiseFigure", DoubleValue (5)); phy.SetChannel (channel.Create ()); NetDeviceContainer wifiDevices = wifi.Install (phy, mac, wifiNodes); InternetStackHelper internet; internet.InstallAll (); Config::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (1)); Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1460)); Ipv4AddressHelper addresses; addresses.SetBase ("10.0.0.0", "255.255.255.0"); Ipv4InterfaceContainer ipInterfs = addresses.Assign (wifiDevices); Ipv4StaticRoutingHelper ipv4RoutingHelper; std::ostringstream srcNode, dstNode; srcNode << "10.0.0.1"; dstNode << "10.0.0." << nNodes; for (uint32_t n = 0; n < wifiNodes.GetN (); n++) { Ptr _ipv4 = wifiNodes.Get (n)->GetObject (); Ptr _staticRouting = ipv4RoutingHelper.GetStaticRouting (_ipv4); std::ostringstream nextNode, prevNode; nextNode << "10.0.0." << n+2; prevNode << "10.0.0." << n; if (n < nNodes-1) _staticRouting->AddHostRouteTo (Ipv4Address ( dstNode.str ().c_str ()), Ipv4Address ( nextNode.str ().c_str ()), 1); if (n > 0) _staticRouting->AddHostRouteTo (Ipv4Address ( srcNode.str ().c_str ()), Ipv4Address ( prevNode.str ().c_str ()), 1); } MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "GridWidth", UintegerValue (10), "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (xyDist), "DeltaY", DoubleValue (xyDist), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (wifiNodes); uint16_t port = 21; Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port)); PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress); ApplicationContainer sinkApp = sinkHelper.Install (wifiNodes.Get (nNodes-1)); sinkApp.Start (Seconds (1.0)); sinkApp.Stop (Seconds (51.0)); OnOffHelper sourceHelper ("ns3::TcpSocketFactory", Address ()); sourceHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); sourceHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); AddressValue remoteAddress (InetSocketAddress (ipInterfs.GetAddress (nNodes-1), port)); sourceHelper.SetAttribute ("Remote", remoteAddress); sourceHelper.SetAttribute ("PacketSize", UintegerValue (1460)); sourceHelper.SetAttribute ("DataRate", StringValue ("10Mb/s")); ApplicationContainer sourceApp; sourceApp.Add (sourceHelper.Install (wifiNodes.Get(0))); sourceApp.Start (Seconds (1.0)); sourceApp.Stop (Seconds (51.0)); phy.EnablePcap ("adhoc-transfer", wifiDevices); Config::Connect ("/NodeList/0/DeviceList/0/Phy/State/RxOk", MakeCallback (&PhyRxOkTrace)); Config::Connect ("/NodeList/0/DeviceList/0/Phy/State/RxError", MakeCallback (&PhyRxErrorTrace)); Simulator::Schedule (Seconds (10.0), Config::Set, "/NodeList/0/DeviceList/0/RemoteStationManager/FragmentationThreshold", StringValue ("800")); Simulator::Stop (Seconds(52.5)); Simulator::Run (); Simulator::Destroy (); return 0; }