management: Add ExpirationPeriod to nfd::FaceStatus

Change-Id: Ie902991b86653609de3b8e6815c0e3313e7dc815
Refs: #1685
diff --git a/src/management/nfd-face-status.hpp b/src/management/nfd-face-status.hpp
index 1b86b3c..a92e154 100644
--- a/src/management/nfd-face-status.hpp
+++ b/src/management/nfd-face-status.hpp
@@ -28,6 +28,7 @@
 #include "../encoding/tlv-nfd.hpp"
 #include "../encoding/encoding-buffer.hpp"
 #include "../encoding/block-helpers.hpp"
+#include "../util/time.hpp"
 
 namespace ndn {
 namespace nfd {
@@ -117,6 +118,27 @@
     return *this;
   }
 
+  bool
+  hasExpirationPeriod() const
+  {
+    return m_hasExpirationPeriod;
+  }
+
+  const time::milliseconds&
+  getExpirationPeriod() const
+  {
+    BOOST_ASSERT(m_hasExpirationPeriod);
+    return m_expirationPeriod;
+  }
+
+  FaceStatus&
+  setExpirationPeriod(const time::milliseconds& expirationPeriod)
+  {
+    m_expirationPeriod = expirationPeriod;
+    m_hasExpirationPeriod = true;
+    return *this;
+  }
+
   uint64_t
   getFlags() const
   {
@@ -191,6 +213,8 @@
   uint64_t m_faceId;
   std::string m_remoteUri;
   std::string m_localUri;
+  time::milliseconds m_expirationPeriod;
+  bool m_hasExpirationPeriod;
   uint64_t m_flags;
   uint64_t m_nInInterests;
   uint64_t m_nInDatas;
@@ -203,6 +227,7 @@
 inline
 FaceStatus::FaceStatus()
   : m_faceId(0)
+  , m_hasExpirationPeriod(false)
   , m_flags(0)
   , m_nInInterests(0)
   , m_nInDatas(0)
@@ -227,6 +252,10 @@
                  tlv::nfd::NInInterests, m_nInInterests);
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::FaceFlags, m_flags);
+  if (m_hasExpirationPeriod) {
+    totalLength += prependNonNegativeIntegerBlock(encoder,
+                   tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
+  }
   totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
                  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
   totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
@@ -266,7 +295,7 @@
   Block::element_const_iterator val = m_wire.elements_begin();
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
-    m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_faceId = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -289,8 +318,18 @@
     throw Error("missing required LocalUri field");
   }
 
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
+    m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
+    m_hasExpirationPeriod = true;
+    ++val;
+  }
+  else {
+    m_hasExpirationPeriod = false;
+    // ExpirationPeriod is optional
+  }
+
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
-    m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_flags = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -298,7 +337,7 @@
   }
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
-    m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_nInInterests = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -306,7 +345,7 @@
   }
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
-    m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_nInDatas = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -314,7 +353,7 @@
   }
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
-    m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_nOutInterests = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -322,7 +361,7 @@
   }
 
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
-    m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
+    m_nOutDatas = readNonNegativeInteger(*val);
     ++val;
   }
   else {
@@ -336,8 +375,16 @@
   os << "FaceStatus("
      << "FaceID: " << status.getFaceId() << ", "
      << "RemoteUri: " << status.getRemoteUri() << ", "
-     << "LocalUri: " << status.getLocalUri() << ", "
-     << "Flags: " << status.getFlags() << ", "
+     << "LocalUri: " << status.getLocalUri() << ", ";
+
+  if (status.hasExpirationPeriod()) {
+    os << "ExpirationPeriod: " << status.getExpirationPeriod() << ", ";
+  }
+  else {
+    os << "ExpirationPeriod: infinite, ";
+  }
+
+  os << "Flags: " << status.getFlags() << ", "
      << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
      << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
      << ")";
diff --git a/tests/unit-tests/management/test-nfd-face-status.cpp b/tests/unit-tests/management/test-nfd-face-status.cpp
index bdca095..2691645 100644
--- a/tests/unit-tests/management/test-nfd-face-status.cpp
+++ b/tests/unit-tests/management/test-nfd-face-status.cpp
@@ -35,6 +35,7 @@
          .setRemoteUri("tcp4://192.0.2.1:6363")
          .setLocalUri("tcp4://192.0.2.2:55555")
          .setFlags(FACE_IS_ON_DEMAND)
+         .setExpirationPeriod(time::seconds(10))
          .setNInInterests(10)
          .setNInDatas(200)
          .setNOutInterests(3000)
@@ -45,16 +46,16 @@
 
   // These octets are obtained by the snippet below.
   // This check is intended to detect unexpected encoding change in the future.
-  //for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
   //  printf("0x%02x, ", *it);
-  //}
+  // }
   static const uint8_t expected[] = {
-    0x80, 0x42, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70, 0x34, 0x3a,
+    0x80, 0x46, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70, 0x34, 0x3a,
     0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x3a,
     0x36, 0x33, 0x36, 0x33, 0x81, 0x16, 0x74, 0x63, 0x70, 0x34, 0x3a, 0x2f,
     0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x32, 0x3a, 0x35,
-    0x35, 0x35, 0x35, 0x35, 0xc2, 0x01, 0x02, 0x90, 0x01, 0x0a, 0x91, 0x01,
-    0xc8, 0x92, 0x02, 0x0b, 0xb8, 0x93, 0x01, 0x04
+    0x35, 0x35, 0x35, 0x35, 0x6d, 0x02, 0x27, 0x10, 0xc2, 0x01, 0x02, 0x90,
+    0x01, 0x0a, 0x91, 0x01, 0xc8, 0x92, 0x02, 0x0b, 0xb8, 0x93, 0x01, 0x04
   };
   BOOST_REQUIRE_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                   wire.begin(), wire.end());
@@ -75,6 +76,7 @@
   BOOST_CHECK_EQUAL(os.str(), "FaceStatus(FaceID: 100, "
                               "RemoteUri: tcp4://192.0.2.1:6363, "
                               "LocalUri: tcp4://192.0.2.2:55555, "
+                              "ExpirationPeriod: 10000 milliseconds, "
                               "Flags: 2, "
                               "Counters: 10|200|3000|4)");
 }