A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
main-attribute-value.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2008 University of Washington
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Tom Henderson <tomh@tomh.org>
7
*/
8
9
#include "ns3/command-line.h"
10
#include "ns3/config.h"
11
#include "ns3/drop-tail-queue.h"
12
#include "ns3/log.h"
13
#include "ns3/node.h"
14
#include "ns3/point-to-point-net-device.h"
15
#include "ns3/pointer.h"
16
#include "ns3/ptr.h"
17
#include "ns3/queue.h"
18
#include "ns3/simulator.h"
19
#include "ns3/string.h"
20
#include "ns3/uinteger.h"
21
22
using namespace
ns3
;
23
24
NS_LOG_COMPONENT_DEFINE
(
"AttributeValueSample"
);
25
26
//
27
// This is a basic example of how to use the attribute system to
28
// set and get a value in the underlying system; namely, the maximum
29
// size of the FIFO queue in the PointToPointNetDevice
30
//
31
32
int
33
main(
int
argc,
char
* argv[])
34
{
35
LogComponentEnable
(
"AttributeValueSample"
,
LOG_LEVEL_INFO
);
36
37
// Queues in ns-3 are objects that hold items (other objects) in
38
// a queue structure. The C++ implementation uses templates to
39
// allow queues to hold various types of items, but the most
40
// common is a pointer to a packet (Ptr<Packet>).
41
//
42
// The maximum queue size can either be enforced in bytes ('b') or
43
// packets ('p'). A special type called the ns3::QueueSize can
44
// hold queue size values in either unit (bytes or packets). The
45
// DropTailQueue<Packet> class has a MaxSize attribute that can
46
// be set to a QueueSize.
47
48
// By default, the MaxSize attribute has a value of 100 packets ('100p')
49
// (this default can be observed in the function DropTail<Item>::GetTypeId)
50
//
51
// Here, we set it to 80 packets. We could use one of two value types:
52
// a string-based value or a QueueSizeValue value
53
Config::SetDefault
(
"ns3::DropTailQueue<Packet>::MaxSize"
,
StringValue
(
"80p"
));
54
// The below function call is redundant
55
Config::SetDefault
(
"ns3::DropTailQueue<Packet>::MaxSize"
,
56
QueueSizeValue
(
QueueSize
(
QueueSizeUnit::PACKETS
, 80)));
57
58
// Allow the user to override any of the defaults and the above
59
// SetDefaults() at run-time, via command-line arguments
60
// For example, via "--ns3::DropTailQueue<Packet>::MaxSize=80p"
61
CommandLine
cmd
(__FILE__);
62
// This provides yet another way to set the value from the command line:
63
cmd
.AddValue(
"maxSize"
,
"ns3::DropTailQueue<Packet>::MaxSize"
);
64
cmd
.Parse(argc, argv);
65
66
// Now, we will create a few objects using the low-level API
67
Ptr<Node>
n0 =
CreateObject<Node>
();
68
69
Ptr<PointToPointNetDevice>
net0 =
CreateObject<PointToPointNetDevice>
();
70
n0->AddDevice(net0);
71
72
Ptr<Queue<Packet>
> q =
CreateObject<DropTailQueue<Packet>
>();
73
net0->SetQueue(q);
74
75
// At this point, we have created a single node (Node 0) and a
76
// single PointToPointNetDevice (NetDevice 0) and added a
77
// DropTailQueue to it.
78
79
// Now, we can manipulate the MaxSize value of the already
80
// instantiated DropTailQueue. Here are various ways to do that.
81
82
// We assume that a smart pointer (Ptr) to a relevant network device
83
// is in hand; here, it is the net0 pointer.
84
85
// 1. Pointer-based access
86
//
87
// One way to change the value is to access a pointer to the
88
// underlying queue and modify its attribute.
89
//
90
// First, we observe that we can get a pointer to the (base class)
91
// queue via the PointToPointNetDevice attributes, where it is called
92
// TxQueue
93
PointerValue
ptr;
94
net0->GetAttribute(
"TxQueue"
, ptr);
95
Ptr<Queue<Packet>
> txQueue = ptr.
Get
<
Queue<Packet>
>();
96
97
// Using the GetObject function, we can perform a safe downcast
98
// to a DropTailQueue
99
Ptr<DropTailQueue<Packet>
> dtq = txQueue->GetObject<
DropTailQueue<Packet>
>();
100
NS_ASSERT
(dtq);
101
102
// Next, we can get the value of an attribute on this queue
103
// We have introduced wrapper "Value" classes for the underlying
104
// data types, similar to Java wrappers around these types, since
105
// the attribute system stores values and not disparate types.
106
// Here, the attribute value is assigned to a QueueSizeValue, and
107
// the Get() method on this value produces the (unwrapped) QueueSize.
108
QueueSizeValue
limit;
109
dtq->
GetAttribute
(
"MaxSize"
, limit);
110
NS_LOG_INFO
(
"1. dtq limit: "
<< limit.
Get
());
111
112
// Note that the above downcast is not really needed; we could have
113
// done the same using the Ptr<Queue> even though the attribute
114
// is a member of the subclass
115
txQueue->GetAttribute(
"MaxSize"
, limit);
116
NS_LOG_INFO
(
"2. txQueue limit: "
<< limit.
Get
());
117
118
// Now, let's set it to another value (60 packets). Let's also make
119
// use of the StringValue shorthand notation to set the size by
120
// passing in a string (the string must be a positive integer suffixed
121
// by either the 'p' or 'b' character).
122
txQueue->SetAttribute(
"MaxSize"
,
StringValue
(
"60p"
));
123
txQueue->GetAttribute(
"MaxSize"
, limit);
124
NS_LOG_INFO
(
"3. txQueue limit changed: "
<< limit.
Get
());
125
126
// 2. Namespace-based access
127
//
128
// An alternative way to get at the attribute is to use the configuration
129
// namespace. Here, this attribute resides on a known path in this
130
// namespace; this approach is useful if one doesn't have access to
131
// the underlying pointers and would like to configure a specific
132
// attribute with a single statement.
133
Config::Set
(
"/NodeList/0/DeviceList/0/TxQueue/MaxSize"
,
StringValue
(
"25p"
));
134
txQueue->GetAttribute(
"MaxSize"
, limit);
135
NS_LOG_INFO
(
"4. txQueue limit changed through namespace: "
<< limit.
Get
());
136
137
// we could have also used wildcards to set this value for all nodes
138
// and all net devices (which in this simple example has the same
139
// effect as the previous Set())
140
Config::Set
(
"/NodeList/*/DeviceList/*/TxQueue/MaxSize"
,
StringValue
(
"15p"
));
141
txQueue->GetAttribute(
"MaxSize"
, limit);
142
NS_LOG_INFO
(
"5. txQueue limit changed through wildcarded namespace: "
<< limit.
Get
());
143
144
Simulator::Destroy
();
145
146
return
0;
147
}
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::DropTailQueue
A FIFO packet queue that drops tail-end packets on overflow.
Definition
drop-tail-queue.h:22
ns3::ObjectBase::GetAttribute
void GetAttribute(std::string name, AttributeValue &value, bool permissive=false) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition
object-base.cc:239
ns3::PointerValue
AttributeValue implementation for Pointer.
Definition
pointer.h:37
ns3::PointerValue::Get
Ptr< T > Get() const
Definition
pointer.h:223
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Queue
Template class for packet Queues.
Definition
queue.h:257
ns3::QueueSize
Class for representing queue sizes.
Definition
queue-size.h:85
ns3::QueueSizeValue
Definition
queue-size.h:210
ns3::QueueSizeValue::Get
QueueSize Get() const
Definition
queue-size.cc:18
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::StringValue
Hold variables of type string.
Definition
string.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition
assert.h:55
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition
config.cc:886
ns3::Config::Set
void Set(std::string path, const AttributeValue &value)
Definition
config.cc:872
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition
log.h:264
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
ns3::PACKETS
@ PACKETS
Use number of packets for queue size.
Definition
queue-size.h:34
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LogComponentEnable
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition
log.cc:291
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition
log.h:93
second.cmd
cmd
Definition
second.py:29
src
point-to-point
examples
main-attribute-value.cc
Generated on Wed Jun 11 2025 13:15:37 for ns-3 by
1.13.2