A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
second.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "ns3/applications-module.h"
6#include "ns3/core-module.h"
7#include "ns3/csma-module.h"
8#include "ns3/internet-module.h"
9#include "ns3/ipv4-global-routing-helper.h"
10#include "ns3/network-module.h"
11#include "ns3/point-to-point-module.h"
12
13// Default Network Topology
14//
15// 10.1.1.0
16// n0 -------------- n1 n2 n3 n4
17// point-to-point | | | |
18// ================
19// LAN 10.1.2.0
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("SecondScriptExample");
24
25int
26main(int argc, char* argv[])
27{
28 bool verbose = true;
29 uint32_t nCsma = 3;
30
31 CommandLine cmd(__FILE__);
32 cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
33 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
34
35 cmd.Parse(argc, argv);
36
37 if (verbose)
38 {
39 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
40 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
41 }
42
43 nCsma = nCsma == 0 ? 1 : nCsma;
44
46 p2pNodes.Create(2);
47
49 csmaNodes.Add(p2pNodes.Get(1));
50 csmaNodes.Create(nCsma);
51
53 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
54 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
55
57 p2pDevices = pointToPoint.Install(p2pNodes);
58
60 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
61 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
62
64 csmaDevices = csma.Install(csmaNodes);
65
67 stack.Install(p2pNodes.Get(0));
68 stack.Install(csmaNodes);
69
71 address.SetBase("10.1.1.0", "255.255.255.0");
73 p2pInterfaces = address.Assign(p2pDevices);
74
75 address.SetBase("10.1.2.0", "255.255.255.0");
77 csmaInterfaces = address.Assign(csmaDevices);
78
80
82 serverApps.Start(Seconds(1.0));
83 serverApps.Stop(Seconds(10.0));
84
85 UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
86 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
87 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
88 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
89
91 clientApps.Start(Seconds(2.0));
92 clientApps.Stop(Seconds(10.0));
93
95
96 pointToPoint.EnablePcapAll("second");
97 csma.EnablePcap("second", csmaDevices.Get(1), true);
98
101 return 0;
102}
holds a vector of ns3::Application pointers.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
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:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
echoClient
Definition first.py:48
address
Definition first.py:36
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
echoServer
Definition first.py:41
clientApps
Definition first.py:53
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
p2pNodes
Definition second.py:39
p2pInterfaces
Definition second.py:64
csmaInterfaces
Definition second.py:67
csmaNodes
Definition second.py:42
p2pDevices
Definition second.py:50
nCsma
Definition second.py:27
csmaDevices
Definition second.py:56
bool verbose