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  */
17 // Network topology
18 //
19 // n0 n1 n2 n3
20 // | | | |
21 // =================
22 // LAN
23 //
24 // - UDP flows from n0 to n1 and back
25 // - DropTail queues
26 // - Tracing of queues and packet receptions to file "udp-echo.tr"
28 #include <fstream>
29 #include "ns3/core-module.h"
30 #include "ns3/csma-module.h"
31 #include "ns3/applications-module.h"
32 #include "ns3/internet-module.h"
34 using namespace ns3;
36 NS_LOG_COMPONENT_DEFINE ("RealtimeUdpEchoExample");
38 int
39 main (int argc, char *argv[])
40 {
41  //
42  // Allow the user to override any of the defaults and the above Bind() at
43  // run-time, via command-line arguments
44  //
46  cmd.Parse (argc, argv);
48  //
49  // But since this is a realtime script, don't allow the user to mess with
50  // that.
51  //
52  GlobalValue::Bind ("SimulatorImplementationType",
53  StringValue ("ns3::RealtimeSimulatorImpl"));
55  //
56  // Explicitly create the nodes required by the topology (shown above).
57  //
58  NS_LOG_INFO ("Create nodes.");
59  NodeContainer n;
60  n.Create (4);
62  InternetStackHelper internet;
63  internet.Install (n);
65  //
66  // Explicitly create the channels required by the topology (shown above).
67  //
68  NS_LOG_INFO ("Create channels.");
70  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
71  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
72  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
73  NetDeviceContainer d = csma.Install (n);
75  //
76  // We've got the "hardware" in place. Now we need to add IP addresses.
77  //
78  NS_LOG_INFO ("Assign IP Addresses.");
79  Ipv4AddressHelper ipv4;
80  ipv4.SetBase ("", "");
81  Ipv4InterfaceContainer i = ipv4.Assign (d);
83  NS_LOG_INFO ("Create Applications.");
85  //
86  // Create a UdpEchoServer application on node one.
87  //
88  uint16_t port = 9; // well-known echo port number
89  UdpEchoServerHelper server (port);
90  ApplicationContainer apps = server.Install (n.Get (1));
91  apps.Start (Seconds (1.0));
92  apps.Stop (Seconds (10.0));
94  //
95  // Create a UdpEchoClient application to send UDP datagrams from node zero to
96  // node one.
97  //
98  uint32_t packetSize = 1024;
99  uint32_t maxPacketCount = 500;
100  Time interPacketInterval = Seconds (0.01);
101  UdpEchoClientHelper client (i.GetAddress (1), port);
102  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
103  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
104  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
105  apps = client.Install (n.Get (0));
106  apps.Start (Seconds (2.0));
107  apps.Stop (Seconds (10.0));
109  AsciiTraceHelper ascii;
110  csma.EnableAsciiAll (ascii.CreateFileStream ("realtime-udp-echo.tr"));
111  csma.EnablePcapAll ("realtime-udp-echo", false);
113  //
114  // Now, do the actual simulation.
115  //
116  Simulator::Stop (Seconds (11.0));
117  NS_LOG_INFO ("Run Simulation.");
118  Simulator::Run ();
120  NS_LOG_INFO ("Done.");
121 }
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:71
holds a vector of std::pair of Ptr and interface index.
Hold variables of type string.
Definition: string.h:41
Create an application which sends a UDP packet and waits for an echo of this packet.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Define a Log component with a specific name.
Definition: log.h:201
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_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
Class for representing data rates.
Definition: data-rate.h:88
Create a server application which waits for input UDP packets and sends them back to the original sen...
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
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...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:65
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
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 EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
static const uint32_t packetSize
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const