A Discrete-Event Network Simulator
API
nsclick-simple-lan.cc
Go to the documentation of this file.
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 
39 {
40  NS_LOG_UNCOND ("Received one packet!");
41 }
42 
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 
53  csmaNodes.Create (2);
54 
55  // Setup CSMA channel between the nodes
57  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
58  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
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 
103  return 0;
104 #else
105  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
106 #endif
107 }
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
static Ipv4Address GetAny(void)
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
AttributeValue implementation for Time.
Definition: nstime.h:1308
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csmaNodes
Definition: second.py:53
csma
Definition: second.py:63
cmd
Definition: second.py:35
csmaDevices
Definition: second.py:67
void ReceivePacket(Ptr< Socket > socket)