util: Porting NotificationSubscriber, FaceMonitor, and NotificationStream from NFD

New generic classes:
- `util::NotificationSubscriber` (`util/notification-subscriber.hpp`)
- `util::NotificationStream` (`util/notification-stream.hpp`)

NFD Face monitoring class:
- `nfd::FaceMonitor` (`management/nfd-face-monitor.hpp`)

Change-Id: I2ab0a2cd9d7e3ac07036f290f0b4de5eb16e6e38
diff --git a/src/management/nfd-face-monitor.hpp b/src/management/nfd-face-monitor.hpp
new file mode 100644
index 0000000..07af961
--- /dev/null
+++ b/src/management/nfd-face-monitor.hpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 NDN_MANAGEMENT_NFD_FACE_MONITOR_HPP
+#define NDN_MANAGEMENT_NFD_MONITOR_HPP
+
+#include "../util/notification-subscriber.hpp"
+#include "nfd-face-event-notification.hpp"
+
+namespace ndn {
+namespace nfd {
+
+/** \brief A subscriber for Face status change notification stream
+ *  \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
+ */
+class FaceMonitor : public util::NotificationSubscriber<FaceEventNotification>
+{
+public:
+  FaceMonitor(Face& face)
+    : NotificationSubscriber<nfd::FaceEventNotification>(face, "ndn:/localhost/nfd/faces/events")
+  {
+  }
+};
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_FACE_MONITOR_HPP
diff --git a/src/util/concepts.hpp b/src/util/concepts.hpp
new file mode 100644
index 0000000..dee15d3
--- /dev/null
+++ b/src/util/concepts.hpp
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_CONCEPTS_HPP
+#define NDN_UTIL_CONCEPTS_HPP
+
+#include <boost/concept/usage.hpp>
+
+namespace ndn {
+
+template<class X>
+class WireEncodable
+{
+public:
+  BOOST_CONCEPT_USAGE(WireEncodable)
+  {
+    X j;
+    j.wireEncode();
+  }
+};
+
+template<class X>
+class WireDecodable
+{
+public:
+  BOOST_CONCEPT_USAGE(WireDecodable)
+  {
+    Block block;
+    X j(block);
+    j.wireDecode(block);
+  }
+};
+
+} // namespace ndn
+
+#endif // NDN_UTIL_CONCEPTS_HPP
diff --git a/src/util/notification-stream.hpp b/src/util/notification-stream.hpp
new file mode 100644
index 0000000..991a885
--- /dev/null
+++ b/src/util/notification-stream.hpp
@@ -0,0 +1,112 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 NDN_UTIL_NOTIFICATION_STREAM_HPP
+#define NDN_UTIL_NOTIFICATION_STREAM_HPP
+
+#include "../name.hpp"
+#include "../face.hpp"
+#include "../security/key-chain.hpp"
+
+#include "concepts.hpp"
+
+namespace ndn {
+
+class Face;
+class KeyChain;
+
+namespace util {
+
+/** \brief provides a publisher of Notification Stream
+ *  \sa http://redmine.named-data.net/projects/nfd/wiki/Notification
+ */
+template<typename Notification>
+class NotificationStream : noncopyable
+{
+public:
+  BOOST_CONCEPT_ASSERT((WireEncodable<Notification>));
+
+  NotificationStream(Face& face, const Name& prefix, KeyChain& keyChain)
+    : m_face(face)
+    , m_prefix(prefix)
+    , m_keyChain(keyChain)
+    , m_sequenceNo(0)
+  {
+  }
+
+  virtual
+  ~NotificationStream()
+  {
+  }
+
+  void
+  postNotification(const Notification& notification)
+  {
+    Name dataName = m_prefix;
+    dataName.appendSequenceNumber(m_sequenceNo);
+
+    shared_ptr<Data> data = make_shared<Data>(dataName);
+    data->setContent(notification.wireEncode());
+    data->setFreshnessPeriod(time::seconds(1));
+
+    m_keyChain.sign(*data);
+    m_face.put(*data);
+
+    ++m_sequenceNo;
+  }
+
+private:
+  Face& m_face;
+  const Name m_prefix;
+  KeyChain& m_keyChain;
+  uint64_t m_sequenceNo;
+};
+
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_NOTIFICATION_STREAM_HPP
diff --git a/src/util/notification-subscriber.hpp b/src/util/notification-subscriber.hpp
new file mode 100644
index 0000000..32d9d6a
--- /dev/null
+++ b/src/util/notification-subscriber.hpp
@@ -0,0 +1,245 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
+#define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
+
+#include "event-emitter.hpp"
+#include "../face.hpp"
+#include "concepts.hpp"
+#include <boost/concept_check.hpp>
+
+namespace ndn {
+namespace util {
+
+/** \brief provides a subscriber of Notification Stream
+ *  \sa http://redmine.named-data.net/projects/nfd/wiki/Notification
+ *  \tparam Notification type of Notification item, appears in payload of Data packets
+ */
+template<typename Notification>
+class NotificationSubscriber : noncopyable
+{
+public:
+  BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>));
+  BOOST_CONCEPT_ASSERT((WireDecodable<Notification>));
+
+  /** \brief construct a NotificationSubscriber
+   *  \note The subscriber is not started after construction.
+   *        User should add one or more handlers to onNotification, and invoke .start().
+   */
+  NotificationSubscriber(Face& face, const Name& prefix,
+                         const time::milliseconds& interestLifetime = time::milliseconds(60000))
+    : m_face(face)
+    , m_prefix(prefix)
+    , m_isRunning(false)
+    , m_lastSequenceNo(std::numeric_limits<uint64_t>::max())
+    , m_lastInterestId(0)
+    , m_interestLifetime(interestLifetime)
+  {
+  }
+
+  virtual
+  ~NotificationSubscriber()
+  {
+  }
+
+  /** \return InterestLifetime of Interests to retrieve notifications
+   *  \details This must be greater than FreshnessPeriod of Notification Data packets,
+   *           to ensure correct operation of this subscriber implementation.
+   */
+  time::milliseconds
+  getInterestLifetime() const
+  {
+    return m_interestLifetime;
+  }
+
+  bool
+  isRunning() const
+  {
+    return m_isRunning;
+  }
+
+  /** \brief start or resume receiving notifications
+   *  \note onNotification must have at least one listener,
+   *        otherwise this operation has no effect.
+   */
+  void
+  start()
+  {
+    if (m_isRunning) // already running
+      return;
+    m_isRunning = true;
+
+    this->sendInitialInterest();
+  }
+
+  /** \brief stop receiving notifications
+   */
+  void
+  stop()
+  {
+    if (!m_isRunning) // not running
+      return;
+    m_isRunning = false;
+
+    if (m_lastInterestId != 0)
+      m_face.removePendingInterest(m_lastInterestId);
+    m_lastInterestId = 0;
+  }
+
+public: // subscriptions
+  /** \brief fires when a Notification is received
+   *  \note Removing all handlers will cause the subscriber to stop.
+   */
+  EventEmitter<Notification> onNotification;
+
+  /** \brief fires when no Notification is received within .getInterestLifetime period
+   */
+  EventEmitter<> onTimeout;
+
+  /** \brief fires when a Data packet in the Notification Stream cannot be decoded as Notification
+   */
+  EventEmitter<Data> onDecodeError;
+
+private:
+  void
+  sendInitialInterest()
+  {
+    if (this->shouldStop())
+      return;
+
+    shared_ptr<Interest> interest = make_shared<Interest>(m_prefix);
+    interest->setMustBeFresh(true);
+    interest->setChildSelector(1);
+    interest->setInterestLifetime(getInterestLifetime());
+
+    m_lastInterestId = m_face.expressInterest(*interest,
+                         bind(&NotificationSubscriber<Notification>::afterReceiveData, this, _2),
+                         bind(&NotificationSubscriber<Notification>::afterTimeout, this));
+  }
+
+  void
+  sendNextInterest()
+  {
+    if (this->shouldStop())
+      return;
+
+    BOOST_ASSERT(m_lastSequenceNo !=
+                 std::numeric_limits<uint64_t>::max());// overflow or missing initial reply
+
+    Name nextName = m_prefix;
+    nextName.appendSequenceNumber(m_lastSequenceNo + 1);
+
+    shared_ptr<Interest> interest = make_shared<Interest>(nextName);
+    interest->setInterestLifetime(getInterestLifetime());
+
+    m_lastInterestId = m_face.expressInterest(*interest,
+                         bind(&NotificationSubscriber<Notification>::afterReceiveData, this, _2),
+                         bind(&NotificationSubscriber<Notification>::afterTimeout, this));
+  }
+
+  /** \brief Check if the subscriber is or should be stopped.
+   *  \return true if the subscriber is stopped.
+   */
+  bool
+  shouldStop()
+  {
+    if (!m_isRunning)
+      return true;
+    if (onNotification.isEmpty()) {
+      this->stop();
+      return true;
+    }
+    return false;
+  }
+
+  void
+  afterReceiveData(const Data& data)
+  {
+    if (this->shouldStop())
+      return;
+
+    Notification notification;
+    try {
+      m_lastSequenceNo = data.getName().get(-1).toSequenceNumber();
+      notification.wireDecode(data.getContent().blockFromValue());
+    }
+    catch (tlv::Error&) {
+      this->onDecodeError(data);
+      this->sendInitialInterest();
+      return;
+    }
+
+    this->onNotification(notification);
+
+    this->sendNextInterest();
+  }
+
+  void
+  afterTimeout()
+  {
+    if (this->shouldStop())
+      return;
+
+    this->onTimeout();
+
+    this->sendInitialInterest();
+  }
+
+private:
+  Face& m_face;
+  Name m_prefix;
+  bool m_isRunning;
+  uint64_t m_lastSequenceNo;
+  const PendingInterestId* m_lastInterestId;
+  time::milliseconds m_interestLifetime;
+};
+
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
diff --git a/tests/unit-tests/transport/dummy-face.hpp b/tests/unit-tests/dummy-client-face.hpp
similarity index 69%
rename from tests/unit-tests/transport/dummy-face.hpp
rename to tests/unit-tests/dummy-client-face.hpp
index faeae55..d8789bd 100644
--- a/tests/unit-tests/transport/dummy-face.hpp
+++ b/tests/unit-tests/dummy-client-face.hpp
@@ -19,21 +19,23 @@
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
-#define NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
+#ifndef NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
+#define NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
 
 #include "face.hpp"
 #include "transport/transport.hpp"
 
 namespace ndn {
+namespace tests {
 
-class DummyTransport : public Transport
+class DummyClientTransport : public ndn::Transport
 {
 public:
   void
   receive(const Block& block)
   {
-    m_receiveCallback(block);
+    if (static_cast<bool>(m_receiveCallback))
+      m_receiveCallback(block);
   }
 
   virtual void
@@ -74,13 +76,13 @@
 };
 
 
-/** \brief a Face for unit testing
+/** \brief a client-side face for unit testing
  */
-class DummyFace : public Face
+class DummyClientFace : public ndn::Face
 {
 public:
   explicit
-  DummyFace(shared_ptr<DummyTransport> transport)
+  DummyClientFace(shared_ptr<DummyClientTransport> transport)
     : Face(transport)
     , m_transport(transport)
   {
@@ -98,19 +100,28 @@
   }
 
 public:
+  /** \brief sent Interests
+   *  \note After .expressInterest, .processEvents must be called before
+   *        the Interest would show up here.
+   */
   std::vector<Interest> m_sentInterests;
+  /** \brief sent Datas
+   *  \note After .put, .processEvents must be called before
+   *        the Interest would show up here.
+   */
   std::vector<Data>     m_sentDatas;
 
 private:
-  shared_ptr<DummyTransport> m_transport;
+  shared_ptr<DummyClientTransport> m_transport;
 };
 
-inline shared_ptr<DummyFace>
-makeDummyFace()
+inline shared_ptr<DummyClientFace>
+makeDummyClientFace()
 {
-  return make_shared<DummyFace>(make_shared<DummyTransport>());
+  return make_shared<DummyClientFace>(make_shared<DummyClientTransport>());
 }
 
+} // namespace tests
 } // namespace ndn
 
-#endif // NDN_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
+#endif // NDN_TESTS_UNIT_TESTS_DUMMY_CLIENT_FACE_HPP
diff --git a/tests/unit-tests/management/test-nfd-controller.cpp b/tests/unit-tests/management/test-nfd-controller.cpp
index 525d0d0..7132c69 100644
--- a/tests/unit-tests/management/test-nfd-controller.cpp
+++ b/tests/unit-tests/management/test-nfd-controller.cpp
@@ -22,7 +22,7 @@
 #include "management/nfd-controller.hpp"
 #include "management/nfd-control-response.hpp"
 
-#include "../transport/dummy-face.hpp"
+#include "../dummy-client-face.hpp"
 
 #include <boost/tuple/tuple.hpp>
 
@@ -30,6 +30,9 @@
 
 namespace ndn {
 namespace nfd {
+namespace tests {
+
+using namespace ::ndn::tests;
 
 BOOST_AUTO_TEST_SUITE(ManagementTestNfdController)
 
@@ -37,7 +40,7 @@
 {
 protected:
   CommandFixture()
-    : face(makeDummyFace())
+    : face(makeDummyClientFace())
     , controller(*face)
     , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1))
     , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2))
@@ -58,7 +61,7 @@
   }
 
 protected:
-  shared_ptr<DummyFace> face;
+  shared_ptr<DummyClientFace> face;
   Controller controller;
   KeyChain keyChain;
 
@@ -188,5 +191,6 @@
 
 BOOST_AUTO_TEST_SUITE_END()
 
+} // namespace tests
 } // namespace nfd
 } // namespace ndn
