A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
nsclick-udp-client-server-csma.cc
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 
17 // Adaptation of examples/udp/udp-client-server.cc for
18 // Click based nodes.
19 //
20 // Network topology
21 //
22 // 172.16.1.0/24
23 // (1.1) (1.2) (1.3)
24 // n0 n1 n2
25 // | | |
26 // =============
27 // LAN
28 //
29 // - UDP flows from n0 to n1 and n2 to n1
30 // - All nodes are Click based.
31 // - The single ethernet interface that each node
32 // uses is named 'eth0' in the Click file.
33 //
34 
35 #include <fstream>
36 #include "ns3/core-module.h"
37 #include "ns3/network-module.h"
38 #include "ns3/internet-module.h"
39 #include "ns3/csma-module.h"
40 #include "ns3/applications-module.h"
41 #include "ns3/ipv4-click-routing.h"
42 #include "ns3/click-internet-stack-helper.h"
43 
44 using namespace ns3;
45 
46 NS_LOG_COMPONENT_DEFINE ("NsclickUdpClientServerCsma");
47 
48 int
49 main (int argc, char *argv[])
50 {
51 #ifdef NS3_CLICK
52 //
53 // Enable logging for UdpClient and
54 //
55  LogComponentEnable ("NsclickUdpClientServerCsma", LOG_LEVEL_INFO);
56 
57 //
58 // Explicitly create the nodes required by the topology (shown above).
59 //
60  NS_LOG_INFO ("Create nodes.");
61  NodeContainer n;
62  n.Create (3);
63 
64  NS_LOG_INFO ("Create channels.");
65 //
66 // Explicitly create the channels required by the topology (shown above).
67 //
68  CsmaHelper csma;
69  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
70  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
71  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
72  NetDeviceContainer d = csma.Install (n);
73 
74 //
75 // Install Click on the nodes
76 //
77  ClickInternetStackHelper clickinternet;
78  clickinternet.SetClickFile (n, "src/click/examples/nsclick-lan-single-interface.click");
79  clickinternet.SetRoutingTableElement (n, "rt");
80  clickinternet.Install (n);
81 
82  Ipv4AddressHelper ipv4;
83 //
84 // We've got the "hardware" in place. Now we need to add IP addresses.
85 //
86  NS_LOG_INFO ("Assign IP Addresses.");
87  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
88  Ipv4InterfaceContainer i = ipv4.Assign (d);
89 
90  NS_LOG_INFO ("Create Applications.");
91 //
92 // Create one udpServer applications on node one.
93 //
94  uint16_t port = 4000;
95  UdpServerHelper server (port);
96  ApplicationContainer apps = server.Install (n.Get (1));
97  apps.Start (Seconds (1.0));
98  apps.Stop (Seconds (10.0));
99 
100 //
101 // Create one UdpClient application to send UDP datagrams from node zero to
102 // node one.
103 //
104  uint32_t MaxPacketSize = 1024;
105  Time interPacketInterval = Seconds (0.05);
106  uint32_t maxPacketCount = 320;
107  UdpClientHelper client (i.GetAddress (1), port);
108  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
109  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
110  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
111  apps = client.Install (NodeContainer (n.Get (0), n.Get (2)));
112  apps.Start (Seconds (2.0));
113  apps.Stop (Seconds (10.0));
114 
115  csma.EnablePcap ("nsclick-udp-client-server-csma", d, false);
116 
117 //
118 // Now, do the actual simulation.
119 //
120  NS_LOG_INFO ("Run Simulation.");
121  Simulator::Stop (Seconds (20.0));
122  Simulator::Run ();
124  NS_LOG_INFO ("Done.");
125 #else
126  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
127 #endif
128 }
holds a vector of ns3::Application pointers.
keep track of time unit.
Definition: nstime.h:149
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
NetDeviceContainer Install(Ptr< Node > node) const
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:264
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Class for representing data rates.
Definition: data-rate.h:71
Create a client application which sends udp packets carrying a 32bit sequence number and a 64 bit tim...
hold objects of type ns3::Time
Definition: nstime.h:700
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
Create a server application which waits for input udp packets and uses the information carried into t...
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)
Definition: simulator.cc:121
void SetAttribute(std::string name, const AttributeValue &value)
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
hold objects of type ns3::DataRate
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
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="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const