apps: Switch ndn::App::GetId() to use application ID on the node

Previously, GetId() used ID of the underlying application face, which
is less convenient for simulation trace analyses.

Change-Id: I403182ecb4b9e8fb010c4c25ab007c1e274bc86a
diff --git a/apps/ndn-app.cpp b/apps/ndn-app.cpp
index 802f697..a1c9e80 100644
--- a/apps/ndn-app.cpp
+++ b/apps/ndn-app.cpp
@@ -61,6 +61,7 @@
 App::App()
   : m_active(false)
   , m_face(0)
+  , m_appId(std::numeric_limits<uint32_t>::max())
 {
 }
 
@@ -69,6 +70,21 @@
 }
 
 void
+App::DoInitialize()
+{
+  NS_LOG_FUNCTION_NOARGS();
+
+  // find out what is application id on the node
+  for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
+    if (GetNode()->GetApplication(id) == this) {
+      m_appId = id;
+    }
+  }
+
+  Application::DoInitialize();
+}
+
+void
 App::DoDispose(void)
 {
   NS_LOG_FUNCTION_NOARGS();
@@ -82,10 +98,7 @@
 uint32_t
 App::GetId() const
 {
-  if (m_face == 0)
-    return (uint32_t)-1;
-  else
-    return m_face->getId();
+  return m_appId;
 }
 
 void
diff --git a/apps/ndn-app.hpp b/apps/ndn-app.hpp
index 57dda58..2d84134 100644
--- a/apps/ndn-app.hpp
+++ b/apps/ndn-app.hpp
@@ -85,9 +85,9 @@
   typedef void (*DataTraceCallback)(shared_ptr<const Data>, Ptr<App>, shared_ptr<Face>);
 
 protected:
-  /**
-   * @brief Do cleanup when application is destroyed
-   */
+  virtual void
+  DoInitialize();
+
   virtual void
   DoDispose();
 
@@ -101,6 +101,7 @@
 protected:
   bool m_active; ///< @brief Flag to indicate that application is active (set by StartApplication and StopApplication)
   shared_ptr<AppFace> m_face; ///< @brief automatically created application face through which application communicates
+  uint32_t m_appId;
 
   TracedCallback<shared_ptr<const Interest>, Ptr<App>, shared_ptr<Face>>
     m_receivedInterests; ///< @brief App-level trace of received Interests
diff --git a/tests/unit-tests/utils/tracers/ndn-app-delay-tracer.t.cpp b/tests/unit-tests/utils/tracers/ndn-app-delay-tracer.t.cpp
index 64e4aa9..1ed6890 100644
--- a/tests/unit-tests/utils/tracers/ndn-app-delay-tracer.t.cpp
+++ b/tests/unit-tests/utils/tracers/ndn-app-delay-tracer.t.cpp
@@ -89,12 +89,12 @@
 
   BOOST_CHECK_EQUAL(buffer.str(),
     "Time	Node	AppId	SeqNo	Type	DelayS	DelayUS	RetxCount	HopCount\n"
-    "0.0417424	1	257	0	LastDelay	0.0417424	41742.4	1	2\n"
-    "0.0417424	1	257	0	FullDelay	0.0417424	41742.4	1	2\n"
-    "2	2	258	0	LastDelay	0	0	1	0\n"
-    "2	2	258	0	FullDelay	0	0	1	0\n"
-    "3.02087	2	258	1	LastDelay	0.0208712	20871.2	1	1\n"
-    "3.02087	2	258	1	FullDelay	0.0208712	20871.2	1	1\n");
+    "0.0417424	1	0	0	LastDelay	0.0417424	41742.4	1	2\n"
+    "0.0417424	1	0	0	FullDelay	0.0417424	41742.4	1	2\n"
+    "2	2	0	0	LastDelay	0	0	1	0\n"
+    "2	2	0	0	FullDelay	0	0	1	0\n"
+    "3.02087	2	0	1	LastDelay	0.0208712	20871.2	1	1\n"
+    "3.02087	2	0	1	FullDelay	0.0208712	20871.2	1	1\n");
 }
 
 BOOST_AUTO_TEST_CASE(InstallNodeContainer)
@@ -115,8 +115,8 @@
 
   BOOST_CHECK_EQUAL(buffer.str(),
     "Time	Node	AppId	SeqNo	Type	DelayS	DelayUS	RetxCount	HopCount\n"
-    "0.0417424	1	257	0	LastDelay	0.0417424	41742.4	1	2\n"
-    "0.0417424	1	257	0	FullDelay	0.0417424	41742.4	1	2\n");
+    "0.0417424	1	0	0	LastDelay	0.0417424	41742.4	1	2\n"
+    "0.0417424	1	0	0	FullDelay	0.0417424	41742.4	1	2\n");
 }
 
 BOOST_AUTO_TEST_CASE(InstallNode)
@@ -134,10 +134,10 @@
 
   BOOST_CHECK_EQUAL(buffer.str(),
     "Time	Node	AppId	SeqNo	Type	DelayS	DelayUS	RetxCount	HopCount\n"
-    "2	2	258	0	LastDelay	0	0	1	0\n"
-    "2	2	258	0	FullDelay	0	0	1	0\n"
-    "3.02087	2	258	1	LastDelay	0.0208712	20871.2	1	1\n"
-    "3.02087	2	258	1	FullDelay	0.0208712	20871.2	1	1\n");
+    "2	2	0	0	LastDelay	0	0	1	0\n"
+    "2	2	0	0	FullDelay	0	0	1	0\n"
+    "3.02087	2	0	1	LastDelay	0.0208712	20871.2	1	1\n"
+    "3.02087	2	0	1	FullDelay	0.0208712	20871.2	1	1\n");
 }
 
 BOOST_AUTO_TEST_CASE(InstallNodeDumpStream)
@@ -151,10 +151,10 @@
   tracer = nullptr; // destroy tracer
 
   BOOST_CHECK(output->is_equal(
-    "2	2	258	0	LastDelay	0	0	1	0\n"
-    "2	2	258	0	FullDelay	0	0	1	0\n"
-    "3.02087	2	258	1	LastDelay	0.0208712	20871.2	1	1\n"
-    "3.02087	2	258	1	FullDelay	0.0208712	20871.2	1	1\n"));
+    "2	2	0	0	LastDelay	0	0	1	0\n"
+    "2	2	0	0	FullDelay	0	0	1	0\n"
+    "3.02087	2	0	1	LastDelay	0.0208712	20871.2	1	1\n"
+    "3.02087	2	0	1	FullDelay	0.0208712	20871.2	1	1\n"));
 }
 
 BOOST_AUTO_TEST_SUITE_END()