/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 *  Copyright (c) 2007,2008, 2009 INRIA, UDcast
 *
 * 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: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
 *                              <amine.ismail@udcast.com>
 */

//
// Default network topology includes a base station (BS) and 2
// subscriber station (SS).

//      +-----+
//      | SS0 |
//      +-----+
//     10.1.1.1
//      -------
//        ((*))
//
//                  10.1.1.7
//               +------------+
//               |Base Station| ==((*))
//               +------------+
//
//        ((*))
//       -------
//      10.1.1.2
//       +-----+
//       | SS1 |
//       +-----+

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wimax-module.h"
#include "ns3/internet-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/ipcs-classifier-record.h"
#include "ns3/service-flow.h"
#include <iostream>

#include "ns3/ndnSIM-module.h"

NS_LOG_COMPONENT_DEFINE ("WimaxSimpleExample");

using namespace std;
namespace ns3 {
int main (int argc, char *argv[])
{
  bool verbose = false;

  int duration = 10, schedType = 0;
  WimaxHelper::SchedulerType scheduler ;
  CommandLine cmd;
  cmd.AddValue ("scheduler", "type of scheduler to use with the network devices", schedType);
  cmd.AddValue ("duration", "duration of the simulation in seconds", duration);
  cmd.AddValue ("verbose", "turn on all WimaxNetDevice log components", verbose);
  cmd.Parse (argc, argv);
  switch (schedType)
    {
    case 0:
      scheduler = WimaxHelper::SCHED_TYPE_SIMPLE;
      break;
    case 1:
      scheduler = WimaxHelper::SCHED_TYPE_MBQOS;
      break;
    case 2:
      scheduler = WimaxHelper::SCHED_TYPE_RTPS;
      break;
    default:
      scheduler = WimaxHelper::SCHED_TYPE_SIMPLE;
    }

  NodeContainer ssNodes;
  NodeContainer bsNodes;

  ssNodes.Create (2);
  bsNodes.Create (1);

  WimaxHelper wimax;

  NetDeviceContainer ssDevs, bsDevs;

  Ptr<SimpleOfdmWimaxChannel> channel;
  channel = CreateObject<SimpleOfdmWimaxChannel> ();
  channel->SetPropagationModel (SimpleOfdmWimaxChannel::COST231_PROPAGATION);
  ssDevs = wimax.Install (ssNodes,
                          WimaxHelper::DEVICE_TYPE_SUBSCRIBER_STATION,
                          WimaxHelper::SIMPLE_PHY_TYPE_OFDM,
                          channel,
                          scheduler);

 bsDevs = wimax.Install (bsNodes,
                                           WimaxHelper::DEVICE_TYPE_BASE_STATION,
                                           WimaxHelper::SIMPLE_PHY_TYPE_OFDM,
                                           channel,
                                           scheduler);


  //wimax.EnableAscii ("bs-devices", bsDevs);
  wimax.EnableAscii ("ss-devices", ssDevs);


  Ptr<BaseStationNetDevice> bs;
  bs = bsDevs.Get (0)->GetObject<BaseStationNetDevice> ();

  /**************************/
  InternetStackHelper stack;
  stack.Install (bsNodes);
  stack.Install (ssNodes);

  Ipv4Address multicastGroup ("255.255.255.255");
  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer BSinterface = address.Assign (bsDevs);
  Ipv4InterfaceContainer SSinterfaces = address.Assign (ssDevs);


  Ptr<SubscriberStationNetDevice> ss[2];

  for (int i = 0; i < 2; i++)
    {
      ss[i] = ssDevs.Get (i)->GetObject<SubscriberStationNetDevice> ();
      ss[i]->SetModulationType (WimaxPhy::MODULATION_TYPE_QAM16_12);
    }



  if (verbose)
    {
      wimax.EnableLogComponents ();  // Turn on all wimax logging
    }
  /*------------------------------*/
  



  Simulator::Stop(Seconds(10));

 	 wimax.EnablePcap ("wimax-simple-ss0", ssNodes.Get (0)->GetId (), ss[0]->GetIfIndex ());
    wimax.EnablePcap ("wimax-simple-ss1", ssNodes.Get (1)->GetId (), ss[1]->GetIfIndex ());
    wimax.EnablePcap ("wimax-simple-bs0", bsNodes.Get (0)->GetId (), bs->GetIfIndex ());





    IpcsClassifierRecord MulticastClassifier (Ipv4Address ("0.0.0.0"),
                                              Ipv4Mask ("0.0.0.0"),
											  Ipv4Address ("255.255.255.255"),
                                              Ipv4Mask ("255.255.255.255"),
                                              0,
                                              65000,
                                              100,
                                              100,
                                              17,
                                              1);

    ServiceFlow MulticastServiceFlow = wimax.CreateServiceFlow (ServiceFlow::SF_DIRECTION_DOWN,
                                                                ServiceFlow::SF_TYPE_RTPS,
                                                                MulticastClassifier);

    bs->GetServiceFlowManager ()->AddMulticastServiceFlow (MulticastServiceFlow, WimaxPhy::MODULATION_TYPE_QPSK_12);


//because wimax need to set service flow ,so i build two service flow ,
    IpcsClassifierRecord DlClassifierUgs (Ipv4Address ("0.0.0.0"),//source
                                          Ipv4Mask ("0.0.0.0"),
                                          SSinterfaces.GetAddress (0),//disc
                                          Ipv4Mask ("255.255.255.255"),
                                          0,
                                          65000,
                                          100,
                                          100,
                                          17,
                                          1);
    ServiceFlow DlServiceFlowUgs = wimax.CreateServiceFlow (ServiceFlow::SF_DIRECTION_DOWN,
                                                            ServiceFlow::SF_TYPE_RTPS,
                                                            DlClassifierUgs);

    IpcsClassifierRecord DlClassifierUgs_1 (Ipv4Address ("0.0.0.0"),//source
                                          Ipv4Mask ("0.0.0.0"),
                                          SSinterfaces.GetAddress (1),//disc
                                          Ipv4Mask ("255.255.255.255"),
                                          0,
                                          65000,
                                          100,
                                          100,
                                          17,
                                          1);
    ServiceFlow DlServiceFlowUgs_1 = wimax.CreateServiceFlow (ServiceFlow::SF_DIRECTION_DOWN,
                                                            ServiceFlow::SF_TYPE_RTPS,
                                                            DlClassifierUgs_1);

    IpcsClassifierRecord UlClassifierUgs (SSinterfaces.GetAddress (1),//source
                                          Ipv4Mask ("255.255.255.255"),
                                          Ipv4Address ("0.0.0.0"),//disc
                                          Ipv4Mask ("0.0.0.0"),
                                          0,
                                          65000,
                                          100,
                                          100,
                                          17,
                                          1);
    ServiceFlow UlServiceFlowUgs = wimax.CreateServiceFlow (ServiceFlow::SF_DIRECTION_UP,
                                                            ServiceFlow::SF_TYPE_RTPS,
                                                            UlClassifierUgs);

    IpcsClassifierRecord UlClassifierUgs_0 (SSinterfaces.GetAddress (0),//source
    									  Ipv4Mask ("255.255.255.255"),
                                          Ipv4Address ("0.0.0.0"),//disc
                                          Ipv4Mask ("0.0.0.0"),
                                          0,
                                          65000,
                                          100,
                                          100,
                                          17,
                                          1);
    ServiceFlow UlServiceFlowUgs_0 = wimax.CreateServiceFlow (ServiceFlow::SF_DIRECTION_UP,
                                                            ServiceFlow::SF_TYPE_RTPS,
                                                            UlClassifierUgs_0);
    ss[0]->AddServiceFlow (DlServiceFlowUgs);
    ss[0]->AddServiceFlow (UlServiceFlowUgs_0);
    ss[1]->AddServiceFlow (DlServiceFlowUgs_1);
    ss[1]->AddServiceFlow (UlServiceFlowUgs);

    ////////////////
      // 1. Install Wifi
      //NetDeviceContainer wifiNetDevices = wifi.Install(wifiPhyHelper, wifiMacHelper, nodes);

      // 2. Install Mobility model
      //mobility.Install(ssDevs);

      // 3. Install NDN stack
      NS_LOG_INFO("Installing NDN stack");
      ndn::StackHelper ndnHelper;
      // ndnHelper.AddNetDeviceFaceCreateCallback (WifiNetDevice::GetTypeId (), MakeCallback
      // (MyNetDeviceFaceCallback));
      ndnHelper.SetOldContentStore("ns3::ndn::cs::Lru", "MaxSize", "1000");
      ndnHelper.SetDefaultRoutes(true);
      ndnHelper.Install(bsNodes);
      ndnHelper.Install(ssNodes);

      // Set BestRoute strategy
      ndn::StrategyChoiceHelper::InstallAll("/", "/localhost/nfd/strategy/best-route");

      //ndn::StrategyChoiceHelper::Install(ssNodes, "/prefix", "/localhost/nfd/strategy/best-route");


      // 4. Set up applications
      NS_LOG_INFO("Installing Applications");

      ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
      consumerHelper.SetPrefix("/prefix");
      consumerHelper.SetAttribute("Frequency", DoubleValue(100.0));
      consumerHelper.Install(ssNodes.Get(0));
      //consumerHelper.Install(ssNodes.Get(1));
      //consumerHelper.Install(bsNodes.Get(0));

      ndn::AppHelper producerHelper("ns3::ndn::Producer");
      producerHelper.SetPrefix("/");
      producerHelper.SetAttribute("PayloadSize", StringValue("1200"));
      producerHelper.Install(ssNodes.Get(1));
      //producerHelper.Install(bsNodes);

      // Add /prefix origins to ndn::GlobalRouter
    /*  ndnGlobalRoutingHelper.AddOrigins("/prefix" ,ssNodes);

      // Calculate and install FIBs
      ndn::GlobalRoutingHelper::CalculateRoutes();*/

  NS_LOG_INFO ("Starting simulation.....");
  Simulator::Run ();

  ss[0] = 0;
  ss[1] = 0;
  bs = 0;

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

  return 0;
}
}

int
main(int argc, char* argv[])
{
  return ns3::main(argc, argv);
}
