A Discrete-Event Network Simulator
API
main-attribute-value.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Henderson <tomh@tomh.org>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/command-line.h"
23 #include "ns3/ptr.h"
24 #include "ns3/config.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/string.h"
27 #include "ns3/pointer.h"
28 #include "ns3/simulator.h"
29 
30 #include "ns3/node.h"
31 #include "ns3/queue.h"
32 #include "ns3/drop-tail-queue.h"
33 #include "ns3/point-to-point-net-device.h"
34 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE ("AttributeValueSample");
38 
39 //
40 // This is a basic example of how to use the attribute system to
41 // set and get a value in the underlying system; namely, the maximum
42 // size of the FIFO queue in the PointToPointNetDevice
43 //
44 
45 int
46 main (int argc, char *argv[])
47 {
48  LogComponentEnable ("AttributeValueSample", LOG_LEVEL_INFO);
49 
50  // Queues in ns-3 are objects that hold items (other objects) in
51  // a queue structure. The C++ implementation uses templates to
52  // allow queues to hold various types of items, but the most
53  // common is a pointer to a packet (Ptr<Packet>).
54  //
55  // The maximum queue size can either be enforced in bytes ('b') or
56  // packets ('p'). A special type called the ns3::QueueSize can
57  // hold queue size values in either unit (bytes or packets). The
58  // queue base class ns3::QueueBase has a MaxSize attribute that can
59  // be set to a QueueSize.
60 
61  // By default, the MaxSize attribute has a value of 100 packets ('100p')
62  // (this default can be observed in the function QueueBase::GetTypeId)
63  //
64  // Here, we set it to 80 packets. We could use one of two value types:
65  // a string-based value or a QueueSizeValue value
66  Config::SetDefault ("ns3::QueueBase::MaxSize", StringValue ("80p"));
67  // The below function call is redundant
68  Config::SetDefault ("ns3::QueueBase::MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, 80)));
69 
70  // Allow the user to override any of the defaults and the above
71  // SetDefaults() at run-time, via command-line arguments
72  // For example, via "--ns3::QueueBase::MaxSize=80p"
74  // This provides yet another way to set the value from the command line:
75  cmd.AddValue ("maxSize", "ns3::QueueBase::MaxSize");
76  cmd.Parse (argc, argv);
77 
78  // Now, we will create a few objects using the low-level API
79  Ptr<Node> n0 = CreateObject<Node> ();
80 
81  Ptr<PointToPointNetDevice> net0 = CreateObject<PointToPointNetDevice> ();
82  n0->AddDevice (net0);
83 
84  Ptr<Queue<Packet> > q = CreateObject<DropTailQueue<Packet> > ();
85  net0->SetQueue (q);
86 
87  // At this point, we have created a single node (Node 0) and a
88  // single PointToPointNetDevice (NetDevice 0) and added a
89  // DropTailQueue to it.
90 
91  // Now, we can manipulate the MaxSize value of the already
92  // instantiated DropTailQueue. Here are various ways to do that.
93 
94  // We assume that a smart pointer (Ptr) to a relevant network device
95  // is in hand; here, it is the net0 pointer.
96 
97  // 1. Pointer-based access
98  //
99  // One way to change the value is to access a pointer to the
100  // underlying queue and modify its attribute.
101  //
102  // First, we observe that we can get a pointer to the (base class)
103  // queue via the PointToPointNetDevice attributes, where it is called
104  // TxQueue
105  PointerValue ptr;
106  net0->GetAttribute ("TxQueue", ptr);
107  Ptr<Queue<Packet> > txQueue = ptr.Get<Queue<Packet> > ();
108 
109  // Using the GetObject function, we can perform a safe downcast
110  // to a DropTailQueue
111  Ptr<DropTailQueue<Packet> > dtq = txQueue->GetObject <DropTailQueue<Packet> > ();
112  NS_ASSERT (dtq);
113 
114  // Next, we can get the value of an attribute on this queue
115  // We have introduced wrapper "Value" classes for the underlying
116  // data types, similar to Java wrappers around these types, since
117  // the attribute system stores values and not disparate types.
118  // Here, the attribute value is assigned to a QueueSizeValue, and
119  // the Get() method on this value produces the (unwrapped) QueueSize.
120  QueueSizeValue limit;
121  dtq->GetAttribute ("MaxSize", limit);
122  NS_LOG_INFO ("1. dtq limit: " << limit.Get ());
123 
124  // Note that the above downcast is not really needed; we could have
125  // done the same using the Ptr<Queue> even though the attribute
126  // is a member of the subclass
127  txQueue->GetAttribute ("MaxSize", limit);
128  NS_LOG_INFO ("2. txQueue limit: " << limit.Get ());
129 
130  // Now, let's set it to another value (60 packets). Let's also make
131  // use of the StringValue shorthand notation to set the size by
132  // passing in a string (the string must be a positive integer suffixed
133  // by either the 'p' or 'b' character).
134  txQueue->SetAttribute ("MaxSize", StringValue ("60p"));
135  txQueue->GetAttribute ("MaxSize", limit);
136  NS_LOG_INFO ("3. txQueue limit changed: " << limit.Get ());
137 
138  // 2. Namespace-based access
139  //
140  // An alternative way to get at the attribute is to use the configuration
141  // namespace. Here, this attribute resides on a known path in this
142  // namespace; this approach is useful if one doesn't have access to
143  // the underlying pointers and would like to configure a specific
144  // attribute with a single statement.
145  Config::Set ("/NodeList/0/DeviceList/0/TxQueue/MaxSize", StringValue ("25p"));
146  txQueue->GetAttribute ("MaxSize", limit);
147  NS_LOG_INFO ("4. txQueue limit changed through namespace: " <<
148  limit.Get ());
149 
150  // we could have also used wildcards to set this value for all nodes
151  // and all net devices (which in this simple example has the same
152  // effect as the previous Set())
153  Config::Set ("/NodeList/*/DeviceList/*/TxQueue/MaxSize", StringValue ("15p"));
154  txQueue->GetAttribute ("MaxSize", limit);
155  NS_LOG_INFO ("5. txQueue limit changed through wildcarded namespace: " <<
156  limit.Get ());
157 
159 }
Class for representing queue sizes.
Definition: queue-size.h:94
Hold variables of type string.
Definition: string.h:41
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:777
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#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:278
cmd
Definition: second.py:35
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
Use number of packets for queue size.
Definition: queue-size.h:44
LOG_INFO and above.
Definition: log.h:104
Parse command-line arguments.
Definition: command-line.h:213
QueueSize Get(void) const
Definition: queue-size.cc:29
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
Ptr< T > Get(void) const
Definition: pointer.h:194
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223