A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-names.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// n0 n1 n2 n3
8// | | | |
9// =================
10// LAN
11//
12// This program demonstrates some basic use of the Object names capability
13//
14
15#include "ns3/applications-module.h"
16#include "ns3/core-module.h"
17#include "ns3/csma-module.h"
18#include "ns3/internet-module.h"
19
20using namespace ns3;
21
22NS_LOG_COMPONENT_DEFINE("ObjectNamesExample");
23
24/// Counter of the received bytes.
26
27/**
28 * Function called when a packet is received.
29 *
30 * \param context The context.
31 * \param packet The received packet.
32 */
33void
34RxEvent(std::string context, Ptr<const Packet> packet)
35{
36 std::cout << Simulator::Now().GetSeconds() << "s " << context << " packet size "
37 << packet->GetSize() << std::endl;
38 bytesReceived += packet->GetSize();
39}
40
41int
42main(int argc, char* argv[])
43{
44 bool outputValidated = true;
45
46 CommandLine cmd(__FILE__);
47 cmd.Parse(argc, argv);
48
50 n.Create(4);
51
52 //
53 // We're going to use the zeroth node in the container as the client, and
54 // the first node as the server. Add some "human readable" names for these
55 // nodes. The names below will go into the name system as "/Names/clientZero"
56 // and "/Names/server", but note that the Add function assumes that if you
57 // omit the leading "/Names/" the remaining string is assumed to be rooted
58 // in the "/Names" namespace. The following calls,
59 //
60 // Names::Add ("clientZero", n.Get (0));
61 // Names::Add ("/Names/clientZero", n.Get (0));
62 //
63 // will produce identical results.
64 //
65 Names::Add("clientZero", n.Get(0));
66 Names::Add("/Names/server", n.Get(1));
67
68 //
69 // It is possible to rename a node that has been previously named. This is
70 // useful in automatic name generation. You can automatically generate node
71 // names such as, "node-0", "node-1", etc., and then go back and change
72 // the name of some distinguished node to another value -- "access-point"
73 // for example. We illustrate this by just changing the client's name.
74 // As is typical of the object name service, you can either provide or elide
75 // the "/Names" prefix as you choose.
76 //
77 Names::Rename("clientZero", "client");
78
80 internet.Install(n);
81
83 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
84 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
85 csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
86 NetDeviceContainer d = csma.Install(n);
87
88 //
89 // Add some human readable names for the devices we'll be interested in.
90 // We add the names to the name space "under" the nodes we created above.
91 // This has the effect of making "/Names/client/eth0" and "/Names/server/eth0".
92 // In this case, we again omit the "/Names/" prefix on one call to illustrate
93 // the shortcut.
94 //
95 Names::Add("/Names/client/eth0", d.Get(0));
96 Names::Add("server/eth0", d.Get(1));
97
98 //
99 // You can use the object names that you've assigned in calls to the Config
100 // system to set Object Attributes. For example, you can set the Mtu
101 // Attribute of a Csma devices using the object naming service. Note that
102 // in this case, the "/Names" prefix is always required since the _Config_
103 // system always expects to see a fully qualified path name.
104 //
105
106 Ptr<CsmaNetDevice> csmaNetDevice = d.Get(0)->GetObject<CsmaNetDevice>();
107 UintegerValue val;
108 csmaNetDevice->GetAttribute("Mtu", val);
109 std::cout << "MTU on device 0 before configuration is " << val.Get() << std::endl;
110
111 Config::Set("/Names/client/eth0/Mtu", UintegerValue(1234));
112
113 // Check the attribute again
114 csmaNetDevice->GetAttribute("Mtu", val);
115 std::cout << "MTU on device 0 after configuration is " << val.Get() << std::endl;
116
117 if (val.Get() != 1234)
118 {
119 outputValidated = false;
120 }
121
122 //
123 // You can mix and match names and Attributes in calls to the Config system.
124 // For example, if "eth0" is a named object, you can get to its parent through
125 // a different namespace. For example, you could use the NodeList namespace
126 // to get to the server node, and then continue seamlessly adding named objects
127 // in the path. This is not nearly as readable as the previous version, but it
128 // illustrates how you can mix and match object names and Attribute names.
129 // Note that the config path now begins with a path in the "/NodeList"
130 // namespace.
131 //
132 Config::Set("/NodeList/1/eth0/Mtu", UintegerValue(1234));
133
135 ipv4.SetBase("10.1.1.0", "255.255.255.0");
136 Ipv4InterfaceContainer i = ipv4.Assign(d);
137
138 uint16_t port = 9;
140 //
141 // Install the UdpEchoServer application on the server node using its name
142 // directly.
143 //
144 ApplicationContainer apps = server.Install("/Names/server");
145 apps.Start(Seconds(1.0));
146 apps.Stop(Seconds(10.0));
147
148 uint32_t packetSize = 1024;
149 uint32_t maxPacketCount = 1;
150 Time interPacketInterval = Seconds(1.);
152 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
153 client.SetAttribute("Interval", TimeValue(interPacketInterval));
154 client.SetAttribute("PacketSize", UintegerValue(packetSize));
155 //
156 // Install the UdpEchoClient application on the server node using its name
157 // directly.
158 //
159 apps = client.Install("/Names/client");
160 apps.Start(Seconds(2.0));
161 apps.Stop(Seconds(10.0));
162
163 //
164 // Use the Config system to connect a trace source using the object name
165 // service to specify the path. Note that in this case, the "/Names"
166 // prefix is always required since the _Config_ system always expects to
167 // see a fully qualified path name
168 //
169 Config::Connect("/Names/client/eth0/MacRx", MakeCallback(&RxEvent));
170
171 //
172 // Set up some pcap tracing on the CSMA devices. The names of the trace
173 // files will automatically correspond to the object names if present.
174 // In this case, you will find trace files called:
175 //
176 // object-names-client-eth0.pcap
177 // object-names-server-eth0.pcap
178 //
179 // since those nodes and devices have had names associated with them. You
180 // will also see:
181 //
182 // object-names-2-1.pcap
183 // object-names-3-1.pcap
184 //
185 // since nodes two and three have no associated names.
186 //
187 csma.EnablePcapAll("object-names");
188
189 //
190 // We can also create a trace file with a name we completely control by
191 // overriding a couple of default parameters.
192 //
193 csma.EnablePcap("client-device.pcap", d.Get(0), false, true);
194
195 std::cout << "Running simulation..." << std::endl;
198
199 // Expected to see ARP exchange and one packet
200 // 64 bytes (minimum Ethernet frame size) x 2, plus (1024 + 8 + 20 + 18)
201 if (bytesReceived != (64 + 64 + 1070))
202 {
203 outputValidated = false;
204 }
205
206 if (!outputValidated)
207 {
208 std::cerr << "Program internal checking failed; returning with error" << std::endl;
209 return 1;
210 }
211
212 return 0;
213}
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.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
A Device for a Csma Network Link.
Class for representing data rates.
Definition data-rate.h:78
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
static void Rename(std::string oldpath, std::string newname)
Rename a previously associated name.
Definition names.cc:772
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
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
uint16_t port
Definition dsdv-manet.cc:33
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
void Set(std::string path, const AttributeValue &value)
Definition config.cc:869
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
uint32_t bytesReceived
Counter of the received bytes.
void RxEvent(std::string context, Ptr< const Packet > packet)
Function called when a packet is received.
static const uint32_t packetSize
Packet size generated at the AP.