change default FreshnessPeriod to 0 ms

set InterestLifetime to default value when not present

do not encode ChildSelector if at default value

refs #3944

Change-Id: I4a3ebf5512d0d2270798ed212eab7903ba8b8ed0
diff --git a/src/interest.cpp b/src/interest.cpp
index 17a7aa4..80434ab 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -36,14 +36,14 @@
               "Interest::Error must inherit from tlv::Error");
 
 Interest::Interest()
-  : m_interestLifetime(time::milliseconds::min())
+  : m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
   , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
 {
 }
 
 Interest::Interest(const Name& name)
   : m_name(name)
-  , m_interestLifetime(time::milliseconds::min())
+  , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
   , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
 {
 }
@@ -222,6 +222,17 @@
           this->getSelectors() == other.getSelectors());
 }
 
+Interest&
+Interest::setInterestLifetime(time::milliseconds interestLifetime)
+{
+  if (interestLifetime < time::milliseconds::zero()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
+  }
+  m_interestLifetime = interestLifetime;
+  m_wire.reset();
+  return *this;
+}
+
 template<encoding::Tag TAG>
 size_t
 Interest::wireEncode(EncodingImpl<TAG>& encoder) const
@@ -251,23 +262,20 @@
   }
 
   // InterestLifetime
-  if (getInterestLifetime() >= time::milliseconds::zero() &&
-      getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
-    {
-      totalLength += prependNonNegativeIntegerBlock(encoder,
-                                                    tlv::InterestLifetime,
-                                                    getInterestLifetime().count());
-    }
+  if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
+    totalLength += prependNonNegativeIntegerBlock(encoder,
+                                                  tlv::InterestLifetime,
+                                                  getInterestLifetime().count());
+  }
 
   // Nonce
   getNonce(); // to ensure that Nonce is properly set
   totalLength += encoder.prependBlock(m_nonce);
 
   // Selectors
-  if (hasSelectors())
-    {
-      totalLength += getSelectors().wireEncode(encoder);
-    }
+  if (hasSelectors()) {
+    totalLength += getSelectors().wireEncode(encoder);
+  }
 
   // Name
   totalLength += getName().wireEncode(encoder);
@@ -469,7 +477,7 @@
     os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
     delim = '&';
   }
-  if (interest.getChildSelector() >= 0) {
+  if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) {
     os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
     delim = '&';
   }
@@ -477,8 +485,7 @@
     os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
     delim = '&';
   }
-  if (interest.getInterestLifetime() >= time::milliseconds::zero()
-      && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
+  if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
     os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
     delim = '&';
   }
diff --git a/src/interest.hpp b/src/interest.hpp
index 8d3f29b..e98747e 100644
--- a/src/interest.hpp
+++ b/src/interest.hpp
@@ -242,13 +242,12 @@
     return m_interestLifetime;
   }
 
+  /**
+   * @brief Set Interest's lifetime
+   * @throw std::invalid_argument specified lifetime is < 0
+   */
   Interest&
-  setInterestLifetime(const time::milliseconds& interestLifetime)
-  {
-    m_interestLifetime = interestLifetime;
-    m_wire.reset();
-    return *this;
-  }
+  setInterestLifetime(time::milliseconds interestLifetime);
 
   /** @brief Check if Nonce set
    */
diff --git a/src/meta-info.cpp b/src/meta-info.cpp
index 0fd6923..41ad0f7 100644
--- a/src/meta-info.cpp
+++ b/src/meta-info.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -34,7 +34,7 @@
 
 MetaInfo::MetaInfo()
   : m_type(tlv::ContentType_Blob)
-  , m_freshnessPeriod(-1)
+  , m_freshnessPeriod(DEFAULT_FRESHNESS_PERIOD)
 {
 }
 
@@ -52,8 +52,11 @@
 }
 
 MetaInfo&
-MetaInfo::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
+MetaInfo::setFreshnessPeriod(time::milliseconds freshnessPeriod)
 {
+  if (freshnessPeriod < time::milliseconds::zero()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("FreshnessPeriod must be >= 0"));
+  }
   m_wire.reset();
   m_freshnessPeriod = freshnessPeriod;
   return *this;
