src: use psync::SegmentPublisher and fix proccessing of interests w/ segments

refs: #4784, #4760

Change-Id: Ia017a6f340e1cd35991068453c9da595c453ca1f
diff --git a/src/common.hpp b/src/common.hpp
index 3337520..cd6dc18 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2017,  The University of Memphis,
+ * Copyright (c) 2014-2018,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -27,7 +27,6 @@
 #ifndef NLSR_COMMON_HPP
 #define NLSR_COMMON_HPP
 
-#include <ndn-cxx/common.hpp>
 #include <ndn-cxx/util/time.hpp>
 #include <ndn-cxx/name.hpp>
 
diff --git a/src/lsa-segment-storage.cpp b/src/lsa-segment-storage.cpp
index dfe92d7..830906d 100644
--- a/src/lsa-segment-storage.cpp
+++ b/src/lsa-segment-storage.cpp
@@ -97,21 +97,8 @@
       NLSR_LOG_TRACE("The received segment is already in the storage.");
     }
 
-    std::string content(reinterpret_cast<const char*>(lsaSegment.getContent().value()),
-                        lsaSegment.getContent().value_size());
-
     ndn::time::seconds expirationTime(LSA_REFRESH_TIME_DEFAULT);
 
-    std::vector<std::string> options;
-    boost::split(options, content, boost::is_any_of("|"));
-
-    try {
-      expirationTime = ndn::time::duration_cast<ndn::time::seconds>
-                       (ndn::time::fromIsoString(options.at(3)) - ndn::time::system_clock::now());
-    } catch (const std::exception& e) {
-      NLSR_LOG_ERROR("Cannot extract expiration time from LSA content: " << e.what());
-    }
-
     // schedule the segment deletion
     scheduleLsaSegmentDeletion(lsaSegmentsKey, expirationTime);
   }
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index b8cda8e..1507a08 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -24,7 +24,6 @@
 #include "logger.hpp"
 #include "lsa-segment-storage.hpp"
 #include "nlsr.hpp"
-#include "publisher/segment-publisher.hpp"
 #include "utility/name-helper.hpp"
 
 #include <ndn-cxx/security/signing-helpers.hpp>
@@ -33,31 +32,6 @@
 
 INIT_LOGGER(Lsdb);
 
-class LsaContentPublisher : public SegmentPublisher<ndn::Face>
-{
-public:
-  LsaContentPublisher(ndn::Face& face,
-                      ndn::KeyChain& keyChain,
-                      const ndn::security::SigningInfo& signingInfo,
-                      const ndn::time::milliseconds& freshnessPeriod,
-                      const std::string& content)
-    : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod)
-    , m_content(content)
-  {
-  }
-
-  virtual size_t
-  generate(ndn::EncodingBuffer& outBuffer) {
-    size_t totalLength = 0;
-    totalLength += outBuffer.prependByteArray(reinterpret_cast<const uint8_t*>(m_content.c_str()),
-                                              m_content.size());
-    return totalLength;
-  }
-
-private:
-  const std::string m_content;
-};
-
 const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
 const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
 const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
@@ -76,11 +50,12 @@
   , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
   , m_sequencingManager()
   , m_onNewLsaConnection(m_sync.onNewLsa->connect(
-      [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
+      [this] (const ndn::Name& updateName, uint64_t sequenceNumber) {
         ndn::Name lsaInterest{updateName};
         lsaInterest.appendNumber(sequenceNumber);
         expressInterest(lsaInterest, 0);
       }))
+  , m_segmentPublisher(m_nlsr.getNlsrFace(), m_nlsr.getKeyChain())
 {
 }
 
@@ -127,7 +102,7 @@
 Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
 {
   std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
-  data->setContent(bufferPtr);
+  data->setContent(ndn::Block(bufferPtr));
 
   NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
 
@@ -1050,48 +1025,53 @@
 void
 Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
 {
+  ndn::Name interestName(interest.getName());
+  NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
+
+  if (interestName[-2].isVersion()) {
+    // Interest for particular segment
+    if (m_segmentPublisher.replyFromStore(interestName)) {
+      NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
+      return;
+    }
+    // Remove version and segment
+    interestName = interestName.getSubName(0, interestName.size() - 2);
+    NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
+  }
+
   // increment RCV_LSA_INTEREST
   lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
 
-  const ndn::Name& interestName(interest.getName());
-  NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
-
   std::string chkString("LSA");
-  int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
+  int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
 
   // Forms the name of the router that the Interest packet came from.
   ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
   originRouter.append(interestName.getSubName(lsaPosition + 1,
-                                              interest.getName().size() - lsaPosition - 3));
+                                              interestName.size() - lsaPosition - 3));
 
   // if the interest is for this router's LSA
-  if (originRouter == m_nlsr.getConfParameter().getRouterPrefix()) {
+  if (originRouter == m_nlsr.getConfParameter().getRouterPrefix() && lsaPosition >= 0) {
+    uint64_t seqNo = interestName[-1].toNumber();
+    NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
 
-    if (lsaPosition >= 0) {
+    std::string lsaType = interestName[-2].toUri();
+    Lsa::Type interestedLsType;
+    std::istringstream(lsaType) >> interestedLsType;
 
-      uint64_t seqNo = interestName[-1].toNumber();
-      NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
-
-      Lsa::Type interestedLsType;
-      std::istringstream(interestName[-2].toUri()) >> interestedLsType;
-
-      if (interestedLsType == Lsa::Type::NAME) {
-        processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
-                                  seqNo);
-      }
-      else if (interestedLsType == Lsa::Type::ADJACENCY) {
-        processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
-                                       seqNo);
-      }
-      else if (interestedLsType == Lsa::Type::COORDINATE) {
-        processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
-                                        seqNo);
-      }
-      else {
-        NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
-      }
-      lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
+    if (interestedLsType == Lsa::Type::NAME) {
+      processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
     }
+    else if (interestedLsType == Lsa::Type::ADJACENCY) {
+      processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
+    }
+    else if (interestedLsType == Lsa::Type::COORDINATE) {
+      processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
+    }
+    else {
+      NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
+    }
+    lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
   }
   else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
     const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
