A Discrete-Event Network Simulator
API
nsclick-udp-client-server-csma.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
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
44using namespace ns3;
45
46NS_LOG_COMPONENT_DEFINE ("NsclickUdpClientServerCsma");
47
48int
49main (int argc, char *argv[])
50{
51#ifdef NS3_CLICK
52 std::string clickConfigFolder = "src/click/examples";
53
54 CommandLine cmd (__FILE__);
55 cmd.AddValue ("clickConfigFolder", "Base folder for click configuration files", clickConfigFolder);
56 cmd.Parse (argc, argv);
57
58//
59// Enable logging for UdpClient and
60//
61 LogComponentEnable ("NsclickUdpClientServerCsma", LOG_LEVEL_INFO);
62
63//
64// Explicitly create the nodes required by the topology (shown above).
65//
66 NS_LOG_INFO ("Create nodes.");
68 n.Create (3);
69
70 NS_LOG_INFO ("Create channels.");
71//
72// Explicitly create the channels required by the topology (shown above).
73//
75 csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
76 csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
77 csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
78 NetDeviceContainer d = csma.Install (n);
79
80//
81// Install Click on the nodes
82//
83 ClickInternetStackHelper clickinternet;
84 clickinternet.SetClickFile (n, clickConfigFolder + "/nsclick-lan-single-interface.click");
85 clickinternet.SetRoutingTableElement (n, "rt");
86 clickinternet.Install (n);
87
89//
90// We've got the "hardware" in place. Now we need to add IP addresses.
91//
92 NS_LOG_INFO ("Assign IP Addresses.");
93 ipv4.SetBase ("172.16.1.0", "255.255.255.0");
94 Ipv4InterfaceContainer i = ipv4.Assign (d);
95
96 NS_LOG_INFO ("Create Applications.");
97//
98// Create one udpServer applications on node one.
99//
100 uint16_t port = 4000;
101 UdpServerHelper server (port);
102 ApplicationContainer apps = server.Install (n.Get (1));
103 apps.Start (Seconds (1.0));
104 apps.Stop (Seconds (10.0));
105
106//
107// Create one UdpClient application to send UDP datagrams from node zero to
108// node one.
109//
110 uint32_t MaxPacketSize = 1024;
111 Time interPacketInterval = Seconds (0.05);
112 uint32_t maxPacketCount = 320;
113 UdpClientHelper client (i.GetAddress (1), port);
114 client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
115 client.SetAttribute ("Interval", TimeValue (interPacketInterval));
116 client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
117 apps = client.Install (NodeContainer (n.Get (0), n.Get (2)));
118 apps.Start (Seconds (2.0));
119 apps.Stop (Seconds (10.0));
120
121 csma.EnablePcap ("nsclick-udp-client-server-csma", d, false);
122
123//
124// Now, do the actual simulation.
125//
126 NS_LOG_INFO ("Run Simulation.");
127 Simulator::Stop (Seconds (20.0));
128 Simulator::Run ();
129 Simulator::Destroy ();
130 NS_LOG_INFO ("Done.");
131#else
132 NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
133#endif
134}
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 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.
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...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
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.
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
csma
Definition: second.py:63
cmd
Definition: second.py:35