/* -*- 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
 */
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/packet-sink.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/netanim-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/netanim-module.h"
#include "ns3/applications-module.h"
#include "ns3/animation-interface.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/flow-monitor-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include "ns3/simulator.h"
#include "ns3/nstime.h"
#include "ns3/command-line.h"
#include <iostream>
/*
using namespace ns3;
int X[5];
class MyModel {
public:
 void Start (void);
Â
private:
 void DealWithEvent (double eventValue);
};
void
MyModel::Start (void)
{
 Simulator::Schedule (Seconds (0.0),
                      &MyModel::DealWithEvent,
                      this, Simulator::Now ().GetSeconds ());
}
void
MyModel::DealWithEvent (double value)
{
 //std::cout << "Member method received event at " << Simulator::Now ().GetSeconds ()
           //<< "s started at " << value << "s" << std::endl;
    std::cout << "entrer le cout entre les liens MErci "<<std::endl;;
      //X[i] =((double) rand() / (RAND_MAX)) + 1 - 1;
      ::X[0]=rand()% 10;
      std::cout << ::X[0];
      ::X[1]=rand()% 10;
         ::X[2]=rand()% 10;
     Â
  Â
          Â
          Â
          Â
}
static void
random_function (MyModel *model)
{
 std::cout << "random function received event at " <<
     Simulator::Now ().GetSeconds () << "s" << std::endl;
 model->Start ();
Â
}
*/
int X[5];
using namespace std;
using namespace ns3;
void entrer();
void premier();
void deux();
  Â
   int D[5];
   int F[5];
   int P[5];
NS_LOG_COMPONENT_DEFINE ("MyFirstScript");
int main(int argc, char** argv)
{
Â
// a varier doit etre dynamique
Â
Â
Â
   uint16_t UDPport = 9;
std::string DataRate ("2Mbps");
 //double interval = 0.05;
 uint32_t PacketSize = 512;
 /*
 CommandLine cmd;
 cmd.AddValue ("latency", "P2P link Latency in miliseconds", lat);
 cmd.AddValue ("rate", "P2P data rate in bps", rate);
 cmd.AddValue ("interval", "UDP client packet interval D=0.9/I ", interval);
 cmd.AddValue ("packetsize", "size of all the packets", packetsize);
 cmd.AddValue ("maxpackets", "maximum number of packets", maxpackets);
*/
Â
 // Users may find it convenient to turn on explicit debugging
 // for selected modules; the below lines suggest how to do this
#if 0
 LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
#endif
 // LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
 // LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
 // Set up some default values for the simulation. Use the
  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(PacketSize));
   Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue(DataRate));;
 // Allow the user to override any of the defaults and the above
 // Bind ()s at run-time, via command-line arguments
 //CommandLine cmd;
 //cmd.Parse (argc, argv);
  NS_LOG_INFO ("Create nodes."); Â
 Â
 std::string animFile = "sma.xml";
  NodeContainer c;
 Time::SetResolution (Time::NS);
 NS_LOG_INFO ("Create nodes.");
Â
 NodeContainer nodes;
 c.Create (7);
Â
 // doit etre dynamique
 NodeContainer n0n1 = NodeContainer (c.Get (0), c.Get (1));Â
 NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
 NodeContainer n1n3 = NodeContainer (c.Get (1), c.Get (3));
 NodeContainer n1n4 = NodeContainer (c.Get (1), c.Get (4));
 NodeContainer n2n5 = NodeContainer (c.Get (2), c.Get (5));
 NodeContainer n3n5 = NodeContainer (c.Get (3), c.Get (5));
 NodeContainer n4n5 = NodeContainer (c.Get (4), c.Get (5)); Â
 NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
 InternetStackHelper stack;
 stack.Install (nodes);
// We create the channels first without any IP addressing information
 NS_LOG_INFO ("Create channels.");
 PointToPointHelper p2p;
 p2p.SetDeviceAttribute("DataRate", StringValue("1Mbps"));
   p2p.SetChannelAttribute("Delay", StringValue("10ms"));
 NetDeviceContainer d0d1 = p2p.Install (n0n1);
 NetDeviceContainer d5d6 = p2p.Install (n5n6);
Â
Â
 Â
 NetDeviceContainer d1d2 = p2p.Install (n1n2);
 NetDeviceContainer d1d3 = p2p.Install (n1n3);
 NetDeviceContainer d1d4 = p2p.Install (n1n4);
  Â
 NetDeviceContainer d2d5 = p2p.Install (n2n5);
 NetDeviceContainer d3d5 = p2p.Install (n3n5);
 NetDeviceContainer d4d5 = p2p.Install (n4n5);
 InternetStackHelper internet;
 internet.Install (c);
