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, an unsigned
42 // integer of the maximum number of packets in a queue
43 //
44 
45 int
46 main (int argc, char *argv[])
47 {
48  LogComponentEnable ("AttributeValueSample", LOG_LEVEL_INFO);
49 
50  // By default, the MaxPackets attribute has a value of 100 packets
51  // (this default can be observed in the function DropTailQueue::GetTypeId)
52  //
53  // Here, we set it to 80 packets. We could use one of two value types:
54  // a string-based value or a UintegerValue value
55  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("80"));
56  // The below function call is redundant
57  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", UintegerValue (80));
58 
59  // Allow the user to override any of the defaults and the above
60  // SetDefaults() at run-time, via command-line arguments
61  // For example, via "--ns3::DropTailQueue::MaxPackets=80"
63  // This provides yet another way to set the value from the command line:
64  cmd.AddValue ("maxPackets", "ns3::DropTailQueue::MaxPackets");
65  cmd.Parse (argc, argv);
66 
67  // Now, we will create a few objects using the low-level API
68  Ptr<Node> n0 = CreateObject<Node> ();
69 
70  Ptr<PointToPointNetDevice> net0 = CreateObject<PointToPointNetDevice> ();
71  n0->AddDevice (net0);
72 
73  Ptr<Queue> q = CreateObject<DropTailQueue> ();
74  net0->SetQueue (q);
75 
76  // At this point, we have created a single node (Node 0) and a
77  // single PointToPointNetDevice (NetDevice 0) and added a
78  // DropTailQueue to it.
79 
80  // Now, we can manipulate the MaxPackets value of the already
81  // instantiated DropTailQueue. Here are various ways to do that.
82 
83  // We assume that a smart pointer (Ptr) to a relevant network device
84  // is in hand; here, it is the net0 pointer.
85 
86  // 1. Pointer-based access
87  //
88  // One way to change the value is to access a pointer to the
89  // underlying queue and modify its attribute.
90  //
91  // First, we observe that we can get a pointer to the (base class)
92  // queue via the PointToPointNetDevice attributes, where it is called
93  // TxQueue
94  PointerValue ptr;
95  net0->GetAttribute ("TxQueue", ptr);
96  Ptr<Queue> txQueue = ptr.Get<Queue> ();
97 
98  // Using the GetObject function, we can perform a safe downcast
99  // to a DropTailQueue, where MaxPackets is a member
100  Ptr<DropTailQueue> dtq = txQueue->GetObject <DropTailQueue> ();
101  NS_ASSERT (dtq);
102 
103  // Next, we can get the value of an attribute on this queue
104  // We have introduced wrapper "Value" classes for the underlying
105  // data types, similar to Java wrappers around these types, since
106  // the attribute system stores values and not disparate types.
107  // Here, the attribute value is assigned to a Uinteger, and
108  // the Get() method on this value produces the (unwrapped) uint32_t.
109  UintegerValue limit;
110  dtq->GetAttribute ("MaxPackets", limit);
111  NS_LOG_INFO ("1. dtq limit: " << limit.Get () << " packets");
112 
113  // Note that the above downcast is not really needed; we could have
114  // done the same using the Ptr<Queue> even though the attribute
115  // is a member of the subclass
116  txQueue->GetAttribute ("MaxPackets", limit);
117  NS_LOG_INFO ("2. txQueue limit: " << limit.Get () << " packets");
118 
119  // Now, let's set it to another value (60 packets)
120  txQueue->SetAttribute ("MaxPackets", UintegerValue (60));
121  txQueue->GetAttribute ("MaxPackets", limit);
122  NS_LOG_INFO ("3. txQueue limit changed: " << limit.Get () << " packets");
123 
124  // 2. Namespace-based access
125  //
126  // An alternative way to get at the attribute is to use the configuration
127  // namespace. Here, this attribute resides on a known path in this
128  // namespace; this approach is useful if one doesn't have access to
129  // the underlying pointers and would like to configure a specific
130  // attribute with a single statement.
131  Config::Set ("/NodeList/0/DeviceList/0/TxQueue/MaxPackets", UintegerValue (25));
132  txQueue->GetAttribute ("MaxPackets", limit);
133  NS_LOG_INFO ("4. txQueue limit changed through namespace: " <<
134  limit.Get () << " packets");
135 
136  // we could have also used wildcards to set this value for all nodes
137  // and all net devices (which in this simple example has the same
138  // effect as the previous Set())
139  Config::Set ("/NodeList/*/DeviceList/*/TxQueue/MaxPackets", UintegerValue (15));
140  txQueue->GetAttribute ("MaxPackets", limit);
141  NS_LOG_INFO ("5. txQueue limit changed through wildcarded namespace: " <<
142  limit.Get () << " packets");
143 
145 }
Ptr< T > Get(void) const
Definition: pointer.h:194
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Hold variables of type string.
Definition: string.h:41
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:769
#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:201
LOG_INFO and above.
Definition: log.h:103
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
tuple cmd
Definition: second.py:35
Abstract base class for packet Queues.
Definition: queue.h:44
uint64_t Get(void) const
Definition: uinteger.cc:35
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
Hold an unsigned integer type.
Definition: uinteger.h:44
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:229
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Parse(int argc, char *argv[])
Parse the program arguments.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191
A FIFO packet queue that drops tail-end packets on overflow.