1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Authors: Lalith Suresh <suresh.lalith@gmail.com>
17  */
18 
19 // Scenario:
20 //
21 // (Click)       CSMA    (non-Click)
22 //    A   ================   B
23 // (172.16.1.1)         (172.16.1.2)
24 //    (eth0)
25 //
26 //
27 
28 #include "ns3/core-module.h"
29 #include "ns3/network-module.h"
30 #include "ns3/csma-module.h"
31 #include "ns3/internet-module.h"
32 #include "ns3/applications-module.h"
33 #include "ns3/click-internet-stack-helper.h"
34 #include "ns3/log.h"
35 
36 using namespace ns3;
37 
ReceivePacket(Ptr<Socket> socket)38 void ReceivePacket (Ptr<Socket> socket)
39 {
40   NS_LOG_UNCOND ("Received one packet!");
41 }
42 
main(int argc,char * argv[])43 int main (int argc, char *argv[])
44 {
45 #ifdef NS3_CLICK
46   std::string clickConfigFolder = "src/click/examples";
47 
48   CommandLine cmd (__FILE__);
49   cmd.AddValue ("clickConfigFolder", "Base folder for click configuration files", clickConfigFolder);
50   cmd.Parse (argc, argv);
51 
52   NodeContainer csmaNodes;
53   csmaNodes.Create (2);
54 
55   // Setup CSMA channel between the nodes
56   CsmaHelper csma;
57   csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
58   csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
59   NetDeviceContainer csmaDevices = csma.Install (csmaNodes);
60 
61   // Install normal internet stack on node B
62   InternetStackHelper internet;
63   internet.Install (csmaNodes.Get (1));
64 
65   // Install Click on node A
66   ClickInternetStackHelper clickinternet;
67   clickinternet.SetClickFile (csmaNodes.Get (0), clickConfigFolder + "/nsclick-lan-single-interface.click");
68   clickinternet.SetRoutingTableElement (csmaNodes.Get (0), "rt");
69   clickinternet.Install (csmaNodes.Get (0));
70 
71   // Configure IP addresses for the nodes
72   Ipv4AddressHelper ipv4;
73   ipv4.SetBase ("172.16.1.0", "255.255.255.0");
74   ipv4.Assign (csmaDevices);
75 
76   // Configure traffic application and sockets
77   Address LocalAddress (InetSocketAddress (Ipv4Address::GetAny (), 50000));
78   PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", LocalAddress);
79   ApplicationContainer recvapp = packetSinkHelper.Install (csmaNodes.Get (1));
80   recvapp.Start (Seconds (5.0));
81   recvapp.Stop (Seconds (10.0));
82 
83   OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
84   onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
85   onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
86 
87   ApplicationContainer appcont;
88 
89   AddressValue remoteAddress (InetSocketAddress (Ipv4Address ("172.16.1.2"), 50000));
90   onOffHelper.SetAttribute ("Remote", remoteAddress);
91   appcont.Add (onOffHelper.Install (csmaNodes.Get (0)));
92 
93   appcont.Start (Seconds (5.0));
94   appcont.Stop (Seconds (10.0));
95 
96   // For tracing
97   csma.EnablePcap ("nsclick-simple-lan", csmaDevices, false);
98 
99   Simulator::Stop (Seconds (20.0));
100   Simulator::Run ();
101 
102   Simulator::Destroy ();
103   return 0;
104 #else
105   NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
106 #endif
107 }
108