A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  NodeContainer csmaNodes;
47  csmaNodes.Create (2);
48 
49  // Setup CSMA channel between the nodes
50  CsmaHelper csma;
51  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
52  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
53  NetDeviceContainer csmaDevices = csma.Install (csmaNodes);
54 
55  // Install normal internet stack on node B
56  InternetStackHelper internet;
57  internet.Install (csmaNodes.Get (1));
58 
59  // Install Click on node A
60  ClickInternetStackHelper clickinternet;
61  clickinternet.SetClickFile (csmaNodes.Get (0), "src/click/examples/nsclick-lan-single-interface.click");
62  clickinternet.SetRoutingTableElement (csmaNodes.Get (0), "rt");
63  clickinternet.Install (csmaNodes.Get (0));
64 
65  // Configure IP addresses for the nodes
66  Ipv4AddressHelper ipv4;
67  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
68  ipv4.Assign (csmaDevices);
69 
70  // Configure traffic application and sockets
71  Address LocalAddress (InetSocketAddress (Ipv4Address::GetAny (), 50000));
72  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", LocalAddress);
73  ApplicationContainer recvapp = packetSinkHelper.Install (csmaNodes.Get (1));
74  recvapp.Start (Seconds (5.0));
75  recvapp.Stop (Seconds (10.0));
76 
77  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
78  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
79  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
80 
81  ApplicationContainer appcont;
82 
83  AddressValue remoteAddress (InetSocketAddress (Ipv4Address ("172.16.1.2"), 50000));
84  onOffHelper.SetAttribute ("Remote", remoteAddress);
85  appcont.Add (onOffHelper.Install (csmaNodes.Get (0)));
86 
87  appcont.Start (Seconds (5.0));
88  appcont.Stop (Seconds (10.0));
89 
90  // For tracing
91  csma.EnablePcap ("nsclick-simple-lan", csmaDevices, false);
92 
93  Simulator::Stop (Seconds (20.0));
94  Simulator::Run ();
95 
97  return 0;
98 #else
99  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
100 #endif
101 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
hold variables of type string
Definition: string.h:19
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
hold objects of type ns3::Time
Definition: nstime.h:961
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
#define NS_LOG_UNCOND(msg)
Definition: log.h:377
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
hold objects of type ns3::Address
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
hold objects of type ns3::DataRate
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
int main(int argc, char *argv[])
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void ReceivePacket(Ptr< Socket > socket)