diff --git a/tests/unit-tests/util/notification-stream.cpp b/tests/unit-tests/util/notification-stream.cpp
new file mode 100644
index 0000000..0378bd1
--- /dev/null
+++ b/tests/unit-tests/util/notification-stream.cpp
@@ -0,0 +1,90 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 "util/notification-stream.hpp"
+#include "simple-notification.hpp"
+
+#include "boost-test.hpp"
+#include "../dummy-client-face.hpp"
+
+namespace ndn {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(UtilNotificationStream)
+
+BOOST_AUTO_TEST_CASE(Post)
+{
+  shared_ptr<DummyClientFace> face = makeDummyClientFace();
+  ndn::KeyChain keyChain;
+  util::NotificationStream<SimpleNotification> 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 ndn
diff --git a/tests/unit-tests/util/notification-subscriber.cpp b/tests/unit-tests/util/notification-subscriber.cpp
new file mode 100644
index 0000000..1633ccf
--- /dev/null
+++ b/tests/unit-tests/util/notification-subscriber.cpp
@@ -0,0 +1,240 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 "util/notification-subscriber.hpp"
+#include "util/notification-stream.hpp"
+#include "simple-notification.hpp"
+
+#include "boost-test.hpp"
+#include "../dummy-client-face.hpp"
+
+namespace ndn {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber)
+
+class EndToEndFixture
+{
+public:
+  EndToEndFixture()
+    : streamPrefix("ndn:/NotificationSubscriberTest")
+    , publisherFace(makeDummyClientFace())
+    , notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
+    , subscriberFace(makeDummyClientFace())
+    , subscriber(*subscriberFace, streamPrefix, time::seconds(1))
+  {
+  }
+
+  /** \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 (first sent Interest)
+   */
+  bool
+  hasInitialRequest() const
+  {
+    if (subscriberFace->m_sentInterests.empty())
+      return 0;
+
+    const Interest& interest = subscriberFace->m_sentInterests[0];
+    return interest.getName() == streamPrefix &&
+           interest.getChildSelector() == 1 &&
+           interest.getMustBeFresh() &&
+           interest.getInterestLifetime() == subscriber.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() == subscriber.getInterestLifetime())
+      return name[-1].toSequenceNumber();
+    else
+      return 0;
+  }
+
+protected:
+  Name streamPrefix;
+  shared_ptr<DummyClientFace> publisherFace;
+  ndn::KeyChain publisherKeyChain;
+  util::NotificationStream<SimpleNotification> notificationStream;
+  shared_ptr<DummyClientFace> subscriberFace;
+  util::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(2 * subscriber.getInterestLifetime());
+  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 ndn
diff --git a/tests/unit-tests/util/simple-notification.hpp b/tests/unit-tests/util/simple-notification.hpp
new file mode 100644
index 0000000..2def4bb
--- /dev/null
+++ b/tests/unit-tests/util/simple-notification.hpp
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/**
+ * Original copyright notice from NFD:
+ *
+ * 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 NDN_UNIT_TESTS_UTIL_SIMPLE_NOTIFICATION_HPP
+#define NDN_UNIT_TESTS_UTIL_SIMPLE_NOTIFICATION_HPP
+
+#include "common.hpp"
+
+#include "encoding/encoding-buffer.hpp"
+#include "security/key-chain.hpp"
+
+namespace ndn {
+namespace tests {
+
+class SimpleNotification
+{
+public:
+  SimpleNotification()
+  {
+  }
+
+  explicit
+  SimpleNotification(const Block& block)
+  {
+    wireDecode(block);
+  }
+
+  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 ndn
+
+#endif // NDN_UNIT_TESTS_UTIL_CORE_SIMPLE_NOTIFICATION_HPP