A Discrete-Event Network Simulator
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
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  */
19 // Scenario:
20 //
21 // (Click) CSMA (non-Click)
22 // A ================ B
23 // ( (
24 // (eth0)
25 //
26 //
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"
36 using namespace ns3;
39 {
40  NS_LOG_UNCOND ("Received one packet!");
41 }
43 int main (int argc, char *argv[])
44 {
45 #ifdef NS3_CLICK
46  std::string clickConfigFolder = "src/click/examples";
49  cmd.AddValue ("clickConfigFolder", "Base folder for click configuration files", clickConfigFolder);
50  cmd.Parse (argc, argv);
53  csmaNodes.Create (2);
55  // Setup CSMA channel between the nodes
57  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
58  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
59  NetDeviceContainer csmaDevices = csma.Install (csmaNodes);
61  // Install normal internet stack on node B
62  InternetStackHelper internet;
63  internet.Install (csmaNodes.Get (1));
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));
71  // Configure IP addresses for the nodes
72  Ipv4AddressHelper ipv4;
73  ipv4.SetBase ("", "");
74  ipv4.Assign (csmaDevices);
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));
83  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
84  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
85  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
87  ApplicationContainer appcont;
89  AddressValue remoteAddress (InetSocketAddress (Ipv4Address (""), 50000));
90  onOffHelper.SetAttribute ("Remote", remoteAddress);
91  appcont.Add (onOffHelper.Install (csmaNodes.Get (0)));
93  appcont.Start (Seconds (5.0));
94  appcont.Stop (Seconds (10.0));
96  // For tracing
97  csma.EnablePcap ("nsclick-simple-lan", csmaDevices, false);
99  Simulator::Stop (Seconds (20.0));
100  Simulator::Run ();
103  return 0;
104 #else
105  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
106 #endif
107 }
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:71
Hold variables of type string.
Definition: string.h:41
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1001
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:217
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
tuple csmaNodes
Definition: second.py:53
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
tuple csmaDevices
Definition: second.py:67
AttributeValue implementation for Time.
Definition: nstime.h:1055
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...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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)
Output the requested message unconditionaly.
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:40
AttributeValue implementation for Address.
Definition: address.h:278
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...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void Parse(int argc, char *argv[])
Parse the program arguments.
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.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63
void ReceivePacket(Ptr< Socket > socket)