entrer();
 // Later, we add IP addresses.
 NS_LOG_INFO ("Assign IP Addresses.");
 Ipv4AddressHelper ipv4;
 ipv4.SetBase ("10.0.0.0", "255.255.255.0");
 Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);
 Ipv4InterfaceContainer i = i0i1;
  i0i1.SetMetric (0,1);
  // std::cout << std::fixed <<"la métric entre nodes 0 -- 1 est"<< X[0]<<endl;
 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
 Ipv4InterfaceContainer i1i2 =ipv4.Assign (d1d2);
 Ipv4InterfaceContainer i1i3 =ipv4.Assign (d1d3);
 Ipv4InterfaceContainer i1i4 =ipv4.Assign (d1d4);
Â
// dynalique
   // MyModel model;
 //Simulator::Schedule (Seconds (10.0), &random_function, &model);
 Â
 Â
   entrer();
   Simulator::Schedule(Seconds(10), &entrer); Simulator::Schedule(Seconds(15), &entrer);
  Â
  Simulator::Schedule (Seconds (20),
 &Ipv4GlobalRoutingHelper::RecomputeRoutingTables);
Â
 i1i2.SetMetric (1,::X[0]);
 //i1i2.SetMetric (1,X[1]);
 cout <<"la métric entre 1 --- 2 est"<< X[0] <<endl;
    Â
 //std::cout << std::fixed <<"la métric entre 1 --- 2 est"<< X[0]<<endl;
 i1i3.SetMetric (1,::X[1]);
 cout <<"la métric entre 1 --- 3 est"<< X[1]<<endl;
  // std::cout << std::fixed <<"la métric entre 1 --- 3 est"<< X[1]<<endl;
   i1i4.SetMetric (1,::X[2]);
 cout <<"la métric entre 1 --- 4 est"<< X[2]<<endl;
 //std::cout << std::fixed <<"la métric entre 1 --- 4 est"<< X[2]<<endl;
Â
 ipv4.SetBase ("10.2.2.0", "255.255.255.0");
 Ipv4InterfaceContainer i2i5 = ipv4.Assign (d2d5);
 Ipv4InterfaceContainer i3i5 = ipv4.Assign(d3d5);
 Ipv4InterfaceContainer i4i5 = ipv4.Assign (d4d5);
Â
 i2i5.SetMetric (0, 2);
 // i2i5.SetMetric (1, X[3]);
 //cout <<"la métric entre 2 --- 5 est"<< X[4]<<endl;
Â
 i3i5.SetMetric (0,2);
 // i3i5.SetMetric (1, X[4]);
  //cout <<"la métric entre 3 --- 5 est"<< X[5]<<endl;
 i4i5.SetMetric (0,1);
 // i4i5.SetMetric (1, X[5]);
  //cout <<"la métric entre 4 --- 5 est"<< X[6]<<endl;
 ipv4.SetBase ("10.3.3.0", "255.255.255.0");
 Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
 i5i6.SetMetric (0, 1);
  //cout <<"la métric entre 5 --- 6 est"<< X[7]<<endl;
// Create router nodes, initialize routing database and set up the routing
 // tables in the nodes.
 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  GlobalRouteManager::BuildGlobalRoutingDatabase ();
  GlobalRouteManager::InitializeRoutes ();
  Simulator::Schedule (Seconds (10),
 &Ipv4GlobalRoutingHelper::RecomputeRoutingTables);
Â
//---------------------------------------On-Off-Application------------------------
 PacketSinkHelper UDPsink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), UDPport));
   ApplicationContainer App;
   NodeContainer SourceNode = NodeContainer(c.Get(0));
   NodeContainer SinkNode = NodeContainer(c.Get(6));
   App = UDPsink.Install(SinkNode);
   App.Start(Seconds(0.0));
   App.Stop(Seconds(120.0));
   Address D_Address(InetSocketAddress(i5i6.GetAddress(1), UDPport));
   OnOffHelper UDPsource("ns3::UdpSocketFactory", D_Address);
   UDPsource.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
   UDPsource.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));  Â
   App = UDPsource.Install(SourceNode);
   App.Start(Seconds(0.0));
   App.Stop(Seconds(120.0));
  Â
  Â
//-------------------------------------------------------------------------------------------------
Â
Â
 //
