Add sample file for attribute values
authorTom Henderson <tomh@tomh.org>
Wed, 19 Mar 2008 07:10:01 -0700
changeset 2623 bdd52bd78977
parent 2622 6440851b111a
child 2624 17172ab64e3f
Add sample file for attribute values
samples/main-attribute-value.cc
samples/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/main-attribute-value.cc	Wed Mar 19 07:10:01 2008 -0700
@@ -0,0 +1,138 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2008 University of Washington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Tom Henderson <tomh@tomh.org>
+ */
+
+#include "ns3/log.h"
+#include "ns3/command-line.h"
+#include "ns3/ptr.h"
+#include "ns3/config.h"
+#include "ns3/uinteger.h"
+#include "ns3/string.h"
+#include "ns3/simulator.h"
+
+#include "ns3/node.h"
+#include "ns3/queue.h"
+#include "ns3/drop-tail-queue.h"
+#include "ns3/point-to-point-net-device.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("AttributeValueSample");
+
+//
+// This is a basic example of how to use the attribute system to
+// set and get a value in the underlying system; namely, an unsigned
+// integer of the maximum number of packets in a queue
+//
+
+int 
+main (int argc, char *argv[])
+{
+  LogComponentEnable ("AttributeValueSample", LOG_LEVEL_INFO);
+
+  // By default, the MaxPackets attribute has a value of 100 packets
+  // (this default can be observed in the function DropTailQueue::GetTypeId)
+  // 
+  // Here, we set it to 80 packets.  We could use one of two value types:
+  // a string-based value or a Uinteger value
+  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", String ("80"));
+  // The below function call is redundant
+  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", Uinteger(80));
+
+  // Allow the user to override any of the defaults and the above
+  // SetDefaults() at run-time, via command-line arguments
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+
+  // Now, we will create a few objects using the low-level API
+  Ptr<Node> n0 = CreateObject<Node> ();
+
+  Ptr<PointToPointNetDevice> net0 = CreateObject<PointToPointNetDevice> ();
+  n0->AddDevice (net0);
+
+  Ptr<Queue> q = CreateObject<DropTailQueue> ();
+  net0->AddQueue(q);
+
+  // At this point, we have created a single node (Node 0) and a 
+  // single PointToPointNetDevice (NetDevice 0) and added a 
+  // DropTailQueue to it.
+
+  // Now, we can manipulate the MaxPackets value of the already 
+  // instantiated DropTailQueue.  Here are various ways to do that.
+
+  // We assume that a smart pointer (Ptr) to a relevant network device
+  // is in hand; here, it is the net0 pointer. 
+
+  // 1.  Pointer-based access
+  //
+  // One way to change the value is to access a pointer to the
+  // underlying queue and modify its attribute.
+  // 
+  // First, we observe that we can get a pointer to the (base class)
+  // queue via the PointToPointNetDevice attributes, where it is called
+  // TxQueue 
+  Ptr<Queue> txQueue = net0->GetAttribute ("TxQueue");
+
+  // Using the GetObject function, we can perform a safe downcast
+  // to a DropTailQueue, where MaxPackets is a member
+  Ptr<DropTailQueue> dtq = txQueue->GetObject <DropTailQueue> ();
+  NS_ASSERT (dtq);
+
+  // Next, we can get the value of an attribute on this queue
+  // We have introduced wrapper "Value" classes for the underlying
+  // data types, similar to Java wrappers around these types, since
+  // the attribute system stores values and not disparate types.
+  // Here, the attribute value is assigned to a Uinteger, and
+  // the Get() method on this value produces the (unwrapped) uint32_t.
+  Uinteger limit = dtq->GetAttribute ("MaxPackets");
+  NS_LOG_INFO ("1.  dtq limit: " << limit.Get () << " packets");
+  
+  // Note that the above downcast is not really needed; we could have
+  // done the same using the Ptr<Queue> even though the attribute
+  // is a member of the subclass
+  limit = txQueue->GetAttribute ("MaxPackets");
+  NS_LOG_INFO ("2.  txQueue limit: " << limit.Get () << " packets");
+
+  // Now, let's set it to another value (60 packets)
+  txQueue->SetAttribute("MaxPackets", Uinteger (60));
+  limit = txQueue->GetAttribute ("MaxPackets");
+  NS_LOG_INFO ("3.  txQueue limit changed: " << limit.Get () << " packets");
+
+  // 2.  Namespace-based access
+  //
+  // An alternative way to get at the attribute is to use the configuration
+  // namespace.  Here, this attribute resides on a known path in this
+  // namespace; this approach is useful if one doesn't have access to
+  // the underlying pointers and would like to configure a specific
+  // attribute with a single statement.  
+  Config::Set ("/NodeList/0/DeviceList/0/TxQueue/MaxPackets", Uinteger (25));
+  limit = txQueue->GetAttribute ("MaxPackets"); 
+  NS_LOG_INFO ("4.  txQueue limit changed through namespace: " << 
+    limit.Get () << " packets");
+
+  // we could have also used wildcards to set this value for all nodes
+  // and all net devices (which in this simple example has the same
+  // effect as the previous Set())
+  Config::Set ("/NodeList/*/DeviceList/*/TxQueue/MaxPackets", Uinteger (15));
+  limit = txQueue->GetAttribute ("MaxPackets"); 
+  NS_LOG_INFO ("5.  txQueue limit changed through wildcarded namespace: " << 
+    limit.Get () << " packets");
+
+  Simulator::Destroy ();
+}
--- a/samples/wscript	Sun Mar 16 14:35:32 2008 +0000
+++ b/samples/wscript	Wed Mar 19 07:10:01 2008 -0700
@@ -1,6 +1,9 @@
 ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 
 def build(bld):
+    obj = bld.create_ns3_program('main-attribute-value')
+    obj.source = 'main-attribute-value.cc'
+
     obj = bld.create_ns3_program('main-callback')
     obj.source = 'main-callback.cc'