core: use markers in StatusDataset and NotificationStream

This commit also refactors FaceMonitor as generic NotificationSubscriber,
and refactors AutoregServer to use FaceMonitor.

refs #1837 #1838

Change-Id: I8b40dfae118853d1224c8290cf92e7cc0daa116f
diff --git a/tests/core/notification-stream.cpp b/tests/core/notification-stream.cpp
new file mode 100644
index 0000000..df3fc5b
--- /dev/null
+++ b/tests/core/notification-stream.cpp
@@ -0,0 +1,69 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  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/>.
+ */
+
+#include "core/notification-stream.hpp"
+#include "simple-notification.hpp"
+
+#include "tests/test-common.hpp"
+#include "tests/dummy-client-face.hpp"
+
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreNotificationStream, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(Post)
+{
+  shared_ptr<DummyClientFace> face = makeDummyClientFace();
+  ndn::KeyChain keyChain;
+  NotificationStream<DummyClientFace> notificationStream(*face,
+    "/localhost/nfd/NotificationStreamTest", keyChain);
+
+  SimpleNotification event1("msg1");
+  notificationStream.postNotification(event1);
+  face->processEvents();
+  BOOST_REQUIRE_EQUAL(face->m_sentDatas.size(), 1);
+  BOOST_CHECK_EQUAL(face->m_sentDatas[0].getName(),
+                    "/localhost/nfd/NotificationStreamTest/%FE%00");
+  SimpleNotification decoded1;
+  BOOST_CHECK_NO_THROW(decoded1.wireDecode(face->m_sentDatas[0].getContent().blockFromValue()));
+  BOOST_CHECK_EQUAL(decoded1.getMessage(), "msg1");
+
+  SimpleNotification event2("msg2");
+  notificationStream.postNotification(event2);
+  face->processEvents();
+  BOOST_REQUIRE_EQUAL(face->m_sentDatas.size(), 2);
+  BOOST_CHECK_EQUAL(face->m_sentDatas[1].getName(),
+                    "/localhost/nfd/NotificationStreamTest/%FE%01");
+  SimpleNotification decoded2;
+  BOOST_CHECK_NO_THROW(decoded2.wireDecode(face->m_sentDatas[1].getContent().blockFromValue()));
+  BOOST_CHECK_EQUAL(decoded2.getMessage(), "msg2");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/core/notification-subscriber.cpp b/tests/core/notification-subscriber.cpp
new file mode 100644
index 0000000..12834f6
--- /dev/null
+++ b/tests/core/notification-subscriber.cpp
@@ -0,0 +1,223 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  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/>.
+ */
+
+#include "core/notification-subscriber.hpp"
+#include "core/notification-stream.hpp"
+#include "simple-notification.hpp"
+
+#include "tests/test-common.hpp"
+#include "tests/dummy-client-face.hpp"
+
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreNotificationSubscriber, BaseFixture)
+
+class EndToEndFixture : public BaseFixture
+{
+public:
+  EndToEndFixture()
+    : streamPrefix("ndn:/NotificationSubscriberTest")
+    , publisherFace(makeDummyClientFace())
+    , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
+    , subscriberFace(makeDummyClientFace())
+    , subscriber(*subscriberFace, streamPrefix)
+  {
+  }
+
+  /** \brief post one notification and deliver to subscriber
+   */
+  void
+  deliverNotification(const std::string& msg)
+  {
+    publisherFace->m_sentDatas.clear();
+    SimpleNotification notification(msg);
+    notificationStream.postNotification(notification);
+    publisherFace->processEvents();
+    BOOST_REQUIRE_EQUAL(publisherFace->m_sentDatas.size(), 1);
+
+    lastDeliveredSeqNo = publisherFace->m_sentDatas[0].getName().at(-1).toSequenceNumber();
+
+    lastNotification.setMessage("");
+    subscriberFace->receive(publisherFace->m_sentDatas[0]);
+  }
+
+  void
+  afterNotification(const SimpleNotification& notification)
+  {
+    lastNotification = notification;
+  }
+
+  void
+  clearNotificationHandlers()
+  {
+    subscriber.onNotification.clear();
+  }
+
+  void
+  afterTimeout()
+  {
+    hasTimeout = true;
+  }
+
+  void
+  afterDecodeError(const Data& data)
+  {
+    lastDecodeErrorData = data;
+  }
+
+  /** \return true if subscriberFace has an initial request as sole sent Interest
+   */
+  bool
+  hasInitialRequest() const
+  {
+    if (subscriberFace->m_sentInterests.size() != 1)
+      return 0;
+
+    const Interest& interest = subscriberFace->m_sentInterests[0];
+    return interest.getName() == streamPrefix &&
+           interest.getChildSelector() == 1 &&
+           interest.getMustBeFresh() &&
+           interest.getInterestLifetime() ==
+             NotificationSubscriber<SimpleNotification>::getInterestLifetime();
+  }
+
+  /** \return sequence number of the continuation request sent from subscriberFace
+   *          or 0 if there's no such request as sole sent Interest
+   */
+  uint64_t
+  getRequestSeqNo() const
+  {
+    if (subscriberFace->m_sentInterests.size() != 1)
+      return 0;
+
+    const Interest& interest = subscriberFace->m_sentInterests[0];
+    const Name& name = interest.getName();
+    if (streamPrefix.isPrefixOf(name) &&
+        name.size() == streamPrefix.size() + 1 &&
+        interest.getInterestLifetime() ==
+          NotificationSubscriber<SimpleNotification>::getInterestLifetime())
+      return name[-1].toSequenceNumber();
+    else
+      return 0;
+  }
+
+protected:
+  Name streamPrefix;
+  shared_ptr<DummyClientFace> publisherFace;
+  ndn::KeyChain publisherKeyChain;
+  NotificationStream<DummyClientFace> notificationStream;
+  shared_ptr<DummyClientFace> subscriberFace;
+  NotificationSubscriber<SimpleNotification> subscriber;
+
+  uint64_t lastDeliveredSeqNo;
+
+  SimpleNotification lastNotification;
+  bool hasTimeout;
+  Data lastDecodeErrorData;
+};
+
+BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
+{
+  BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
+
+  // has no effect because onNotification has no handler
+  subscriber.start();
+  BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false);
+
+  subscriber.onNotification += bind(&EndToEndFixture::afterNotification, this, _1);
+  subscriber.onTimeout += bind(&EndToEndFixture::afterTimeout, this);
+  subscriber.onDecodeError += bind(&EndToEndFixture::afterDecodeError, this, _1);
+
+  // not received when subscriber is not running
+  this->deliverNotification("n1");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK(lastNotification.getMessage().empty());
+  BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0);
+
+  subscriberFace->m_sentInterests.clear();
+  subscriber.start();
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
+  BOOST_CHECK(this->hasInitialRequest());
+
+  // respond to initial request
+  subscriberFace->m_sentInterests.clear();
+  this->deliverNotification("n2");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
+  BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
+
+  // respond to continuation request
+  subscriberFace->m_sentInterests.clear();
+  this->deliverNotification("n3");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
+  BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
+
+  // timeout
+  subscriberFace->m_sentInterests.clear();
+  lastNotification.setMessage("");
+  subscriberFace->processEvents(
+    NotificationSubscriber<SimpleNotification>::getInterestLifetime() +
+    time::milliseconds(1000));
+  BOOST_CHECK(lastNotification.getMessage().empty());
+  BOOST_CHECK_EQUAL(hasTimeout, true);
+  BOOST_CHECK(this->hasInitialRequest());
+
+  // decode error on sequence number
+  Name wrongName = streamPrefix;
+  wrongName.append("%07%07");
+  Data wrongData(wrongName);
+  publisherKeyChain.sign(wrongData);
+  subscriberFace->receive(wrongData);
+  subscriberFace->m_sentInterests.clear();
+  lastNotification.setMessage("");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK(lastNotification.getMessage().empty());
+  BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
+  BOOST_CHECK(this->hasInitialRequest());
+
+  // decode error in payload
+  subscriberFace->m_sentInterests.clear();
+  lastNotification.setMessage("");
+  this->deliverNotification("\x07n4");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK(lastNotification.getMessage().empty());
+  BOOST_CHECK(this->hasInitialRequest());
+
+  // stop if handlers are cleared
+  subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this);
+  subscriberFace->m_sentInterests.clear();
+  this->deliverNotification("n5");
+  subscriberFace->processEvents(time::milliseconds(10));
+  BOOST_CHECK_EQUAL(subscriberFace->m_sentInterests.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/core/segment-publisher.cpp b/tests/core/segment-publisher.cpp
index f859408..8817669 100644
--- a/tests/core/segment-publisher.cpp
+++ b/tests/core/segment-publisher.cpp
@@ -26,7 +26,7 @@
 #include "core/segment-publisher.hpp"
 
 #include "tests/test-common.hpp"
-#include "tests/dummy-face.hpp"
+#include "tests/dummy-client-face.hpp"
 
 #include <ndn-cxx/encoding/tlv.hpp>
 
@@ -38,10 +38,10 @@
 NFD_LOG_INIT("SegmentPublisherTest");
 
 template<int64_t N=10000>
-class TestSegmentPublisher : public SegmentPublisher<DummyFace>
+class TestSegmentPublisher : public SegmentPublisher<DummyClientFace>
 {
 public:
-  TestSegmentPublisher(DummyFace& face,
+  TestSegmentPublisher(DummyClientFace& face,
                        const Name& prefix,
                        ndn::KeyChain& keyChain)
     : SegmentPublisher(face, prefix, keyChain)
@@ -90,7 +90,7 @@
 {
 public:
   SegmentPublisherFixture()
-    : m_face(makeDummyFace())
+    : m_face(makeDummyClientFace())
     , m_publisher(*m_face, "/localhost/nfd/SegmentPublisherFixture", m_keyChain)
   {
   }
@@ -134,7 +134,7 @@
   }
 
 protected:
-  shared_ptr<DummyFace> m_face;
+  shared_ptr<DummyClientFace> m_face;
   TestSegmentPublisher<N> m_publisher;
   ndn::EncodingBuffer m_buffer;
   ndn::KeyChain m_keyChain;
diff --git a/tests/core/simple-notification.hpp b/tests/core/simple-notification.hpp
new file mode 100644
index 0000000..3c8fdef
--- /dev/null
+++ b/tests/core/simple-notification.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  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 NFD_TESTS_CORE_SIMPLE_NOTIFICATION_HPP
+#define NFD_TESTS_CORE_SIMPLE_NOTIFICATION_HPP
+
+#include "common.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+class SimpleNotification
+{
+public:
+  SimpleNotification()
+  {
+  }
+
+  SimpleNotification(const std::string& message)
+    : m_message(message)
+  {
+  }
+
+  ~SimpleNotification()
+  {
+  }
+
+  Block
+  wireEncode() const
+  {
+    ndn::EncodingBuffer buffer;
+    prependByteArrayBlock(buffer,
+                          0x8888,
+                          reinterpret_cast<const uint8_t*>(m_message.c_str()),
+                          m_message.size());
+    return buffer.block();
+  }
+
+  void
+  wireDecode(const Block& block)
+  {
+    m_message.assign(reinterpret_cast<const char*>(block.value()),
+                     block.value_size());
+
+    // error for testing
+    if (!m_message.empty() && m_message[0] == '\x07')
+      throw tlv::Error("0x07 error");
+  }
+
+public:
+  const std::string&
+  getMessage() const
+  {
+    return m_message;
+  }
+
+  void
+  setMessage(const std::string& message)
+  {
+    m_message = message;
+  }
+
+private:
+  std::string m_message;
+};
+
+} // namespace tests
+} // namespace nfd
+
+#endif // NFD_TESTS_CORE_SIMPLE_NOTIFICATION_HPP