docs: improve example applications

refs: #1976

Change-Id: I82e719ac497d3cb4eab74c1c6d02bcaa050388eb
diff --git a/docs/examples.rst b/docs/examples.rst
index e512c72..a609ad6 100644
--- a/docs/examples.rst
+++ b/docs/examples.rst
@@ -15,15 +15,10 @@
 ``/localhost/testApp/randomData``.  While expressing Interest, the app specifies two
 callbacks to be called when Data is retrieved or Interest times out.
 
-``ndn::bind`` is an alias for either `boost::bind
-<http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html>`_ or `std::bind
-<http://en.cppreference.com/w/cpp/utility/functional/bind>`_ when the library is compiled
-in C++11 mode.
-
 .. literalinclude:: ../examples/consumer.cpp
    :language: c++
    :linenos:
-   :emphasize-lines: 24-26,32-34,48-52,54
+   :emphasize-lines: 24-27,39,43-46,50,57,67
 
 
 Trivial producer
@@ -47,7 +42,7 @@
 .. literalinclude:: ../examples/producer.cpp
    :language: c++
    :linenos:
-   :emphasize-lines: 44-47,50,56,71-74
+   :emphasize-lines: 42,55-58,61,64,67,70,76
 
 
 Consumer that uses ndn::Scheduler
@@ -71,4 +66,5 @@
 .. literalinclude:: ../examples/consumer-with-timer.cpp
    :language: c++
    :linenos:
-   :emphasize-lines: 19,61,76,79-80,83
+   :emphasize-lines: 39-40,51-54,58-59,61-62,99-100
+
diff --git a/examples/consumer-with-timer.cpp b/examples/consumer-with-timer.cpp
index 7897b41..c08a312 100644
--- a/examples/consumer-with-timer.cpp
+++ b/examples/consumer-with-timer.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -32,74 +32,77 @@
 // Additional nested namespace could be used to prevent/limit name contentions
 namespace examples {
 
-void
-onData(Face& face,
-       const Interest& interest, Data& data)
+class ConsumerWithTimer : noncopyable
 {
-  std::cout << "I: " << interest.toUri() << std::endl;
-  std::cout << "D: " << data.getName().toUri() << std::endl;
-}
+public:
+  ConsumerWithTimer()
+    : m_face(m_ioService) // Create face with io_service object
+    , m_scheduler(m_ioService)
+  {
+  }
 
-void
-onTimeout(Face& face,
-          const Interest& interest)
-{
-  std::cout << "Timeout" << std::endl;
-}
+  void
+  run()
+  {
+    Interest interest(Name("/example/testApp/randomData"));
+    interest.setInterestLifetime(time::seconds(1));
+    interest.setMustBeFresh(true);
 
-void
-delayedInterest(Face& face)
-{
-  std::cout << "One more Interest, delayed by the scheduler" << std::endl;
+    m_face.expressInterest(interest,
+                           bind(&ConsumerWithTimer::onData, this, _1, _2),
+                           bind(&ConsumerWithTimer::onTimeout, this, _1));
 
-  Interest i(Name("/example/testApp/randomData"));
-  i.setScope(1);
-  i.setInterestLifetime(time::milliseconds(1000));
-  i.setMustBeFresh(true);
-
-  face.expressInterest(i,
-                       bind(&onData, ref(face), _1, _2),
-                       bind(&onTimeout, ref(face), _1));
-}
-
-int
-main(int argc, char** argv)
-{
-  try {
-    // Explicitly create io_service object, which can be shared between Face and Scheduler
-    boost::asio::io_service ioService;
-
-    Interest i(Name("/example/testApp/randomData"));
-    i.setScope(1);
-    i.setInterestLifetime(time::seconds(1));
-    i.setMustBeFresh(true);
-
-    // Create face with io_service object
-    Face face(ioService);
-    face.expressInterest(i,
-                         bind(&onData, ref(face), _1, _2),
-                         bind(&onTimeout, ref(face), _1));
-
-
-    // Create scheduler object
-    Scheduler scheduler(ioService);
+    std::cout << "Sending " << interest << std::endl;
 
     // Schedule a new event
-    scheduler.scheduleEvent(time::seconds(2),
-                            bind(&delayedInterest, ref(face)));
+    m_scheduler.scheduleEvent(time::seconds(2),
+                              bind(&ConsumerWithTimer::delayedInterest, this));
 
-    // ioService.run() will block until all events finished or ioService.stop() is called
-    ioService.run();
+    // m_ioService.run() will block until all events finished or m_ioService.stop() is called
+    m_ioService.run();
 
-    // Alternatively, a helper face.processEvents() also can be called
-    // processEvents will block until the requested data received or timeout occurs
-    // face.processEvents();
+    // Alternatively, m_face.processEvents() can also be called.
+    // processEvents will block until the requested data received or timeout occurs.
+    // m_face.processEvents();
   }
-  catch(std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+
+private:
+  void
+  onData(const Interest& interest, const Data& data)
+  {
+    std::cout << data << std::endl;
   }
-  return 0;
-}
+
+  void
+  onTimeout(const Interest& interest)
+  {
+    std::cout << "Timeout " << interest << std::endl;
+  }
+
+  void
+  delayedInterest()
+  {
+    std::cout << "One more Interest, delayed by the scheduler" << std::endl;
+
+    Interest interest(Name("/example/testApp/randomData"));
+    interest.setInterestLifetime(time::milliseconds(1000));
+    interest.setMustBeFresh(true);
+
+    m_face.expressInterest(interest,
+                           bind(&ConsumerWithTimer::onData, this, _1, _2),
+                           bind(&ConsumerWithTimer::onTimeout, this, _1));
+
+    std::cout << "Sending " << interest << std::endl;
+  }
+
+private:
+  // Explicitly create io_service object, which can be shared between Face and Scheduler
+  boost::asio::io_service m_ioService;
+  Face m_face;
+  Scheduler m_scheduler;
+};
+
+
 
 } // namespace examples
 } // namespace ndn
