Example #1
0
int main (int argc, char *argv[])
{
  uint32_t packetSize = 1000; // Application bytes per packet
  double interval = 1.0; // Time between events
  uint32_t generationSize = 3; // RLNC generation size
  double errorRateEncoderRecoder = 0.4; // Error rate for encoder-recoder link
  double errorRateRecoderDecoder = 0.2; // Error rate for recoder-decoder link
  bool recodingFlag = true; // Flag to control recoding

  Time interPacketInterval = Seconds (interval);

  CommandLine cmd;

  cmd.AddValue ("packetSize", "Size of application packet sent", packetSize);
  cmd.AddValue ("interval", "Interval (seconds) between packets", interval);
  cmd.AddValue ("generationSize", "Set the generation size to use",
                generationSize);
  cmd.AddValue ("errorRateEncoderRecoder",
                "Packet erasure rate for the encoder-recoder link",
                errorRateEncoderRecoder);
  cmd.AddValue ("errorRateRecoderDecoder",
                "Packet erasure rate for the recoder-decoder link",
                errorRateRecoderDecoder);
  cmd.AddValue ("recodingFlag", "Enable packet recoding", recodingFlag);
  cmd.Parse (argc, argv);

  Time::SetResolution (Time::NS);

  // Set the basic helper for a single link
  PointToPointHelper pointToPoint;

  // Create node containers
  NodeContainer nodes;
  nodes.Create (3);
  NodeContainer encoderRecoder = NodeContainer (nodes.Get (0), nodes.Get (1));
  NodeContainer recoderDecoder = NodeContainer (nodes.Get (1), nodes.Get (2));

  // Internet stack for the nodes
  InternetStackHelper internet;
  internet.Install (nodes);

  // Create net device containers
  NetDeviceContainer encoderRecoderDevs = pointToPoint.Install (encoderRecoder);
  NetDeviceContainer recoderDecoderDevs = pointToPoint.Install (recoderDecoder);

  NetDeviceContainer devices = NetDeviceContainer (encoderRecoderDevs,
                                                   recoderDecoderDevs);
  // Set IP addresses
  Ipv4AddressHelper ipv4("10.1.1.0", "255.255.255.0");
  ipv4.Assign (devices);

  // Turn on global static routing so we can actually be routed across the hops
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  // Do pcap tracing on all point-to-point devices on all nodes. File naming
  // convention is: multihop-[NODE_NUMBER]-[DEVICE_NUMBER].pcap
  pointToPoint.EnablePcapAll ("multihop");

  // Set error model for the net devices
  Config::SetDefault ("ns3::RateErrorModel::ErrorUnit",
                      StringValue ("ERROR_UNIT_PACKET"));

  Ptr<RateErrorModel> errorEncoderRecoder = CreateObject<RateErrorModel> ();
  errorEncoderRecoder->SetAttribute ("ErrorRate",
                                     DoubleValue (errorRateEncoderRecoder));
  devices.Get (1)->SetAttribute ("ReceiveErrorModel",
                                 PointerValue (errorEncoderRecoder));

  Ptr<RateErrorModel> errorRecoderDecoder = CreateObject<RateErrorModel> ();
  errorRecoderDecoder->SetAttribute ("ErrorRate",
                                     DoubleValue (errorRateRecoderDecoder));
  devices.Get (3)->SetAttribute ("ReceiveErrorModel",
                                 PointerValue (errorRecoderDecoder));
  errorEncoderRecoder->Enable ();
  errorRecoderDecoder->Enable ();

  // Creation of RLNC encoder and decoder objects
  rlnc_encoder::factory encoder_factory(generationSize, packetSize);
  rlnc_decoder::factory decoder_factory(generationSize, packetSize);

  // The member build function creates differents instances of each object
  KodoSimulation kodoSimulator(encoder_factory.build(),
                               decoder_factory.build(),
                               decoder_factory.build(),
                               recodingFlag);

  // Setting up application sockets for recoder and decoder
  uint16_t port = 80;
  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");

  // Get node Ipv4 addresses
  Ipv4Address recoderAddress = nodes.Get (1)->GetObject<Ipv4>()->
                                 GetAddress(1,0).GetLocal();
  Ipv4Address decoderAddress = nodes.Get (2)->GetObject<Ipv4>()->
                                 GetAddress(1,0).GetLocal();
  // Socket connection addresses
  InetSocketAddress recoderSocketAddress = InetSocketAddress (recoderAddress,
                                                              port);
  InetSocketAddress decoderSocketAddress = InetSocketAddress (decoderAddress,
                                                              port);
  // Socket bind address
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny(), port);

  // Encoder
  Ptr<Socket> encoderSocket = Socket::CreateSocket (nodes.Get (0), tid);
  encoderSocket->Connect (recoderSocketAddress);

  // Recoder
  Ptr<Socket> recoderSocket = Socket::CreateSocket (nodes.Get (1), tid);
  recoderSocket->Bind(local);
  recoderSocket->Connect (decoderSocketAddress);

  recoderSocket->
    SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacketRecoder,
                                   &kodoSimulator));
  // Decoder
  Ptr<Socket> decoderSocket = Socket::CreateSocket (nodes.Get (2), tid);
  decoderSocket->Bind(local);
  decoderSocket->
    SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacketDecoder,
                                   &kodoSimulator));
  // Simulation setup

  // Schedule encoding process
  Simulator::ScheduleWithContext (encoderSocket->GetNode ()->GetId (),
                                  Seconds (1.0),
                                  &KodoSimulation::SendPacketEncoder,
                                  &kodoSimulator, encoderSocket,
                                  interPacketInterval);

  Simulator::ScheduleWithContext (recoderSocket->GetNode ()->GetId (),
                                  Seconds (1.5),
                                  &KodoSimulation::SendPacketRecoder,
                                  &kodoSimulator, recoderSocket,
                                  interPacketInterval);

  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}
