NetAnim: Implement Node Counter
authorJohn Abraham <john.abraham.in@gmail.com>
Mon, 13 Jan 2014 05:45:54 -0800
changeset 10568 c2276139e20a
parent 10567 3734efb57619
child 10569 86f8d2f7c2c0
NetAnim: Implement Node Counter
src/netanim/examples/resources_demo.cc
src/netanim/model/animation-interface.cc
src/netanim/model/animation-interface.h
--- a/src/netanim/examples/resources_demo.cc	Mon Jan 13 20:45:55 2014 +0900
+++ b/src/netanim/examples/resources_demo.cc	Mon Jan 13 05:45:54 2014 -0800
@@ -44,6 +44,8 @@
 
 uint32_t resourceId1;
 uint32_t resourceId2;
+uint32_t nodeCounterId;
+
 void modify ()
 {
   std::ostringstream oss;
@@ -92,6 +94,9 @@
   for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
     pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b); 
 
+  // Update Node Counter for node 0, use some random number between 0 to 1000 for value
+  Ptr <UniformRandomVariable> rv = CreateObject<UniformRandomVariable> ();
+  pAnim->UpdateNodeCounter (nodeCounterId, 0, rv->GetValue (0, 1000));
 
   if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
     // will run endlessly
@@ -172,6 +177,11 @@
   resourceId1 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo1.png");
   resourceId2 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo2.png");
   pAnim->SetBackgroundImage ("/Users/john/ns3/netanim-3.105/ns-3-background.png", 0, 0, 0.2, 0.2, 0.1);
+
+
+  // Add a node counter
+  nodeCounterId = pAnim->AddNodeCounter ("Sample Counter", AnimationInterface::UINT32_COUNTER);
+
   Simulator::Schedule (Seconds (0.1), modify);
   
   // Set up the acutal simulation
--- a/src/netanim/model/animation-interface.cc	Mon Jan 13 20:45:55 2014 +0900
+++ b/src/netanim/model/animation-interface.cc	Mon Jan 13 05:45:54 2014 -0800
@@ -324,6 +324,15 @@
   m_stopTime = t;
 }
 
+uint32_t AnimationInterface::AddNodeCounter (std::string counterName, CounterType counterType)
+{
+  m_nodeCounters.push_back (counterName);
+  uint32_t counterId = m_nodeCounters.size () -1;
+  std::ostringstream oss;
+  oss << GetXMLOpenCloseAddNodeCounter (counterId, counterName, counterType);
+  WriteN (oss.str (), m_f);
+  return counterId; 
+}
 
 uint32_t AnimationInterface::AddResource (std::string resourcePath)
 {
@@ -1676,6 +1685,24 @@
 
 // Helper to output a wireless packet.
 // For now, only the XML interface is supported
+std::string AnimationInterface::CounterTypeToString (CounterType counterType)
+{
+  std::string typeString = "unknown";
+  switch (counterType)
+    {
+      case UINT32_COUNTER:
+        {
+          typeString = "UINT32";
+          break;
+        } 
+      case DOUBLE_COUNTER:
+        {
+          typeString = "DOUBLE";
+          break;
+        } 
+    }
+  return typeString;
+}
 
 
 std::string AnimationInterface::GetPreamble ()
@@ -1798,6 +1825,17 @@
   userBoundary->yMin = minY;
 }
 
+void AnimationInterface::UpdateNodeCounter (uint32_t nodeCounterId, uint32_t nodeId, double counter)
+{
+  if (nodeCounterId > (m_nodeCounters.size ()-1))
+    {
+      NS_FATAL_ERROR ("NodeCounter Id:" << nodeCounterId << " not found. Did you use AddNodeCounter?");
+    }
+  std::ostringstream oss;
+  oss << GetXMLOpenCloseUpdateNodeCounter (nodeCounterId, nodeId, counter);
+  WriteN (oss.str (), m_f);
+}
+
 void AnimationInterface::SetBackgroundImage (std::string fileName, double x, double y, double scaleX, double scaleY, double opacity)
 {
   if ((opacity < 0) || (opacity > 1))
@@ -2191,6 +2229,16 @@
   return oss.str ();      
 }
 
