(OK) NS3—NS-3—manet-2015.cc


/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2005,2006,2007 INRIA
 *
 * 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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/stats-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/olsr-module.h"
#include "ns3/netanim-module.h"

#include <iostream>

NS_LOG_COMPONENT_DEFINE ("MANET");

using namespace ns3;

// Simulation parameters
int nodeSpacing = 100; // meters
int cols = 5;
int numNodes = 25;
int sourceNode = 0;
int destinationNode = 24;
int packetRate = 20; // packet per second
int packetSize = 500; // bytes
bool enablePcap = true;
bool showSimTime = true;
int duration = 600; // seconds
int seed = 1;
int run = 1;

// To show simulated time. Sent to STDERR to separation
// from other output (e.g., visualization trace)
void PrintSeconds(void) {
	std::cerr << Simulator::Now() << std::endl;
	Simulator::Schedule(Seconds(1), &PrintSeconds);
}

void RouteChange(std::string source, uint32_t size) {
	std::cout << "Routechange at " << source << ", new size: " << size << std::endl;
}

int main (int argc, char *argv[])
{
  LogComponentEnable ("MANET", LOG_LEVEL_INFO);

  // Obtain command line arguments
  CommandLine cmd;
  cmd.AddValue ("cols", "Columns of nodes", cols);
  cmd.AddValue ("numnodes", "Number of nodes", numNodes);
  cmd.AddValue ("spacing", "Spacing between neighbouring nodes", nodeSpacing);
  cmd.AddValue ("duration", "Duration of simulation", duration);
  cmd.AddValue ("seed", "Random seed for simulation", seed);
  cmd.AddValue ("run", "Simulation run", run);
  cmd.AddValue ("packetrate", "Packets transmitted per second", packetRate);
  cmd.AddValue ("packetsize", "Packet size", packetSize);
  cmd.AddValue ("sourcenode", "Number of source node", sourceNode);
  cmd.AddValue ("destinationnode", "Number of destination node", destinationNode);
  cmd.AddValue ("showtime", "Whether or not to show simulation as simulation progresses (default = true)", showSimTime);
  cmd.Parse (argc,argv);
  int rows = ((int) numNodes / cols) + 1;

  // Set default parameter values
  Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));

  // Set random seed and run number
  SeedManager::SetSeed (seed);
  SeedManager::SetRun(run);

  // Create notes
  NodeContainer c;
  c.Create (numNodes);

  // Set up physical and mac layers
  WifiHelper wifi = WifiHelper::Default ();
  wifi.SetStandard (WIFI_PHY_STANDARD_80211g);
  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  wifiMac.SetType ("ns3::AdhocWifiMac");
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  YansWifiPhyHelper phy = wifiPhy;
  phy.SetChannel (wifiChannel.Create ());
  NetDeviceContainer devices = wifi.Install (phy, wifiMac, c);

  if(enablePcap)
	  wifiPhy.EnablePcap ("MANET", devices);

  // Set up routing and Internet stack
  ns3::OlsrHelper olsr;
  InternetStackHelper internet;
  internet.SetRoutingHelper(olsr);
  internet.Install (c);

  // Assign addresses
  Ipv4AddressHelper address;
  address.SetBase ("10.0.0.0", "255.255.255.0");
  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Server/Receiver
  UdpServerHelper server (4000);
  ApplicationContainer apps = server.Install (c.Get(destinationNode));
  apps.Start (Seconds (1));
  apps.Stop (Seconds (duration - 1));

  // Client/Sender
  UdpClientHelper client (interfaces.GetAddress (destinationNode), 4000);
  client.SetAttribute ("MaxPackets", UintegerValue (100000000));
  client.SetAttribute ("Interval", TimeValue (Seconds(1 / ((double) packetRate))));
  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
  apps = client.Install (c.Get (sourceNode));
  apps.Start (Seconds (1));
  apps.Stop (Seconds (duration - 1));

  // Set up mobility
  MobilityHelper mobility;
  mobility.SetPositionAllocator (
		  "ns3::GridPositionAllocator",
		  "MinX", DoubleValue (1.0),
		  "MinY", DoubleValue (1.0),
		  "DeltaX", DoubleValue (nodeSpacing),
		  "DeltaY", DoubleValue (nodeSpacing),
		  "GridWidth", UintegerValue (cols));

  mobility.SetMobilityModel (
		  "ns3::RandomWalk2dMobilityModel",
		  "Bounds", RectangleValue (Rectangle (0,(cols * nodeSpacing) + 1, 0,(rows * nodeSpacing) + 1)),
		  "Speed", StringValue("ns3::UniformRandomVariable[Min=5.0,Max=10.0]"),
		  "Distance", DoubleValue(30));

  mobility.Install (c);

  // Schedule final events and start simulation

  // Print simulated time
  if(showSimTime)
    Simulator::Schedule(Seconds(1), &PrintSeconds);


  Simulator::Stop(Seconds(duration));
  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}




posted @ 2016-05-15 14:54  张同光  阅读(175)  评论(0编辑  收藏  举报