A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  CommandLine cmd;
62  cmd.Parse (argc, argv);
63 
64  // Now, we will create a few objects using the low-level API
65  Ptr<Node> n0 = CreateObject<Node> ();
66 
67  Ptr<PointToPointNetDevice> net0 = CreateObject<PointToPointNetDevice> ();
68  n0->AddDevice (net0);
69 
70  Ptr<Queue> q = CreateObject<DropTailQueue> ();
71  net0->SetQueue (q);
72 
73  // At this point, we have created a single node (Node 0) and a
74  // single PointToPointNetDevice (NetDevice 0) and added a
75  // DropTailQueue to it.
76 
77  // Now, we can manipulate the MaxPackets value of the already
78  // instantiated DropTailQueue. Here are various ways to do that.
79 
80  // We assume that a smart pointer (Ptr) to a relevant network device
81  // is in hand; here, it is the net0 pointer.
82 
83  // 1. Pointer-based access
84  //
85  // One way to change the value is to access a pointer to the
86  // underlying queue and modify its attribute.
87  //
88  // First, we observe that we can get a pointer to the (base class)
89  // queue via the PointToPointNetDevice attributes, where it is called
90  // TxQueue
91  PointerValue ptr;
92  net0->GetAttribute ("TxQueue", ptr);
93  Ptr<Queue> txQueue = ptr.Get<Queue> ();
94 
95  // Using the GetObject function, we can perform a safe downcast
96  // to a DropTailQueue, where MaxPackets is a member
97  Ptr<DropTailQueue> dtq = txQueue->GetObject <DropTailQueue> ();
98  NS_ASSERT (dtq);
99 
100  // Next, we can get the value of an attribute on this queue
101  // We have introduced wrapper "Value" classes for the underlying
102  // data types, similar to Java wrappers around these types, since
103  // the attribute system stores values and not disparate types.
104  // Here, the attribute value is assigned to a Uinteger, and
105  // the Get() method on this value produces the (unwrapped) uint32_t.
106  UintegerValue limit;
107  dtq->GetAttribute ("MaxPackets", limit);
108  NS_LOG_INFO ("1. dtq limit: " << limit.Get () << " packets");
109 
110  // Note that the above downcast is not really needed; we could have
111  // done the same using the Ptr<Queue> even though the attribute
112  // is a member of the subclass
113  txQueue->GetAttribute ("MaxPackets", limit);
114  NS_LOG_INFO ("2. txQueue limit: " << limit.Get () << " packets");
115 
116  // Now, let's set it to another value (60 packets)
117  txQueue->SetAttribute ("MaxPackets", UintegerValue (60));
118  txQueue->GetAttribute ("MaxPackets", limit);
119  NS_LOG_INFO ("3. txQueue limit changed: " << limit.Get () << " packets");
120 
121  // 2. Namespace-based access
122  //
123  // An alternative way to get at the attribute is to use the configuration
124  // namespace. Here, this attribute resides on a known path in this
125  // namespace; this approach is useful if one doesn't have access to
126  // the underlying pointers and would like to configure a specific
127  // attribute with a single statement.
128  Config::Set ("/NodeList/0/DeviceList/0/TxQueue/MaxPackets", UintegerValue (25));
129  txQueue->GetAttribute ("MaxPackets", limit);
130  NS_LOG_INFO ("4. txQueue limit changed through namespace: " <<
131  limit.Get () << " packets");
132 
133  // we could have also used wildcards to set this value for all nodes
134  // and all net devices (which in this simple example has the same
135  // effect as the previous Set())
136  Config::Set ("/NodeList/*/DeviceList/*/TxQueue/MaxPackets", UintegerValue (15));
137  txQueue->GetAttribute ("MaxPackets", limit);
138  NS_LOG_INFO ("5. txQueue limit changed through wildcarded namespace: " <<
139  limit.Get () << " packets");
140 
142 }
Ptr< T > Get(void) const
Definition: pointer.h:148
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
hold variables of type string
Definition: string.h:19
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_INFO(msg)
Definition: log.h:298
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:662
Abstract base class for packet Queues.
Definition: queue.h:45
uint64_t Get(void) const
Hold an unsigned integer type.
Definition: uinteger.h:46
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
hold objects of type Ptr
Definition: pointer.h:33
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:199
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
void Parse(int argc, char *argv[])
Parse the program arguments.
int main(int argc, char *argv[])
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
A FIFO packet queue that drops tail-end packets on overflow.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311