Example #2
0
int 
main (int argc, char *argv[])
{

  //
  // Set up some default values for the simulation.
  //
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));

  // ??? try and stick 15kb/s into the data rate
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));

  //
  // Default number of nodes in the star.  Overridable by command line argument.
  //
  uint32_t nSpokes = 8;

  CommandLine cmd;
  cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes);
  cmd.Parse (argc, argv);

  NS_LOG_INFO ("Build star topology.");
  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  PointToPointStarHelper star (nSpokes, pointToPoint);

  NS_LOG_INFO ("Install internet stack on all nodes.");
  InternetStackHelper internet;
  star.InstallStack (internet);

  NS_LOG_INFO ("Assign IP Addresses.");
  star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"));

  NS_LOG_INFO ("Create applications.");
  //
  // Create a packet sink on the star "hub" to receive packets.
  // 
  uint16_t port = 50000;
  Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
  ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
  hubApp.Start (Seconds (1.0));
  hubApp.Stop (Seconds (10.0));

  //
  // Create OnOff applications to send TCP to the hub, one on each spoke node.
  //
  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

  ApplicationContainer spokeApps;

  for (uint32_t i = 0; i < star.SpokeCount (); ++i)
    {
      AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port));
      onOffHelper.SetAttribute ("Remote", remoteAddress);
      spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
    }
  spokeApps.Start (Seconds (1.0));
  spokeApps.Stop (Seconds (10.0));

  NS_LOG_INFO ("Enable static global routing.");
  //
  // Turn on global static routing so we can actually be routed across the star.
  //
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  NS_LOG_INFO ("Enable pcap tracing.");
  //
  // Do pcap tracing on all point-to-point devices on all nodes.
  //
  pointToPoint.EnablePcapAll ("star");

  NS_LOG_INFO ("Run Simulation.");
  Simulator::Run ();
  Simulator::Destroy ();
  NS_LOG_INFO ("Done.");

  return 0;
}