/* -*- 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
 */

// Network topology
//
//       n0    n1
//       |     |
//       =======
//         LAN
//
// - CBR/UDP flows from n0 to n1 and from n0 to n1, each over a different ip address.
// - DropTail queues 
// - Tracing of queues and packet receptions to file "multiple-ip-interfaces.tr"

#include <iostream>
#include <fstream>

#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/core-module.h"
#include "ns3/helper-module.h"
#include "ns3/global-route-manager.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("MultipleIpInterfacesExample");

int 
main (int argc, char *argv[])
{
#if 0 
  LogComponentEnable ("MultipleIpInterfacesExample", LOG_LEVEL_INFO);
#endif

  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);

  CommandLine cmd;
  cmd.Parse (argc, argv);

  NS_LOG_INFO ("Create nodes.");
  NodeContainer c;
  c.Create (3);

  NS_LOG_INFO ("Build Topology");
  CsmaHelper csma;
  csma.SetChannelParameter ("BitRate", DataRateValue (5000000));
  csma.SetChannelParameter ("Delay", TimeValue (MilliSeconds (2)));
  NetDeviceContainer nd0 = csma.Install (c);

  InternetStackHelper internet;
  internet.Install (c);

// We've got the "hardware" in place.  Now we need to add IP addresses.
//
  NetDeviceContainer nd1 = NetDeviceContainer (nd0.Get (0), nd0.Get (1));
  NetDeviceContainer nd2 = NetDeviceContainer (nd0.Get (0), nd0.Get (2));

  NS_LOG_INFO ("Assign IP Addresses.");
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  ipv4.Assign (nd1);

  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
  ipv4.Assign (nd2);

  GlobalRouteManager::PopulateRoutingTables ();

  NS_LOG_INFO ("Create Application 0 -> 10.1.1.2");
  uint16_t port = 9;   // Discard port (RFC 863)

  OnOffHelper onoff = OnOffHelper ("ns3::Udp", 
                                   InetSocketAddress (Ipv4Address ("10.1.1.2"), port));
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
  ApplicationContainer app = onoff.Install (c.Get (0));
  // Start the application
  app.Start (Seconds (1.0));
  app.Stop (Seconds (3.0));

  PacketSinkHelper sink = PacketSinkHelper ("ns3::Udp",
                                            InetSocketAddress (Ipv4Address::GetAny (), port));
  app = sink.Install (c.Get (1));
  app.Start (Seconds (0.0));

  NS_LOG_INFO ("Create Application 0 -> 10.1.2.2");
  onoff.SetAttribute ("Remote", AddressValue (Address (InetSocketAddress (Ipv4Address ("10.1.2.2"), port))));
  app = onoff.Install (c.Get (0));
  app.Start(Seconds (1.1));
  app.Stop (Seconds (3.0));

  app = sink.Install (c.Get (2));
  app.Start (Seconds (0.0));

  
  NS_LOG_INFO ("Configure Tracing.");
  std::ofstream ascii;
  ascii.open ("multiple-ip-interfaces.tr");
  CsmaHelper::EnableAsciiAll (ascii);
  CsmaHelper::EnablePcapAll ("multiple-ip-interfaces");


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