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