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