topology-reader: Adding ability to specify custom queue type in topology files (AnnotatedTopologyReader)

Implements feature #1004 (http://redmine.named-data.net/)
diff --git a/examples/ndn-grid-topo-plugin-red-queues.cc b/examples/ndn-grid-topo-plugin-red-queues.cc
new file mode 100644
index 0000000..d73ad79
--- /dev/null
+++ b/examples/ndn-grid-topo-plugin-red-queues.cc
@@ -0,0 +1,100 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-2013 University of California, Los Angeles
+ *
+ * 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+// ndn-grid-topo-plugin.cc
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/ndnSIM-module.h"
+
+using namespace ns3;
+
+/**
+ * This scenario simulates a grid topology (using topology reader module)
+ *
+ * (consumer) -- ( ) ----- ( )
+ *     |          |         |
+ *    ( ) ------ ( ) ----- ( )
+ *     |          |         |
+ *    ( ) ------ ( ) -- (producer)
+ *
+ * All links are 1Mbps with propagation 10ms delay. 
+ *
+ * FIB is populated using ndn::GlobalRoutingHelper.
+ *
+ * Consumer requests data from producer with frequency 10 interests per second
+ * (interests contain constantly increasing sequence number).
+ *
+ * For every received interest, producer replies with a data packet, containing
+ * 1024 bytes of virtual payload.
+ *
+ * To run scenario and see what is happening, use the following command:
+ *
+ *     NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-grid-topo-plugin
+ */
+
+int
+main (int argc, char *argv[])
+{
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+
+  AnnotatedTopologyReader topologyReader ("", 25);
+  topologyReader.SetFileName ("src/ndnSIM/examples/topologies/topo-grid-3x3-red-queues.txt");
+  topologyReader.Read ();
+
+  // Install NDN stack on all nodes
+  ndn::StackHelper ndnHelper;
+  ndnHelper.SetForwardingStrategy ("ns3::ndn::fw::BestRoute");
+  ndnHelper.InstallAll ();
+
+  // Installing global routing interface on all nodes
+  ndn::GlobalRoutingHelper ndnGlobalRoutingHelper;
+  ndnGlobalRoutingHelper.InstallAll ();
+
+  // Getting containers for the consumer/producer
+  Ptr<Node> producer = Names::Find<Node> ("Node8");
+  NodeContainer consumerNodes;
+  consumerNodes.Add (Names::Find<Node> ("Node0"));
+
+  // Install NDN applications
+  std::string prefix = "/prefix";
+
+  ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
+  consumerHelper.SetPrefix (prefix);
+  consumerHelper.SetAttribute ("Frequency", StringValue ("100")); // 100 interests a second
+  consumerHelper.Install (consumerNodes);
+
+  ndn::AppHelper producerHelper ("ns3::ndn::Producer");
+  producerHelper.SetPrefix (prefix);
+  producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
+  producerHelper.Install (producer);
+
+  // Add /prefix origins to ndn::GlobalRouter
+  ndnGlobalRoutingHelper.AddOrigins (prefix, producer);
+
+  // Calculate and install FIBs
+  ndn::GlobalRoutingHelper::CalculateRoutes ();
+
+  Simulator::Stop (Seconds (20.0));
+
+  Simulator::Run ();
+  Simulator::Destroy ();
+
+  return 0;
+}
diff --git a/examples/topologies/topo-grid-3x3-red-queues.txt b/examples/topologies/topo-grid-3x3-red-queues.txt
new file mode 100644
index 0000000..b87a0bf
--- /dev/null
+++ b/examples/topologies/topo-grid-3x3-red-queues.txt
@@ -0,0 +1,61 @@
+# topo-grid-3x3.txt
+
+#   /--------\	    /-\	        /-\
+#   |Consumer|<---->| |<------->| |
+#   \--------/	    \-/	        \-/
+#       ^   	     ^ 	         ^
+#       |            |           |   1Mbps/10ms delay
+#       v            v           v
+#      /-\          /-\         /-\
+#      | |<-------->| |<------->| |
+#      \-/          \-/         \-/
+#       ^   	     ^ 	         ^
+#       |            |           |
+#       v            v           v
+#      /-\	    /-\	     /--------\
+#      | |<-------->| |<---->|Producer|
+#      \-/          \-/      \--------/
+
+# any empty lines and lines starting with '#' symbol is ignored
+#
+# The file should contain exactly two sections: router and link, each starting with the corresponding keyword
+#
+# router section defines topology nodes and their relative positions (e.g., to use in visualizer)
+router
+
+# each line in this section represents one router and should have the following data
+# node  comment     yPos    xPos
+Node0   NA          3       1
+Node1   NA          3       2
+Node2   NA          3       3
+Node3   NA          2       1
+Node4   NA          2       2
+Node5   NA          2       3
+Node6   NA          1       1
+Node7   NA          1       2
+Node8   NA          1       3
+# Note that `node` can be any string. It is possible to access to the node by name using Names::Find, see examples.
+
+# link section defines point-to-point links between nodes and characteristics of these links
+link
+
+# Each line should be in the following format (only first two are required, the rest can be omitted)
+# srcNode   dstNode     bandwidth   metric  delay   queue
+# bandwidth: link bandwidth
+# metric: routing metric
+# delay:  link delay
+# queue:  comma-separated list, specifying class for Queue (on both sides of the link) and necessary attributes
+# error:  comma-separated list, specifying class for ErrorModel and necessary attributes
+Node0       Node1       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node0       Node3       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node1       Node2       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node1       Node4       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node2       Node5       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node3       Node4       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node3       Node6       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node4       Node5       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node4       Node7       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node5       Node8       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node6       Node7       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+Node7       Node8       1Mbps       1       10ms    ns3::RedQueue,MeanPktSize=100
+
diff --git a/examples/wscript b/examples/wscript
index 4f8eddd..6f3bd1f 100644
--- a/examples/wscript
+++ b/examples/wscript
@@ -33,6 +33,9 @@
         obj = bld.create_ns3_program('ndn-grid-topo-plugin-loss', all_modules)
         obj.source = 'ndn-grid-topo-plugin-loss.cc'
 
+        obj = bld.create_ns3_program('ndn-grid-topo-plugin-red-queues', all_modules)
+        obj.source = 'ndn-grid-topo-plugin-red-queues.cc'
+
         obj = bld.create_ns3_program('ndn-congestion-topo-plugin', all_modules)
         obj.source = 'ndn-congestion-topo-plugin.cc'
 
diff --git a/plugins/topology/annotated-topology-reader.cc b/plugins/topology/annotated-topology-reader.cc
index c227507..33549a7 100644
--- a/plugins/topology/annotated-topology-reader.cc
+++ b/plugins/topology/annotated-topology-reader.cc
@@ -1,6 +1,6 @@
 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2011 University of California, Los Angeles
+ * Copyright (c) 2011-2013 University of California, Los Angeles
  *
  * 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
@@ -340,6 +340,50 @@
       // cout << "Link: " << Findlink.GetFromNode () << ", " << link.GetToNode () << endl;
       string tmp;
 
+      ////////////////////////////////////////////////
+      if (link.GetAttributeFailSafe ("MaxPackets", tmp))
+        {
+          NS_LOG_INFO ("MaxPackets = " + link.GetAttribute ("MaxPackets"));
+
+          try
+            {
+              uint32_t maxPackets = boost::lexical_cast<uint32_t> (link.GetAttribute ("MaxPackets"));
+
+              // compatibility mode. Only DropTailQueue is supported
+              p2p.SetQueue ("ns3::DropTailQueue",
+                            "MaxPackets", UintegerValue (maxPackets));
+            }
+          catch (...)
+            {
+              typedef boost::tokenizer<boost::escaped_list_separator<char> > tokenizer;
+              tokenizer tok (link.GetAttribute ("MaxPackets"));
+
+              tokenizer::iterator token = tok.begin ();
+              p2p.SetQueue (*token);
+
+              for (token ++; token != tok.end (); token ++)
+                {
+                  boost::escaped_list_separator<char> separator ('\\', '=', '\"');
+                  tokenizer attributeTok (*token, separator);
+
+                  tokenizer::iterator attributeToken = attributeTok.begin ();
+
+                  string attribute = *attributeToken;
+                  attributeToken++;
+
+                  if (attributeToken == attributeTok.end ())
+                    {
+                      NS_LOG_ERROR ("Queue attribute [" << *token << "] should be in form <Attribute>=<Value>");
+                      continue;
+                    }
+
+                  string value = *attributeToken;
+
+                  p2p.SetQueueAttribute (attribute, StringValue (value));
+                }
+            }
+        }
+      
       if (link.GetAttributeFailSafe ("DataRate", tmp))
         {
           NS_LOG_INFO ("DataRate = " + link.GetAttribute("DataRate"));
@@ -365,14 +409,14 @@
 
           tokenizer::iterator token = tok.begin ();
           ObjectFactory factory (*token);
-          
+
           for (token ++; token != tok.end (); token ++)
             {
               boost::escaped_list_separator<char> separator ('\\', '=', '\"');
               tokenizer attributeTok (*token, separator);
 
               tokenizer::iterator attributeToken = attributeTok.begin ();
-              
+
               string attribute = *attributeToken;
               attributeToken++;
 
@@ -381,7 +425,7 @@
                   NS_LOG_ERROR ("ErrorModel attribute [" << *token << "] should be in form <Attribute>=<Value>");
                   continue;
                 }
-              
+
               string value = *attributeToken;
 
               factory.Set (attribute, StringValue (value));
@@ -390,23 +434,6 @@
           nd.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (factory.Create<ErrorModel> ()));
           nd.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (factory.Create<ErrorModel> ()));
         }
-
-      ////////////////////////////////////////////////
-      if (link.GetAttributeFailSafe ("MaxPackets", tmp))
-        {
-          NS_LOG_INFO ("MaxPackets = " + link.GetAttribute ("MaxPackets"));
-
-          PointerValue txQueue;
-
-          link.GetToNetDevice ()->GetAttribute ("TxQueue", txQueue);
-          NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
-          txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
-
-          link.GetFromNetDevice ()->GetAttribute ("TxQueue", txQueue);
-          NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
-          txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
-        }
-
     }
 }
 
@@ -544,7 +571,7 @@
       // add_edge (node->second, otherNode->second, m_graph);
       boost::add_edge (from->second, to->second, graph);
     }
-  
+
   ofstream of (file.c_str ());
   boost::property_map<Graph, boost::vertex_name_t>::type names = get (boost::vertex_name, graph);
   write_graphviz (of, graph, make_name_writer (names));