/* -*- 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 "ns3/core-module.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/csma-module.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
	//LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
	//LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//CORPORATE NETWORK
	NodeContainer corporateNodes;
	corporateNodes.Create(15);//14 noduri + 1 nefolosit in trafic, doar pt legatura intre LAN
  
	CsmaHelper csmaCorporate;
	csmaCorporate.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
	csmaCorporate.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6000)));

	NetDeviceContainer csmaDevicesCorporate;
	csmaDevicesCorporate = csmaCorporate.Install (corporateNodes);
	
	InternetStackHelper stack;
	stack.Install(corporateNodes);
	
	Ipv4AddressHelper address;
	address.SetBase("10.1.1.0","255.255.255.0");
	Ipv4InterfaceContainer corporateInterfaces;
	corporateInterfaces = address.Assign(csmaDevicesCorporate);
	
//CONTROL NETWORK

	NodeContainer controlNodes;
	
	controlNodes.Create(12);
	controlNodes.Add(corporateNodes.Get(14));
  
	CsmaHelper csmaControl;
	csmaControl.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
	csmaControl.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6000)));

	NetDeviceContainer csmaDevicesControl;
	csmaDevicesControl = csmaControl.Install (controlNodes);
	
	for (int i=0;i<12;i++){
		stack.Install(controlNodes.Get(i));
	}
	
	Ipv4AddressHelper address2;
	address2.SetBase("85.1.1.0","255.255.255.0");
	Ipv4InterfaceContainer controlInterfaces;
	controlInterfaces = address2.Assign(csmaDevicesControl);

// >>>>>>>>>>>>>>>>>>>>>>>>TRAFIC>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


// trafic in CORPORATE
//1 
	UdpEchoServerHelper echoServerLAN(10);
	ApplicationContainer serverAppsLAN = echoServerLAN.Install (corporateNodes.Get (10));
	serverAppsLAN.Start (Seconds (6000.0));
	serverAppsLAN.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN (corporateInterfaces.GetAddress (10), 10);
	echoClientLAN.SetAttribute ("MaxPackets", UintegerValue (1200));
	echoClientLAN.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
	echoClientLAN.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN = echoClientLAN.Install (corporateNodes.Get (11));
	clientAppsLAN.Start (Seconds (1.0));
	clientAppsLAN.Stop (Seconds (7200.0));
//2
	ApplicationContainer serverAppsLAN2 = echoServerLAN.Install (corporateNodes.Get (10));
	serverAppsLAN2.Start (Seconds (5000.0));
	serverAppsLAN2.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN2 (corporateInterfaces.GetAddress (10), 10);
	echoClientLAN2.SetAttribute ("MaxPackets", UintegerValue (600));
	echoClientLAN2.SetAttribute ("Interval", TimeValue (Seconds (2.0)));
	echoClientLAN2.SetAttribute ("PacketSize", UintegerValue (64));

	ApplicationContainer clientAppsLAN2 = echoClientLAN2.Install (corporateNodes.Get (2));
	clientAppsLAN2.Start (Seconds (1.0));
	clientAppsLAN2.Stop (Seconds (7200.0));
//3
	ApplicationContainer serverAppsLAN3 = echoServerLAN.Install (corporateNodes.Get (1));
	serverAppsLAN3.Start (Seconds (1.0));
	serverAppsLAN3.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN3 (corporateInterfaces.GetAddress (1), 10);
	echoClientLAN3.SetAttribute ("MaxPackets", UintegerValue (72));
	echoClientLAN3.SetAttribute ("Interval", TimeValue (Seconds (100)));
	echoClientLAN3.SetAttribute ("PacketSize", UintegerValue (4096));

	ApplicationContainer clientAppsLAN3 = echoClientLAN3.Install (corporateNodes.Get (5));
	clientAppsLAN3.Start (Seconds (1.0));
	clientAppsLAN3.Stop (Seconds (7200.0));
//4
	ApplicationContainer serverAppsLAN4 = echoServerLAN.Install (corporateNodes.Get (8));
	serverAppsLAN4.Start (Seconds (1.0));
	serverAppsLAN4.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN4 (corporateInterfaces.GetAddress (8), 10);
	echoClientLAN4.SetAttribute ("MaxPackets", UintegerValue (7200));
	echoClientLAN4.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
	echoClientLAN4.SetAttribute ("PacketSize", UintegerValue (1024));

	ApplicationContainer clientAppsLAN4 = echoClientLAN4.Install (corporateNodes.Get (11));
	clientAppsLAN4.Start (Seconds (1.0));
	clientAppsLAN4.Stop (Seconds (7200.0));
	
// trafic in CONTROL
//5
	ApplicationContainer serverAppsLAN5 = echoServerLAN.Install (controlNodes.Get (10));
	serverAppsLAN5.Start (Seconds (1.0));
	serverAppsLAN5.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN5 (controlInterfaces.GetAddress (10), 10);
	echoClientLAN5.SetAttribute ("MaxPackets", UintegerValue (72000));
	echoClientLAN5.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
	echoClientLAN5.SetAttribute ("PacketSize", UintegerValue (32));

	ApplicationContainer clientAppsLAN5 = echoClientLAN5.Install (controlNodes.Get (3));
	clientAppsLAN5.Start (Seconds (1.0));
	clientAppsLAN5.Stop (Seconds (7200.0));
//6
	ApplicationContainer serverAppsLAN6 = echoServerLAN.Install (controlNodes.Get (2));
	serverAppsLAN6.Start (Seconds (1.0));
	serverAppsLAN6.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN6 (controlInterfaces.GetAddress (2), 10);
	echoClientLAN6.SetAttribute ("MaxPackets", UintegerValue (5760));
	echoClientLAN6.SetAttribute ("Interval", TimeValue (Seconds (1.25)));
	echoClientLAN6.SetAttribute ("PacketSize", UintegerValue (512));

	ApplicationContainer clientAppsLAN6 = echoClientLAN6.Install (controlNodes.Get (6));
	clientAppsLAN6.Start (Seconds (1.0));
	clientAppsLAN6.Stop (Seconds (7200.0));
//7
	ApplicationContainer serverAppsLAN7= echoServerLAN.Install (controlNodes.Get (9));
	serverAppsLAN7.Start (Seconds (1.0));
	serverAppsLAN7.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN7 (controlInterfaces.GetAddress (9), 10);
	echoClientLAN7.SetAttribute ("MaxPackets", UintegerValue (4800));
	echoClientLAN7.SetAttribute ("Interval", TimeValue (Seconds (1.5)));
	echoClientLAN7.SetAttribute ("PacketSize", UintegerValue (1024));

	ApplicationContainer clientAppsLAN7 = echoClientLAN7.Install (controlNodes.Get (1));
	clientAppsLAN7.Start (Seconds (1.0));
	clientAppsLAN7.Stop (Seconds (7200.0));
//8
	ApplicationContainer serverAppsLAN8 = echoServerLAN.Install (controlNodes.Get (0));
	serverAppsLAN8.Start (Seconds (1.0));
	serverAppsLAN8.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN8 (controlInterfaces.GetAddress (0), 10);
	echoClientLAN8.SetAttribute ("MaxPackets", UintegerValue (720));
	echoClientLAN8.SetAttribute ("Interval", TimeValue (Seconds (10)));
	echoClientLAN8.SetAttribute ("PacketSize", UintegerValue (128));

	ApplicationContainer clientAppsLAN8 = echoClientLAN8.Install (controlNodes.Get (10));
	clientAppsLAN8.Start (Seconds (1.0));
	clientAppsLAN8.Stop (Seconds (7200.0));
//trafic intre retele

//9
	ApplicationContainer serverAppsLAN9 = echoServerLAN.Install (corporateNodes.Get (10));
	serverAppsLAN9.Start (Seconds (1.0));
	serverAppsLAN9.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN9 (corporateInterfaces.GetAddress (10), 10);
	echoClientLAN9.SetAttribute ("MaxPackets", UintegerValue (7200));
	echoClientLAN9.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
	echoClientLAN9.SetAttribute ("PacketSize", UintegerValue (1024));

	ApplicationContainer clientAppsLAN9 = echoClientLAN9.Install (controlNodes.Get (4));
	clientAppsLAN9.Start (Seconds (1.0));
	clientAppsLAN5.Stop (Seconds (7200.0));
//10
	ApplicationContainer serverAppsLAN10 = echoServerLAN.Install (corporateNodes.Get (2));
	serverAppsLAN10.Start (Seconds (1.0));
	serverAppsLAN10.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN10 (corporateInterfaces.GetAddress (2), 10);
	echoClientLAN10.SetAttribute ("MaxPackets", UintegerValue (2400));
	echoClientLAN10.SetAttribute ("Interval", TimeValue (Seconds (3)));
	echoClientLAN10.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN10 = echoClientLAN10.Install (controlNodes.Get (6));
	clientAppsLAN10.Start (Seconds (1.0));
	clientAppsLAN6.Stop (Seconds (7200.0));
//11
	ApplicationContainer serverAppsLAN11= echoServerLAN.Install (controlNodes.Get (7));
	serverAppsLAN11.Start (Seconds (1.0));
	serverAppsLAN11.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN11 (controlInterfaces.GetAddress (7), 10);
	echoClientLAN11.SetAttribute ("MaxPackets", UintegerValue (1450));
	echoClientLAN11.SetAttribute ("Interval", TimeValue (Seconds (5.0)));
	echoClientLAN11.SetAttribute ("PacketSize", UintegerValue (1024));

	ApplicationContainer clientAppsLAN11 = echoClientLAN11.Install (corporateNodes.Get (1));
	clientAppsLAN11.Start (Seconds (1.0));
	clientAppsLAN7.Stop (Seconds (7200.0));
//12
	ApplicationContainer serverAppsLAN12 = echoServerLAN.Install (controlNodes.Get (10));
	serverAppsLAN12.Start (Seconds (1.0));
	serverAppsLAN12.Stop (Seconds (7200.0));
	
	UdpEchoClientHelper echoClientLAN12 (controlInterfaces.GetAddress (10), 10);
	echoClientLAN12.SetAttribute ("MaxPackets", UintegerValue (7200));
	echoClientLAN12.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
	echoClientLAN12.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN12 = echoClientLAN12.Install (corporateNodes.Get (0));
	clientAppsLAN12.Start (Seconds (1.0));
	clientAppsLAN12.Stop (Seconds (7200.0));

//maltrafic
//13
ApplicationContainer serverAppsLAN13 = echoServerLAN.Install (corporateNodes.Get (4));
	serverAppsLAN13.Start (Seconds (1000.0));
	serverAppsLAN13.Stop (Seconds (1100.0));
	
	UdpEchoClientHelper echoClientLAN13 (corporateInterfaces.GetAddress (4), 10);
	echoClientLAN13.SetAttribute ("MaxPackets", UintegerValue (2000));
	echoClientLAN13.SetAttribute ("Interval", TimeValue (Seconds (0.05)));
	echoClientLAN13.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN13 = echoClientLAN13.Install (corporateNodes.Get (3));
	clientAppsLAN13.Start (Seconds (1000.0));
	clientAppsLAN13.Stop (Seconds (1100.0));
	
//14
ApplicationContainer serverAppsLAN14 = echoServerLAN.Install (controlNodes.Get (2));
	serverAppsLAN14.Start (Seconds (1800.0));
	serverAppsLAN14.Stop (Seconds (1900.0));
	
	UdpEchoClientHelper echoClientLAN14 (controlInterfaces.GetAddress (2), 10);
	echoClientLAN14.SetAttribute ("MaxPackets", UintegerValue (1000));
	echoClientLAN14.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
	echoClientLAN14.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN14 = echoClientLAN14.Install (corporateNodes.Get (7));
	clientAppsLAN14.Start (Seconds (1800.0));
	clientAppsLAN14.Stop (Seconds (1900.0));
	
//15
	ApplicationContainer serverAppsLAN15 = echoServerLAN.Install (corporateNodes.Get (1));
	serverAppsLAN15.Start (Seconds (3000.0));
	serverAppsLAN15.Stop (Seconds (3100.0));
	
	UdpEchoClientHelper echoClientLAN15 (corporateInterfaces.GetAddress (1), 10);
	echoClientLAN15.SetAttribute ("MaxPackets", UintegerValue (1000));
	echoClientLAN15.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
	echoClientLAN15.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN15 = echoClientLAN15.Install (controlNodes.Get (1));
	clientAppsLAN15.Start (Seconds (3000.0));
	clientAppsLAN15.Stop (Seconds (3100.0));

//16
	ApplicationContainer serverAppsLAN16 = echoServerLAN.Install (controlNodes.Get (7));
	serverAppsLAN16.Start (Seconds (4500.0));
	serverAppsLAN16.Stop (Seconds (4600.0));
	
	UdpEchoClientHelper echoClientLAN16 (controlInterfaces.GetAddress (7), 10);
	echoClientLAN16.SetAttribute ("MaxPackets", UintegerValue (1000));
	echoClientLAN16.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
	echoClientLAN16.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN16 = echoClientLAN16.Install (corporateNodes.Get (7));
	clientAppsLAN16.Start (Seconds (4500.0));
	clientAppsLAN16.Stop (Seconds (4600.0));
	
//17
	ApplicationContainer serverAppsLAN17 = echoServerLAN.Install (controlNodes.Get (3));
	serverAppsLAN17.Start (Seconds (6600.0));
	serverAppsLAN17.Stop (Seconds (6700.0));
	
	UdpEchoClientHelper echoClientLAN17 (controlInterfaces.GetAddress (3), 10);
	echoClientLAN17.SetAttribute ("MaxPackets", UintegerValue (1000));
	echoClientLAN17.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
	echoClientLAN17.SetAttribute ("PacketSize", UintegerValue (256));

	ApplicationContainer clientAppsLAN17 = echoClientLAN17.Install (controlNodes.Get (1));
	clientAppsLAN17.Start (Seconds (6600.0));
	clientAppsLAN17.Stop (Seconds (6700.0));

//tracing	
	Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
	
	
	GlobalValue::Bind("ChecksumEnabled",BooleanValue(true));
	//csmaCorporate.EnablePcapAll ("5-2corporate");
	csmaCorporate.EnablePcapAll("5-2corporate");
	csmaControl.EnablePcapAll("5-2control");
    //AsciiTraceHelper ascii;
    //pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("52corporate.tr"));
    //csma.EnableAsciiAll (ascii.CreateFileStream ("52control.tr"));
	
	Simulator::Run ();
	Simulator::Destroy ();
	return 0;
}