@@ -1100,27 +1080,11 @@
       m_nlsr.getNlsrFace().put(*lsaSegment);
     }
     else {
-      NLSR_LOG_TRACE(interest << "  was not found in this lsa storage.");
+      NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
     }
   }
 }
 
-  // \brief Sends LSA data.
-  // \param interest The Interest that warranted the data.
-  // \param content The data that the Interest was seeking.
-void
-Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
-{
-  LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
-                                m_nlsr.getKeyChain(),
-                                m_nlsr.getSigningInfo(),
-                                m_lsaRefreshTime,
-                                content);
-  NLSR_LOG_DEBUG("Sending requested data ( " << content << ")  for interest (" << interest
-             << ") to be published and added to face.");
-  publisher.publish(interest.getName());
-}
-
   // \brief Finds and sends a requested name LSA.
   // \param interest The interest that seeks the name LSA.
   // \param lsaKey The LSA that the Interest is seeking.
@@ -1134,17 +1098,15 @@
   // increment RCV_NAME_LSA_INTEREST
   lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
   NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
-  NameLsa*  nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
+  NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
   if (nameLsa != nullptr) {
     NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
     if (nameLsa->getLsSeqNo() == seqNo) {
-      // if requested lsa belongs to this router then sign it and serve it
       std::string content = nameLsa->serialize();
-      putLsaData(interest,content);
-      // else the requested belongs to neighboring routers, so serve the
-      // original data packet corresponding to the lsa
+      m_segmentPublisher.publish(interest.getName(), interest.getName(),
+                                 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
+                                 m_lsaRefreshTime, m_nlsr.getSigningInfo());
 
-      // increment SENT_NAME_LSA_DATA
       lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
     }
     else {
@@ -1170,7 +1132,6 @@
     NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
   }
 
-  // increment RCV_ADJ_LSA_INTEREST
   lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
   NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
   AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
@@ -1178,8 +1139,10 @@
     NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
     if (adjLsa->getLsSeqNo() == seqNo) {
       std::string content = adjLsa->serialize();
-      putLsaData(interest,content);
-      // increment SENT_ADJ_LSA_DATA
+      m_segmentPublisher.publish(interest.getName(), interest.getName(),
+                                 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
+                                 m_lsaRefreshTime, m_nlsr.getSigningInfo());
+
       lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
     }
     else {
@@ -1205,7 +1168,6 @@
     NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
   }
 
-  // increment RCV_COORD_LSA_INTEREST
   lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
   NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
   CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
@@ -1213,8 +1175,10 @@
     NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
     if (corLsa->getLsSeqNo() == seqNo) {
       std::string content = corLsa->serialize();
-      putLsaData(interest,content);
-      // increment SENT_COORD_LSA_DATA
+      m_segmentPublisher.publish(interest.getName(), interest.getName(),
+                                 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
+                                 m_lsaRefreshTime, m_nlsr.getSigningInfo());
+
       lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
     }
     else {
@@ -1264,7 +1228,6 @@
       NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
     }
 
-    // increment RCV_LSA_DATA
     lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
   }
 }
