Replace ndnsType with NdnsContentType

Change-Id: I74afe92d31e78d80404816113063d7f996d666c5
diff --git a/src/clients/iterative-query-controller.cpp b/src/clients/iterative-query-controller.cpp
index 6bd99bd..899a35c 100644
--- a/src/clients/iterative-query-controller.cpp
+++ b/src/clients/iterative-query-controller.cpp
@@ -63,20 +63,16 @@
 void
 IterativeQueryController::onData(const ndn::Interest& interest, const Data& data)
 {
-  NdnsType ndnsType = NDNS_RAW;
-  const Block* block = data.getMetaInfo().findAppMetaInfo(ndns::tlv::NdnsType);
-  if (block != nullptr) {
-    ndnsType = static_cast<NdnsType>(readNonNegativeInteger(*block));
-  }
+  NdnsContentType contentType = NdnsContentType(data.getContentType());
 
-  NDNS_LOG_TRACE("[* -> *] get a " << ndnsType
+  NDNS_LOG_TRACE("[* -> *] get a " << contentType
                  << " Response: " << data.getName());
   if (m_validator == nullptr) {
-    this->onDataValidated(make_shared<Data>(data), ndnsType);
+    this->onDataValidated(make_shared<Data>(data), contentType);
   }
   else {
     m_validator->validate(data,
-                          bind(&IterativeQueryController::onDataValidated, this, _1, ndnsType),
+                          bind(&IterativeQueryController::onDataValidated, this, _1, contentType),
                           [this] (const shared_ptr<const Data>& data, const std::string& str) {
                             NDNS_LOG_WARN("data: " << data->getName() << " fails verification");
                             this->abort();
@@ -85,22 +81,22 @@
   }
 }
 void
-IterativeQueryController::onDataValidated(const shared_ptr<const Data>& data, NdnsType ndnsType)
+IterativeQueryController::onDataValidated(const shared_ptr<const Data>& data, NdnsContentType contentType)
 {
   switch (m_step) {
   case QUERY_STEP_QUERY_NS:
-    if (ndnsType == NDNS_NACK) {
+    if (contentType == NDNS_NACK) {
       m_step = QUERY_STEP_QUERY_RR;
     }
-    else if (ndnsType == NDNS_RESP) {
-      if (m_rrType == label::NS_RR_TYPE) {
-        Link link(data->wireEncode());
-        if (link.getDelegations().empty()) {
-          m_lastLink = Block();
-        } else {
-          m_lastLink = data->wireEncode();
-        }
+    else if (contentType == NDNS_LINK) {
+      Link link(data->wireEncode());
+      if (link.getDelegations().empty()) {
+        m_lastLink = Block();
+      } else {
+        m_lastLink = data->wireEncode();
       }
+
+      // for NS query, if already received, just return, instead of more queries until NACK
       if (m_nFinishedComps + m_nTryComps == m_dstLabel.size() && m_rrType == label::NS_RR_TYPE) {
         // NS_RR_TYPE is different, since its record is stored at higher level
         m_step = QUERY_STEP_ANSWER_STUB;
@@ -110,13 +106,17 @@
         m_nTryComps = 1;
       }
     }
-    else if (ndnsType == NDNS_AUTH) {
+    else if (contentType == NDNS_AUTH) {
       m_nTryComps += 1;
     }
-    else if (ndnsType == NDNS_RAW) {
+    else if (contentType == NDNS_BLOB) {
       std::ostringstream oss;
       oss << *this;
-      NDNS_LOG_WARN("get unexpected Response: NDNS_RAW for QUERY_NS: " << oss.str());
+      NDNS_LOG_WARN("get unexpected Response: NDNS_BLOB for QUERY_NS: " << oss.str());
+    } else {
+      std::ostringstream oss;
+      oss << *this;
+      NDNS_LOG_WARN("get unexpected Response for QUERY_NS: " << oss.str());
     }
     //
     if (m_nFinishedComps + m_nTryComps > m_dstLabel.size()) {
diff --git a/src/clients/iterative-query-controller.hpp b/src/clients/iterative-query-controller.hpp
index 8fc2a34..7f975da 100644
--- a/src/clients/iterative-query-controller.hpp
+++ b/src/clients/iterative-query-controller.hpp
@@ -78,7 +78,7 @@
   onData(const ndn::Interest& interest, const Data& data);
 
   void
-  onDataValidated(const shared_ptr<const Data>& data, NdnsType ndnsType);
+  onDataValidated(const shared_ptr<const Data>& data, NdnsContentType contentType);
 
   /**
    * @brief change the Controller state according to timeout. For current,
diff --git a/src/clients/response.cpp b/src/clients/response.cpp
index fc14346..0bbc9de 100644
--- a/src/clients/response.cpp
+++ b/src/clients/response.cpp
@@ -24,7 +24,7 @@
 namespace ndns {
 
 Response::Response()
-  : m_ndnsType(NDNS_RAW)
+  : m_contentType(NDNS_BLOB)
   , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
   , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0))
 {
@@ -33,7 +33,7 @@
 Response::Response(const Name& zone, const name::Component& queryType)
   : m_zone(zone)
   , m_queryType(queryType)
-  , m_ndnsType(NDNS_RAW)
+  , m_contentType(NDNS_BLOB)
   , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
   , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0))
 {
@@ -43,7 +43,7 @@
 inline size_t
 Response::wireEncode(EncodingImpl<T>& block) const
 {
-  if (m_ndnsType == NDNS_RAW) {
+  if (m_contentType == NDNS_BLOB) {
     // Raw application content
     return block.prependBlock(m_appContent);
   }
@@ -66,7 +66,7 @@
 const Block
 Response::wireEncode() const
 {
-  if (m_ndnsType == NDNS_RAW) {
+  if (m_contentType == NDNS_BLOB) {
     return m_appContent;
   }
 
@@ -80,7 +80,7 @@
 void
 Response::wireDecode(const Block& wire)
 {
-  if (m_ndnsType == NDNS_RAW) {
+  if (m_contentType == NDNS_BLOB) {
     m_appContent = wire;
     return;
   }
@@ -111,9 +111,7 @@
   MetaInfo info = data.getMetaInfo();
 
   m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod());
-  const Block* block = info.findAppMetaInfo(tlv::NdnsType);
-  if (block != 0)
-    m_ndnsType = static_cast<NdnsType>(readNonNegativeInteger(*block));
+  m_contentType = NdnsContentType(data.getContentType());
 
   wireDecode(data.getContent());
   return true;
@@ -139,17 +137,14 @@
 
   shared_ptr<Data> data = make_shared<Data>(name);
 
-  MetaInfo info;
-  info.setFreshnessPeriod(m_freshnessPeriod);
-
-  if (m_ndnsType != NDNS_RAW) {
-    info.addAppMetaInfo(makeNonNegativeIntegerBlock(ndns::tlv::NdnsType, m_ndnsType));
+  if (m_contentType != NDNS_BLOB) {
     data->setContent(this->wireEncode());
   }
   else {
     data->setContent(m_appContent);
   }
-  data->setMetaInfo(info);
+  data->setFreshnessPeriod(m_freshnessPeriod);
+  data->setContentType(m_contentType);
 
   return data;
 }
@@ -198,12 +193,12 @@
   bool tmp = (getZone() == other.getZone() &&
               getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() &&
               getRrType() == other.getRrType() && getVersion() == other.getVersion() &&
-              getNdnsType() == other.getNdnsType());
+              getContentType() == other.getContentType());
 
   if (tmp == false)
     return tmp;
 
-  if (m_ndnsType == NDNS_RAW) {
+  if (m_contentType == NDNS_BLOB) {
     return tmp && (getAppContent() == other.getAppContent());
   }
   else
@@ -219,8 +214,8 @@
      << " rrType=" << response.getRrType()
      << " version=" << response.getVersion()
      << " freshnessPeriod=" << response.getFreshnessPeriod()
-     << " ndnsType=" << response.getNdnsType();
-  if (response.getNdnsType() == NDNS_RAW) {
+     << " NdnsContentType=" << response.getContentType();
+  if (response.getContentType() == NDNS_BLOB) {
     if (response.getAppContent().empty())
       os << " appContent=NULL";
     else
diff --git a/src/clients/response.hpp b/src/clients/response.hpp
index c6a2779..9f2138f 100644
--- a/src/clients/response.hpp
+++ b/src/clients/response.hpp
@@ -177,15 +177,15 @@
   }
 
   void
-  setNdnsType(NdnsType ndnsType)
+  setContentType(NdnsContentType contentType)
   {
-    m_ndnsType = ndnsType;
+    m_contentType = contentType;
   }
 
-  NdnsType
-  getNdnsType() const
+  NdnsContentType
+  getContentType() const
   {
-    return m_ndnsType;
+    return m_contentType;
   }
 
   const Block&
@@ -228,7 +228,7 @@
   name::Component m_rrType;
   name::Component m_version;
 
-  NdnsType m_ndnsType;
+  NdnsContentType m_contentType;
   time::seconds m_freshnessPeriod;
 
   /**
diff --git a/src/daemon/name-server.cpp b/src/daemon/name-server.cpp
index ab3356c..70288ec 100644
--- a/src/daemon/name-server.cpp
+++ b/src/daemon/name-server.cpp
@@ -103,14 +103,11 @@
   }
   else {
     // no record, construct NACK
-    Block block = makeNonNegativeIntegerBlock(tlv::NdnsType, NDNS_NACK);
-    MetaInfo info;
-    info.addAppMetaInfo(block);
-    info.setFreshnessPeriod(this->getContentFreshness());
     Name name = interest.getName();
     name.appendVersion();
     shared_ptr<Data> answer = make_shared<Data>(name);
-    answer->setMetaInfo(info);
+    answer->setFreshnessPeriod(this->getContentFreshness());
+    answer->setContentType(NDNS_NACK);
 
     m_keyChain.sign(*answer, m_certName);
     NDNS_LOG_TRACE("answer query with NDNS-NACK: " << answer->getName());
@@ -168,14 +165,11 @@
   rrset.setLabel(re.rrLabel);
   rrset.setType(re.rrType);
 
-  Block ndnsType = makeNonNegativeIntegerBlock(::ndn::ndns::tlv::NdnsType, NDNS_RESP);
-  MetaInfo info;
-  info.addAppMetaInfo(ndnsType);
-  info.setFreshnessPeriod(this->getContentFreshness());
   Name name = interest->getName();
   name.appendVersion();
   shared_ptr<Data> answer = make_shared<Data>(name);
-  answer->setMetaInfo(info);
+  answer->setFreshnessPeriod(this->getContentFreshness());
+  answer->setContentType(NDNS_RESP);
 
   Block blk(ndn::ndns::tlv::RrData);
   try {
diff --git a/src/daemon/rrset-factory.cpp b/src/daemon/rrset-factory.cpp
index b1bbf90..a75c650 100644
--- a/src/daemon/rrset-factory.cpp
+++ b/src/daemon/rrset-factory.cpp
@@ -153,7 +153,7 @@
     link.addDelegation(i.first, i.second);
   }
 
-  setMetaInfo(link);
+  setContentType(link, NDNS_LINK, ttl);
   sign(link);
   rrset.setData(link.wireEncode());
 
@@ -188,7 +188,7 @@
   Data data(name);
   data.setContent(wireEncode(rrs));
 
-  setMetaInfo(data);
+  setContentType(data, NDNS_RESP, ttl);
   sign(data);
   rrset.setData(data.wireEncode());
 
@@ -216,7 +216,7 @@
   Data data(name);
   data.setContent(cert.wireEncode());
 
-  setMetaInfo(data);
+  setContentType(data, NDNS_BLOB, ttl);
   sign(data);
   rrset.setData(data.wireEncode());
 
@@ -231,12 +231,11 @@
 }
 
 void
-RrsetFactory::setMetaInfo(Data& data)
+RrsetFactory::setContentType(Data& data, NdnsContentType contentType,
+                             const time::seconds& ttl)
 {
-  MetaInfo metaInfo = data.getMetaInfo();
-  metaInfo.addAppMetaInfo(makeNonNegativeIntegerBlock(ndns::tlv::NdnsType,
-                                                      NDNS_RESP));
-  data.setMetaInfo(metaInfo);
+  data.setContentType(contentType);
+  data.setFreshnessPeriod(ttl);
 }
 
 template<encoding::Tag TAG>
@@ -273,7 +272,7 @@
   std::vector<std::string> txts;
   wire.parse();
 
-  for (const auto& e: wire.elements()) {
+  for (const auto& e : wire.elements()) {
     txts.push_back(std::string(reinterpret_cast<const char*>(e.value()),
                                e.value_size()));
   }
diff --git a/src/daemon/rrset-factory.hpp b/src/daemon/rrset-factory.hpp
index 732f0a7..8318bb8 100644
--- a/src/daemon/rrset-factory.hpp
+++ b/src/daemon/rrset-factory.hpp
@@ -24,6 +24,7 @@
 #include "rrset.hpp"
 #include "logger.hpp"
 #include "daemon/db-mgr.hpp"
+#include "ndns-enum.hpp"
 
 #include <ndn-cxx/link.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
@@ -106,8 +107,8 @@
   void
   sign(Data& data);
 
-  void
-  setMetaInfo(Data& data);
+  void setContentType(Data& data, NdnsContentType contentType,
+                      const time::seconds& ttl);
 
 private:
   KeyChain& m_keyChain;
diff --git a/src/mgmt/management-tool.cpp b/src/mgmt/management-tool.cpp
index dc3f971..0ddf57b 100644
--- a/src/mgmt/management-tool.cpp
+++ b/src/mgmt/management-tool.cpp
@@ -345,11 +345,11 @@
     Data data(rrset.getData());
     Response re;
     re.fromData(zoneName, data);
-    int iteration = re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH ?
+    int iteration = re.getContentType() == NDNS_BLOB || re.getContentType() == NDNS_AUTH ?
                       1 : re.getRrs().size();
     const std::vector<Block> &rrs = re.getRrs();
 
-    if (re.getNdnsType() != NDNS_RAW) {
+    if (re.getContentType() != NDNS_BLOB) {
       os << "; rrset=" << rrset.getLabel().toUri()
          << " type=" << rrset.getType().toUri()
          << " version=" << rrset.getVersion().toUri()
@@ -368,7 +368,7 @@
       os.width(typeWidth + 2);
       os << rrset.getType().toUri();
 
-      if (re.getNdnsType() != NDNS_RAW) {
+      if (re.getContentType() != NDNS_BLOB) {
         using namespace CryptoPP;
         if (rrset.getType() == label::TXT_RR_TYPE) {
           os.write(reinterpret_cast<const char*>(rrs[i].value()), rrs[i].value_size());
@@ -376,7 +376,7 @@
         }
         else if (rrset.getType() == label::NS_RR_TYPE) {
           BOOST_ASSERT(iteration == 1);
-          if (re.getNdnsType() == NDNS_AUTH) {
+          if (re.getContentType() == NDNS_AUTH) {
             const std::string authStr = "NDNS-Auth";
             os << authStr;
           } else {
@@ -397,14 +397,14 @@
       }
     }
 
-    if (re.getNdnsType() == NDNS_RAW) {
+    if (re.getContentType() == NDNS_BLOB) {
       os.width();
-      os << "; content-type=" << re.getNdnsType()
+      os << "; content-type=" << re.getContentType()
          << " version=" << rrset.getVersion().toUri()
          << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri();
       os << std::endl;
 
-      if (printRaw && re.getNdnsType() == NDNS_RAW) {
+      if (printRaw && re.getContentType() == NDNS_BLOB) {
         util::IndentedStream istream(os, "; ");
 
         if (re.getRrType() == label::CERT_RR_TYPE) {
diff --git a/src/ndns-enum.cpp b/src/ndns-enum.cpp
index 45504fb..383eb95 100644
--- a/src/ndns-enum.cpp
+++ b/src/ndns-enum.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -23,20 +23,26 @@
 namespace ndns {
 
 std::ostream&
-operator<<(std::ostream& os, const NdnsType ndnsType)
+operator<<(std::ostream& os, const NdnsContentType ndnsType)
 {
   switch (ndnsType) {
-  case NDNS_RESP:
-    os << "NDNS-Resp";
+  case NDNS_BLOB:
+    os << "BLOB";
+    break;
+  case NDNS_LINK:
+    os << "LINK";
     break;
   case NDNS_NACK:
-    os << "NDNS-Nack";
+    os << "NACK";
+    break;
+  case NDNS_KEY:
+    os << "KEY";
     break;
   case NDNS_AUTH:
     os << "NDNS-Auth";
     break;
-  case NDNS_RAW:
-    os << "NDNS-Raw";
+  case NDNS_RESP:
+    os << "NDNS-Resp";
     break;
   default:
     os << "UNKNOWN";
diff --git a/src/ndns-enum.hpp b/src/ndns-enum.hpp
index 39e77b0..89ad87d 100644
--- a/src/ndns-enum.hpp
+++ b/src/ndns-enum.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -21,24 +21,26 @@
 #define NDNS_NDNS_ENUM_HPP
 
 #include <ostream>
+#include <ndn-cxx/encoding/tlv.hpp>
 
 namespace ndn {
 namespace ndns {
 
 /**
- * @brief NdnsType defined in Response.NdnsMetaInfo.NdnsType
+ * @brief contentType of response
  */
-enum NdnsType {
-  NDNS_RAW = 0, ///< this is not a real type, just mean that MetaInfo does not contain NdnsType
-  NDNS_RESP = 1, ///< response type means there are requested RR
-  NDNS_NACK = 2, ///< no requested RR
-  NDNS_AUTH = 3, ///< only has RR for detailed (longer) label
-
-  NDNS_UNKNOWN = 255
+enum NdnsContentType {
+  NDNS_BLOB = ndn::tlv::ContentType_Blob,
+  NDNS_LINK = ndn::tlv::ContentType_Link,
+  NDNS_KEY  = ndn::tlv::ContentType_Key,
+  NDNS_NACK = ndn::tlv::ContentType_Nack,
+  NDNS_AUTH = 1086, ///< only has RR for detailed (longer) label
+  NDNS_RESP = 1087, ///< response type means there are requested RR
+  NDNS_UNKNOWN = 1088,  ///< this is not a real type, just mean that contentType is unknown
 };
 
 std::ostream&
-operator<<(std::ostream& os, const NdnsType ndnsType);
+operator<<(std::ostream& os, const NdnsContentType contentType);
 
 /**
  * @brief define Return code of Update's Response
diff --git a/src/ndns-tlv.hpp b/src/ndns-tlv.hpp
index 3f8399d..27235a3 100644
--- a/src/ndns-tlv.hpp
+++ b/src/ndns-tlv.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -30,7 +30,6 @@
  * @brief the field defined in NDNS packet format
  */
 enum {
-  NdnsType = 180, ///< Detailed Types are defined in NdnsType in ndns-enum.hpp
   Rr = 190,
   RrData = 191,
 
diff --git a/src/util/util.cpp b/src/util/util.cpp
index 1b14896..31f3549 100644
--- a/src/util/util.cpp
+++ b/src/util/util.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -23,8 +23,8 @@
 namespace ndn {
 namespace ndns {
 
-NdnsType
-toNdnsType(const std::string& str)
+NdnsContentType
+toNdnsContentType(const std::string& str)
 {
   if (str == "resp")
     return NDNS_RESP;
@@ -32,8 +32,10 @@
     return NDNS_NACK;
   else if (str == "auth")
     return NDNS_AUTH;
-  else if (str == "raw")
-    return NDNS_RAW;
+  else if (str == "blob")
+    return NDNS_BLOB;
+  else if (str == "link")
+    return NDNS_LINK;
   else
     return NDNS_UNKNOWN;
 }
diff --git a/src/util/util.hpp b/src/util/util.hpp
index 7acf496..24aa179 100644
--- a/src/util/util.hpp
+++ b/src/util/util.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014, Regents of the University of California.
+ * Copyright (c) 2014-2016, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
  * See AUTHORS.md for complete list of NDNS authors and contributors.
@@ -26,8 +26,8 @@
 namespace ndn {
 namespace ndns {
 
-NdnsType
-toNdnsType(const std::string& str);
+NdnsContentType
+toNdnsContentType(const std::string& str);
 
 /**
  * @brief print the data in a flexible way