+std::string AnimationInterface::GetXMLOpenCloseAddNodeCounter (uint32_t nodeCounterId, std::string counterName, CounterType counterType)
+{
+  std::ostringstream oss;
+  oss << "<ncs ncId=\"" << nodeCounterId << "\""
+      << " n=\"" << counterName << "\""
+      << " t=\"" << CounterTypeToString (counterType) << "\""
+      << " />" << std::endl;
+  return oss.str ();
+}
+
 std::string AnimationInterface::GetXMLOpenCloseAddResource (uint32_t resourceId, std::string resourcePath)
 {
   std::ostringstream oss;
@@ -2268,6 +2316,18 @@
   return oss.str ();
 }
 
+
+std::string AnimationInterface::GetXMLOpenCloseUpdateNodeCounter (uint32_t nodeCounterId, uint32_t nodeId, double counterValue)
+{
+  std::ostringstream oss;
+  oss << "<nc c=\"" << nodeCounterId << "\""
+      << " i=\"" << nodeId << "\""
+      << " t=\"" << Simulator::Now ().GetSeconds () << "\""
+      << " v=\"" << counterValue << "\""
+      << " />" << std::endl;
+  return oss.str ();
+}
+
 std::string AnimationInterface::GetXMLOpenCloseUpdateBackground (std::string fileName, double x, double y, double scaleX, double scaleY, double opacity)
 {
   std::ostringstream oss;
--- a/src/netanim/model/animation-interface.h	Mon Jan 13 20:45:55 2014 +0900
+++ b/src/netanim/model/animation-interface.h	Mon Jan 13 05:45:54 2014 -0800
@@ -59,6 +59,7 @@
   std::string linkDescription;
 } LinkProperties;
 
+
 struct LinkPairCompare
 {
   bool operator () (P2pLinkNodeIdPair first, P2pLinkNodeIdPair second) const
@@ -123,6 +124,15 @@
 	uint64_t maxPktsPerFile = MAX_PKTS_PER_TRACE_FILE);
 
   /**
+   * Counter Types 
+   */
+  typedef enum
+    {
+      UINT32_COUNTER,
+      DOUBLE_COUNTER
+    } CounterType;
+
+  /**
    * \brief Destructor for the animator interface.
    *
    */
@@ -345,6 +355,16 @@
    */
   static void SetNodeColor (NodeContainer nc, uint8_t r, uint8_t g, uint8_t b);
 
+
+  /**
+   * \brief Helper function to update a node's counter referenced by the nodeCounterId
+   * \param nodeCounterId The counter Id obtained from AddNodeCounter
+   * \param nodeId Node Id of the node
+   * \param counter Current value of the counter
+   *
+   */
+  void UpdateNodeCounter (uint32_t nodeCounterId, uint32_t nodeId, double counter);
+
   /**
    * \brief Helper function to set the background image
    * \param fileName File name of the background image
@@ -441,8 +461,19 @@
 
   /**
    *
+   * \brief Setup a node counter
+   * \param counterName A string to identify the counter
+   * \param counterType The type of the counter, such as uint32, double etc
+   * 
+   * returns The id of the counter to be used as a reference for future
+   */
+  uint32_t AddNodeCounter (std::string counterName, CounterType counterType); 
+
+  /**
+   *
    * \brief Add a resource such as the path to an image file
-   *
+   * \param resourcePath Absolute Path to an image/resource
+   * 
    * returns a number identifying the resource
    *
    */
@@ -668,6 +699,7 @@
 
   std::map <uint32_t, NodeSize> m_nodeSizes;
   std::vector <std::string> m_resources;
+  std::vector <std::string> m_nodeCounters;
   void StartNewTraceFile ();
 
   std::string GetMacAddress (Ptr <NetDevice> nd);
@@ -680,6 +712,7 @@
 
   // XML helpers
   std::string GetPreamble (void);
+  std::string CounterTypeToString (CounterType counterType);
   // Topology element dimensions
   double m_topoMinX;
   double m_topoMinY;
@@ -696,8 +729,9 @@
   std::string GetXMLOpenCloseUpdateNodeDescription (uint32_t nodeId);
   std::string GetXMLOpenCloseUpdateNodeSize (uint32_t nodeId, double width, double height);
   std::string GetXMLOpenCloseAddResource (uint32_t resourceId, std::string resourcePath);
+  std::string GetXMLOpenCloseAddNodeCounter (uint32_t counterId, std::string counterName, CounterType counterType);
   std::string GetXMLOpenCloseUpdateNodeImage (uint32_t nodeId, uint32_t resourceId);
-
+  std::string GetXMLOpenCloseUpdateNodeCounter (uint32_t counterId, uint32_t nodeId, double value);
   std::string GetXMLOpen_topology (double minX, double minY, double maxX, double maxY);
   std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY);
   std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY, struct Rgb rgb);