@@ -1273,7 +1236,6 @@
 Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
                             uint64_t lsSeqNo, std::string& dataContent)
 {
-  // increment RCV_NAME_LSA_DATA
   lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
   if (isNameLsaNew(lsaKey, lsSeqNo)) {
     NameLsa nameLsa;
@@ -1290,7 +1252,6 @@
 Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
                                  uint64_t lsSeqNo, std::string& dataContent)
 {
-  // increment RCV_ADJ_LSA_DATA
   lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
   if (isAdjLsaNew(lsaKey, lsSeqNo)) {
     AdjLsa adjLsa;
@@ -1307,7 +1268,6 @@
 Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
                                   uint64_t lsSeqNo, std::string& dataContent)
 {
-  // increment RCV_COORD_LSA_DATA
   lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
   if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
     CoordinateLsa corLsa;
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 2ebb952..183f3dc 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -35,6 +35,8 @@
 #include <ndn-cxx/util/time.hpp>
 #include <ndn-cxx/util/segment-fetcher.hpp>
 
+#include <PSync/segment-publisher.hpp>
+
 #include <utility>
 #include <boost/cstdint.hpp>
 
@@ -219,6 +221,12 @@
   expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
                   ndn::time::steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
 
+  /* \brief Process interest which can be either:
+   * 1) Discovery interest from segment fetcher:
+   *    /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>
+   * 2) Interest containing segment number:
+   *    /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>/<version>/<segmentNo>
+  */
   void
   processInterest(const ndn::Name& name, const ndn::Interest& interest);
 
@@ -311,9 +319,6 @@
 private:
 
   void
-  putLsaData(const ndn::Interest& interest, const std::string& content);
-
-  void
   processInterestForNameLsa(const ndn::Interest& interest,
                             const ndn::Name& lsaKey,
                             uint64_t seqNo);
@@ -417,6 +422,7 @@
   ndn::util::signal::ScopedConnection m_onNewLsaConnection;
 
   std::set<std::shared_ptr<ndn::util::SegmentFetcher>> m_fetchers;
+  psync::SegmentPublisher m_segmentPublisher;
 };
 
 } // namespace nlsr
diff --git a/src/publisher/segment-publisher.hpp b/src/publisher/segment-publisher.hpp
deleted file mode 100644
index d80c90e..0000000
--- a/src/publisher/segment-publisher.hpp
+++ /dev/null
@@ -1,141 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018,  Regents of the University of California,
- *                           Arizona Board of Regents,
- *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne University,
- *                           Washington University in St. Louis,
- *                           Beijing Institute of Technology,
- *                           The University of Memphis.
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP
-#define NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP
-
-#include <ndn-cxx/encoding/encoding-buffer.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-
-namespace nlsr {
-
-/*! \brief provides a publisher of Status Dataset or other segmented octet stream
-    \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
- */
-template <class FaceBase>
-class SegmentPublisher : boost::noncopyable
-{
-public:
-  SegmentPublisher(FaceBase& face,
-                   ndn::KeyChain& keyChain,
-                   const ndn::security::SigningInfo& signingInfo,
-                   const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
-    : m_face(face)
-    , m_keyChain(keyChain)
-    , m_signingInfo(signingInfo)
-    , m_freshnessPeriod(freshnessPeriod)
-  {
-  }
-
-  virtual
-  ~SegmentPublisher()
-  {
-  }
-
-  /*! \brief Define the max segment size as half the max NDN packet size.
-   */
-  static size_t
-  getMaxSegmentSize()
-  {
-    static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1;
-    return MAX_SEGMENT_SIZE;
-  }
-
-  static constexpr ndn::time::milliseconds
-  getDefaultFreshness()
-  {
-    return ndn::time::milliseconds(1000);
-  }
-
-  /*! \brief Publish data under the provided prefix
-   *
-   * Processes whatever is provided from SegmentPublisher::generate,
-   * by breaking it into segments of MAX_SEGMENT_SIZE and sending each
-   * one individually. The last segment is distinguished by having the
-   * final block ID set to a timestamp.
-   */
-  void
-  publish(const ndn::Name& prefix)
-  {
-    ndn::EncodingBuffer buffer;
-    generate(buffer);
-
-    const uint8_t* rawBuffer = buffer.buf();
-    const uint8_t* segmentBegin = rawBuffer;
-    const uint8_t* end = rawBuffer + buffer.size();
-
-    ndn::Name segmentPrefix(prefix);
-    segmentPrefix.appendVersion();
-
-    uint64_t segmentNo = 0;
-    do {
-      const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
-      if (segmentEnd > end) {
-        segmentEnd = end;
-      }
-
-      ndn::Name segmentName(segmentPrefix);
-      segmentName.appendSegment(segmentNo);
-
-      std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
-      data->setContent(segmentBegin, segmentEnd - segmentBegin);
-      data->setFreshnessPeriod(m_freshnessPeriod);
-
-      segmentBegin = segmentEnd;
-      if (segmentBegin >= end) {
-        data->setFinalBlock(segmentName[-1]);
-      }
-
-      publishSegment(data);
-      ++segmentNo;
-    } while (segmentBegin < end);
-  }
-
-protected:
-  /*! \brief In a derived class, write the octets into outBuffer.
-   */
-  virtual size_t
-  generate(ndn::EncodingBuffer& outBuffer) = 0;
-
-private:
-  /*! \brief Helper function to sign and put data on a Face.
-   */
-  void
-  publishSegment(std::shared_ptr<ndn::Data>& data)
-  {
-    m_keyChain.sign(*data, m_signingInfo);
-    m_face.put(*data);
-  }
-
-private:
-  FaceBase& m_face;
-  ndn::KeyChain& m_keyChain;
-  const ndn::security::SigningInfo& m_signingInfo;
-  const ndn::time::milliseconds m_freshnessPeriod;
-};
-
-} // namespace nlsr
-
-#endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP