A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
realtime-udp-echo.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Network topology
17 //
18 // n0 n1 n2 n3
19 // | | | |
20 // =================
21 // LAN
22 //
23 // - UDP flows from n0 to n1 and back
24 // - DropTail queues
25 // - Tracing of queues and packet receptions to file "udp-echo.tr"
26 
27 #include "ns3/applications-module.h"
28 #include "ns3/core-module.h"
29 #include "ns3/csma-module.h"
30 #include "ns3/internet-module.h"
31 
32 #include <fstream>
33 
34 using namespace ns3;
35 
36 NS_LOG_COMPONENT_DEFINE("RealtimeUdpEchoExample");
37 
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  //
45  CommandLine cmd(__FILE__);
46  cmd.Parse(argc, argv);
47 
48  //
49  // But since this is a realtime script, don't allow the user to mess with
50  // that.
51  //
52  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
53 
54  //
55  // Explicitly create the nodes required by the topology (shown above).
56  //
57  NS_LOG_INFO("Create nodes.");
58  NodeContainer n;
59  n.Create(4);
60 
62  internet.Install(n);
63 
64  //
65  // Explicitly create the channels required by the topology (shown above).
66  //
67  NS_LOG_INFO("Create channels.");
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  // We've got the "hardware" in place. Now we need to add IP addresses.
76  //
77  NS_LOG_INFO("Assign IP Addresses.");
79  ipv4.SetBase("10.1.1.0", "255.255.255.0");
80  Ipv4InterfaceContainer i = ipv4.Assign(d);
81 
82  NS_LOG_INFO("Create Applications.");
83 
84  //
85  // Create a UdpEchoServer application on node one.
86  //
87  uint16_t port = 9; // well-known echo port number
89  ApplicationContainer apps = server.Install(n.Get(1));
90  apps.Start(Seconds(1.0));
91  apps.Stop(Seconds(10.0));
92 
93  //
94  // Create a UdpEchoClient application to send UDP datagrams from node zero to
95  // node one.
96  //
97  uint32_t packetSize = 1024;
98  uint32_t maxPacketCount = 500;
99  Time interPacketInterval = Seconds(0.01);
101  client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
102  client.SetAttribute("Interval", TimeValue(interPacketInterval));
103  client.SetAttribute("PacketSize", UintegerValue(packetSize));
104  apps = client.Install(n.Get(0));
105  apps.Start(Seconds(2.0));
106  apps.Stop(Seconds(10.0));
107 
108  AsciiTraceHelper ascii;
109  csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"));
110  csma.EnablePcapAll("realtime-udp-echo", false);
111 
112  //
113  // Now, do the actual simulation.
114  //
115  Simulator::Stop(Seconds(11.0));
116  NS_LOG_INFO("Run Simulation.");
117  Simulator::Run();
119  NS_LOG_INFO("Done.");
120 
121  return 0;
122 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
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...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
cmd
Definition: second.py:40
static const uint32_t packetSize
Packet size generated at the AP.