// Tracing
/*
 AsciiTraceHelper ascii;
 p2p.EnableAscii(ascii.CreateFileStream ("maSimulation.tr"), d0d1);
 p2p.EnablePcap("maSimulation", d0d1, true);
*/
// Calculate Throughput using Flowmonitor
//
 FlowMonitorHelper flowmon;
 Ptr<FlowMonitor> monitor = flowmon.InstallAll();
//
// Now, do the actual simulation.
//
 NS_LOG_INFO ("Run Simulation.");
 Simulator::Stop (Seconds(120.0));
Â
Â
Â
      AnimationInterface anim(animFile);
   Ptr<Node> n = c.Get(0);
   anim.SetConstantPosition(n, 3, 6);
   n = c.Get(1);
   anim.SetConstantPosition(n, 6, 6);
   n = c.Get(4);
   anim.SetConstantPosition(n, 9, 6);
   n = c.Get(3);
   anim.SetConstantPosition(n, 9, 9);
   n = c.Get(5);
   anim.SetConstantPosition(n, 12, 6);
   n = c.Get(6);
   anim.SetConstantPosition(n, 15, 6);
   n = c.Get(2);
   anim.SetConstantPosition(n, 9, 3);
  Â
  Â
   /*
Â
 AnimationInterface anim ("animation.xml");
 anim.SetConstantPosition (c.Get(0),3.0, 6.0);
 anim.SetConstantPosition (c.Get(1),6.0, 6.0);
 anim.SetConstantPosition (c.Get(4),9.0, 6.0);
 anim.SetConstantPosition (c.Get(3),9.0, 9.0);
 anim.SetConstantPosition (c.Get(5),12.0, 6.0);
 anim.SetConstantPosition (c.Get(6),15.0, 6.0);
 anim.SetConstantPosition (c.Get(2),9.0, 3.0);
 */
   Simulator::Run ();
  Â
    monitor->CheckForLostPackets ();
 Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
 std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
 for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
   {
    Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
     if ((t.sourceAddress=="10.1.1.1" && ( t.destinationAddress == "10.1.1.2" || t.destinationAddress == "10.1.4.2" ) ))
     {
         std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
         std::cout << " Tx Bytes:  " << i->second.txBytes << "\n";
         std::cout << " Rx Bytes:  " << i->second.rxBytes << "\n";
         std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps\n";
         std::cout << " Taux de pertes en bits :" << float(((i->second.txBytes - i->second.rxBytes)) / float(i->second.txBytes ) * 100) << " % \n";
    std::cout << " Latency :" << "unkown " << "\n";
    std::cout << " Jitter :" << "unkown " << "\n";
    std::cout << " All delay Sum :" << i->second.delaySum / 1000000000 << " sec \n";
    std::cout << " Average Delay :" << i->second.delaySum / float(i->second.rxPackets * 1000000) << " ms \n";
    std::cout << " Jitter Sum :" << i->second.jitterSum << "\n";
    std::cout << " Avg Jitter :" << i->second.jitterSum / float(i->second.rxPackets * 1000000)  << " ms \n";
    std::cout << " Last Delay (for calculating jitter) :" << i->second.lastDelay / 1000000 << " ms \n";
    std::cout << " Total number of Tx packets :" << i->second.txPackets << "\n";
    std::cout << " Total number of Rx packets :" << i->second.rxPackets << "\n";
    std::cout << " Total number of lost packets :" << i->second.txPackets - i->second.rxPackets << "\n";
    std::cout << " Ratio lost packets :" << float(100*((i->second.txPackets - i->second.rxPackets))/float(i->second.txPackets)) << "\n";
  Â
   Â
     }
    }
 monitor->SerializeToXmlFile("hcen.xml", true, true);
  Â
   Simulator::Destroy ();
 return 0;
}
void entrer ()
{
   string const nomFichier("test.txt");
   ofstream monFlux(nomFichier.c_str());
   if(monFlux)  Â
   {
 Â
       cout << "entrer le cout entre les liens MErci "<<endl;;
      //X[i] =((double) rand() / (RAND_MAX)) + 1 - 1;
      ::X[0]=rand()% 10;
      cout << X[0];
      ::X[1]=rand()%10;
      cout << X[1];
      ::X[2]=rand()%10;
   cout << X[2];
 }
}
/*
void deux()
{
   string const nomFichier("test1.txt");
   ofstream monFlux(nomFichier.c_str());
   if(monFlux)  Â
   {
    for (int i=0;i<3;i++)
    {
       cout << "entrer le cout entre les liens MErci "<<endl;;
      //X[i] =((double) rand() / (RAND_MAX)) + 1 - 1;
      X[i]=rand()% 10;
      cout << D[i];