@@ -107,5 +110,12 @@
 int
 main(int argc, char** argv)
 {
-  return ndn::examples::main(argc, argv);
+  ndn::examples::ConsumerWithTimer consumer;
+  try {
+    consumer.run();
+  }
+  catch (const std::exception& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+  }
+  return 0;
 }
diff --git a/examples/consumer.cpp b/examples/consumer.cpp
index 6a76169..4042565 100644
--- a/examples/consumer.cpp
+++ b/examples/consumer.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -30,44 +30,42 @@
 // Additional nested namespace could be used to prevent/limit name contentions
 namespace examples {
 
-void
-onData(Face& face,
-       const Interest& interest, Data& data)
+class Consumer : noncopyable
 {
-  std::cout << "I: " << interest.toUri() << std::endl;
-  std::cout << "D: " << data.getName().toUri() << std::endl;
-}
+public:
+  void
+  run()
+  {
+    Interest interest(Name("/example/testApp/randomData"));
+    interest.setInterestLifetime(time::milliseconds(1000));
+    interest.setMustBeFresh(true);
 
-void
-onTimeout(Face& face,
-          const Interest& interest)
-{
-  std::cout << "Timeout" << std::endl;
-}
+    m_face.expressInterest(interest,
+                           bind(&Consumer::onData, this,  _1, _2),
+                           bind(&Consumer::onTimeout, this, _1));
 
-int
-main(int argc, char** argv)
-{
-  try {
-    Interest i(Name("/example/testApp/randomData"));
-    i.setScope(1);
-    i.setInterestLifetime(time::milliseconds(1000));
-    i.setMustBeFresh(true);
-
-    Face face;
-    face.expressInterest(i,
-                         bind(onData, ref(face), _1, _2),
-                         bind(onTimeout, ref(face), _1));
+    std::cout << "Sending " << interest << std::endl;
 
     // processEvents will block until the requested data received or timeout occurs
-    face.processEvents();
+    m_face.processEvents();
   }
-  catch(std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
-    return 1;
+
+private:
+  void
+  onData(const Interest& interest, const Data& data)
+  {
+    std::cout << data << std::endl;
   }
-  return 0;
-}
+
+  void
+  onTimeout(const Interest& interest)
+  {
+    std::cout << "Timeout " << interest << std::endl;
+  }
+
+private:
+  Face m_face;
+};
 
 } // namespace examples
 } // namespace ndn
@@ -75,5 +73,12 @@
 int
 main(int argc, char** argv)
 {
-  return ndn::examples::main(argc, argv);
+  ndn::examples::Consumer consumer;
+  try {
+    consumer.run();
+  }
+  catch (const std::exception& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+  }
+  return 0;
 }
diff --git a/examples/producer.cpp b/examples/producer.cpp
index 3b5ff07..81df7df 100644
--- a/examples/producer.cpp
+++ b/examples/producer.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -33,11 +33,22 @@
 // Additional nested namespace could be used to prevent/limit name contentions
 namespace examples {
 
-class Producer
+class Producer : noncopyable
 {
 public:
   void
-  onInterest(const Name& name, const Interest& interest)
+  run()
+  {
+    m_face.setInterestFilter("/example/testApp",
+                             bind(&Producer::onInterest, this, _1, _2),
+                             RegisterPrefixSuccessCallback(),
+                             bind(&Producer::onRegisterFailed, this, _1, _2));
+    m_face.processEvents();
+  }
+
+private:
+  void
+  onInterest(const InterestFilter& filter, const Interest& interest)
   {
     std::cout << "<< I: " << interest << std::endl;
 
@@ -50,40 +61,31 @@
     static const std::string content = "HELLO KITTY";
 
     // Create Data packet
-    Data data;
-    data.setName(dataName);
-    data.setFreshnessPeriod(time::seconds(10));
-    data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
+    shared_ptr<Data> data = make_shared<Data>();
+    data->setName(dataName);
+    data->setFreshnessPeriod(time::seconds(10));
+    data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
 
     // Sign Data packet with default identity
-    m_keyChain.sign(data);
+    m_keyChain.sign(*data);
     // m_keyChain.sign(data, <identityName>);
     // m_keyChain.sign(data, <certificate>);
 
     // Return Data packet to the requester
-    std::cout << ">> D: " << data << std::endl;
-    m_face.put(data);
+    std::cout << ">> D: " << *data << std::endl;
+    m_face.put(*data);
   }
 
 
   void
-  onRegisterFailed(const std::string& reason)
+  onRegisterFailed(const Name& prefix, const std::string& reason)
   {
-    std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
+    std::cerr << "ERROR: Failed to register prefix \""
+              << prefix << "\" in local hub's daemon (" << reason << ")"
               << std::endl;
     m_face.shutdown();
   }
 
-  void
-  run()
-  {
-    m_face.setInterestFilter("/example/testApp",
-                             bind(&Producer::onInterest, this, _1, _2),
-                             RegisterPrefixSuccessCallback(),
-                             bind(&Producer::onRegisterFailed, this, _2));
-    m_face.processEvents();
-  }
-
 private:
   Face m_face;
   KeyChain m_keyChain;
@@ -95,11 +97,11 @@
 int
 main(int argc, char** argv)
 {
+  ndn::examples::Producer producer;
   try {
-    ndn::examples::Producer producer;
     producer.run();
   }
-  catch (std::exception& e) {
+  catch (const std::exception& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;
   }
   return 0;