@@ -143,23 +146,20 @@
   }
 
   // FinalBlockId
-  if (!m_finalBlockId.empty())
-    {
-      totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId);
-    }
+  if (!m_finalBlockId.empty()) {
+    totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId);
+  }
 
   // FreshnessPeriod
-  if (m_freshnessPeriod >= time::milliseconds::zero())
-    {
-      totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod,
-                                                    m_freshnessPeriod.count());
-    }
+  if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod,
+                                                  m_freshnessPeriod.count());
+  }
 
   // ContentType
-  if (m_type != tlv::ContentType_Blob)
-    {
-      totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
-    }
+  if (m_type != tlv::ContentType_Blob) {
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
+  }
 
   totalLength += encoder.prependVarNumber(totalLength);
   totalLength += encoder.prependVarNumber(tlv::MetaInfo);
@@ -218,7 +218,7 @@
     ++val;
   }
   else {
-    m_freshnessPeriod = time::milliseconds::min();
+    m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
   }
 
   // FinalBlockId
@@ -248,7 +248,7 @@
   os << "ContentType: " << info.getType();
 
   // FreshnessPeriod
-  if (info.getFreshnessPeriod() >= time::milliseconds::zero()) {
+  if (info.getFreshnessPeriod() > time::milliseconds::zero()) {
     os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
   }
 
diff --git a/src/meta-info.hpp b/src/meta-info.hpp
index 798acd1..bec2275 100644
--- a/src/meta-info.hpp
+++ b/src/meta-info.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -31,6 +31,8 @@
 
 namespace ndn {
 
+const time::milliseconds DEFAULT_FRESHNESS_PERIOD = time::milliseconds::zero();
+
 /**
  * An MetaInfo holds the meta info which is signed inside the data packet.
  *
@@ -97,8 +99,11 @@
   const time::milliseconds&
   getFreshnessPeriod() const;
 
+  /** @brief set FreshnessPeriod
+   *  @throw std::invalid_argument specified FreshnessPeriod is < 0
+   */
   MetaInfo&
-  setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
+  setFreshnessPeriod(time::milliseconds freshnessPeriod);
 
   const name::Component&
   getFinalBlockId() const;
diff --git a/src/selectors.cpp b/src/selectors.cpp
index 7b46b1e..dc741a3 100644
--- a/src/selectors.cpp
+++ b/src/selectors.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -35,7 +35,7 @@
 Selectors::Selectors()
   : m_minSuffixComponents(-1)
   , m_maxSuffixComponents(-1)
-  , m_childSelector(-1)
+  , m_childSelector(DEFAULT_CHILD_SELECTOR)
   , m_mustBeFresh(false)
 {
 }
@@ -78,7 +78,7 @@
   }
 
   // ChildSelector
-  if (getChildSelector() >= 0) {
+  if (getChildSelector() != DEFAULT_CHILD_SELECTOR) {
     totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ChildSelector, getChildSelector());
   }
 
@@ -171,6 +171,9 @@
   if (val != m_wire.elements_end()) {
     m_childSelector = readNonNegativeInteger(*val);
   }
+  else {
+    m_childSelector = DEFAULT_CHILD_SELECTOR;
+  }
 
   // MustBeFresh
   val = m_wire.find(tlv::MustBeFresh);
@@ -214,6 +217,9 @@
 Selectors&
 Selectors::setChildSelector(int childSelector)
 {
+  if (childSelector != 0 && childSelector != 1) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("ChildSelector must be 0 or 1"));
+  }
   m_childSelector = childSelector;
   m_wire.reset();
   return *this;
diff --git a/src/selectors.hpp b/src/selectors.hpp
index 9a1d8a4..960947f 100644
--- a/src/selectors.hpp
+++ b/src/selectors.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -28,6 +28,8 @@
 
 namespace ndn {
 
+const int DEFAULT_CHILD_SELECTOR = 0;
+
 /**
  * @brief Abstraction implementing Interest selectors
  */
@@ -117,6 +119,10 @@
     return m_childSelector;
   }
 
+  /**
+   * @brief set ChildSelector
+   * @throw std::invalid_argument ChildSelector not 0 or 1
+   */
   Selectors&
   setChildSelector(int childSelector);