examples: fix "CanBePrefix unset" warning and general cleanup

refs: #4581
Change-Id: Ia1955e1c1eb746221adc88bfde093d5eadbd88e5
diff --git a/examples/consumer-with-timer.cpp b/examples/consumer-with-timer.cpp
index 04c6037..b3b35ee 100644
--- a/examples/consumer-with-timer.cpp
+++ b/examples/consumer-with-timer.cpp
@@ -29,7 +29,7 @@
 
 // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
 namespace ndn {
-// Additional nested namespaces can be used to prevent/limit name conflicts
+// Additional nested namespaces should be used to prevent/limit name conflicts
 namespace examples {
 
 class ConsumerWithTimer
@@ -44,17 +44,20 @@
   void
   run()
   {
-    Interest interest(Name("/example/testApp/randomData"));
-    interest.setInterestLifetime(2_s); // 2 seconds
-    interest.setMustBeFresh(true);
+    Name interestName("/example/testApp/randomData");
+    interestName.appendVersion();
 
+    Interest interest(interestName);
+    interest.setCanBePrefix(false);
+    interest.setMustBeFresh(true);
+    interest.setInterestLifetime(2_s);
+
+    std::cout << "Sending Interest " << interest << std::endl;
     m_face.expressInterest(interest,
                            bind(&ConsumerWithTimer::onData, this, _1, _2),
                            bind(&ConsumerWithTimer::onNack, this, _1, _2),
                            bind(&ConsumerWithTimer::onTimeout, this, _1));
 
-    std::cout << "Sending " << interest << std::endl;
-
     // Schedule a new event
     m_scheduler.schedule(3_s, [this] { delayedInterest(); });
 
@@ -68,22 +71,22 @@
 
 private:
   void
-  onData(const Interest& interest, const Data& data)
+  onData(const Interest&, const Data& data) const
   {
-    std::cout << data << std::endl;
+    std::cout << "Received Data " << data << std::endl;
   }
 
   void
-  onNack(const Interest& interest, const lp::Nack& nack)
+  onNack(const Interest& interest, const lp::Nack& nack) const
   {
-    std::cout << "received Nack with reason " << nack.getReason()
-              << " for interest " << interest << std::endl;
+    std::cout << "Received Nack with reason " << nack.getReason()
+              << " for " << interest << std::endl;
   }
 
   void
-  onTimeout(const Interest& interest)
+  onTimeout(const Interest& interest) const
   {
-    std::cout << "Timeout " << interest << std::endl;
+    std::cout << "Timeout for " << interest << std::endl;
   }
 
   void
@@ -91,20 +94,23 @@
   {
     std::cout << "One more Interest, delayed by the scheduler" << std::endl;
 
-    Interest interest(Name("/example/testApp/randomData"));
-    interest.setInterestLifetime(2_s); // 2 seconds
-    interest.setMustBeFresh(true);
+    Name interestName("/example/testApp/randomData");
+    interestName.appendVersion();
 
+    Interest interest(interestName);
+    interest.setCanBePrefix(false);
+    interest.setMustBeFresh(true);
+    interest.setInterestLifetime(2_s);
+
+    std::cout << "Sending Interest " << interest << std::endl;
     m_face.expressInterest(interest,
                            bind(&ConsumerWithTimer::onData, this, _1, _2),
                            bind(&ConsumerWithTimer::onNack, 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
+  // Explicitly create io_service object, which will be shared between Face and Scheduler
   boost::asio::io_service m_ioService;
   Face m_face;
   Scheduler m_scheduler;
@@ -116,12 +122,13 @@
 int
 main(int argc, char** argv)
 {
-  ndn::examples::ConsumerWithTimer consumer;
   try {
+    ndn::examples::ConsumerWithTimer consumer;
     consumer.run();
+    return 0;
   }
   catch (const std::exception& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
   }
-  return 0;
 }
diff --git a/examples/consumer.cpp b/examples/consumer.cpp
index f06ac07..ef6cd76 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-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -27,48 +27,50 @@
 
 // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
 namespace ndn {
-// Additional nested namespaces can be used to prevent/limit name conflicts
+// Additional nested namespaces should be used to prevent/limit name conflicts
 namespace examples {
 
-class Consumer : noncopyable
+class Consumer
 {
 public:
   void
   run()
   {
-    Interest interest(Name("/example/testApp/randomData"));
-    interest.setInterestLifetime(2_s); // 2 seconds
-    interest.setMustBeFresh(true);
+    Name interestName("/example/testApp/randomData");
+    interestName.appendVersion();
 
+    Interest interest(interestName);
+    interest.setCanBePrefix(false);
+    interest.setMustBeFresh(true);
+    interest.setInterestLifetime(6_s); // The default is 4 seconds
+
+    std::cout << "Sending Interest " << interest << std::endl;
     m_face.expressInterest(interest,
                            bind(&Consumer::onData, this,  _1, _2),
                            bind(&Consumer::onNack, this, _1, _2),
                            bind(&Consumer::onTimeout, this, _1));
 
-    std::cout << "Sending " << interest << std::endl;
-
-    // processEvents will block until the requested data received or timeout occurs
+    // processEvents will block until the requested data is received or a timeout occurs
     m_face.processEvents();
   }
 
 private:
   void
-  onData(const Interest& interest, const Data& data)
+  onData(const Interest&, const Data& data) const
   {
-    std::cout << data << std::endl;
+    std::cout << "Received Data " << data << std::endl;
   }
 
   void
-  onNack(const Interest& interest, const lp::Nack& nack)
+  onNack(const Interest&, const lp::Nack& nack) const
   {
-    std::cout << "received Nack with reason " << nack.getReason()
-              << " for interest " << interest << std::endl;
+    std::cout << "Received Nack with reason " << nack.getReason() << std::endl;
   }
 
   void
-  onTimeout(const Interest& interest)
+  onTimeout(const Interest& interest) const
   {
-    std::cout << "Timeout " << interest << std::endl;
+    std::cout << "Timeout for " << interest << std::endl;
   }
 
 private:
@@ -81,12 +83,13 @@
 int
 main(int argc, char** argv)
 {
-  ndn::examples::Consumer consumer;
   try {
+    ndn::examples::Consumer consumer;
     consumer.run();
+    return 0;
   }
   catch (const std::exception& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
   }
-  return 0;
 }
diff --git a/examples/producer.cpp b/examples/producer.cpp
index 4544ec0..516e1c0 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-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -23,15 +23,16 @@
 
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
 
 #include <iostream>
 
 // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
 namespace ndn {
-// Additional nested namespaces can be used to prevent/limit name conflicts
+// Additional nested namespaces should be used to prevent/limit name conflicts
 namespace examples {
 
-class Producer : noncopyable
+class Producer
 {
 public:
   void
@@ -39,48 +40,41 @@
   {
     m_face.setInterestFilter("/example/testApp",
                              bind(&Producer::onInterest, this, _1, _2),
-                             RegisterPrefixSuccessCallback(),
+                             nullptr, // RegisterPrefixSuccessCallback is optional
                              bind(&Producer::onRegisterFailed, this, _1, _2));
     m_face.processEvents();
   }
 
 private:
   void
-  onInterest(const InterestFilter& filter, const Interest& interest)
+  onInterest(const InterestFilter&, const Interest& interest)
   {
-    std::cout << "<< I: " << interest << std::endl;
+    std::cout << ">> I: " << interest << std::endl;
 
-    // Create new name, based on Interest's name
-    Name dataName(interest.getName());
-    dataName
-      .append("testApp") // add "testApp" component to Interest name
-      .appendVersion();  // add "version" component (current UNIX timestamp in milliseconds)
-
-    static const std::string content = "HELLO KITTY";
+    static const std::string content("Hello, world!");
 
     // Create Data packet
-    shared_ptr<Data> data = make_shared<Data>();
-    data->setName(dataName);
-    data->setFreshnessPeriod(10_s); // 10 seconds
+    auto data = make_shared<Data>(interest.getName());
+    data->setFreshnessPeriod(10_s);
     data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
 
     // Sign Data packet with default identity
     m_keyChain.sign(*data);
-    // m_keyChain.sign(data, <identityName>);
-    // m_keyChain.sign(data, <certificate>);
+    // m_keyChain.sign(*data, signingByIdentity(<identityName>));
+    // m_keyChain.sign(*data, signingByKey(<keyName>));
+    // m_keyChain.sign(*data, signingByCertificate(<certName>));
+    // m_keyChain.sign(*data, signingWithSha256());
 
     // Return Data packet to the requester
-    std::cout << ">> D: " << *data << std::endl;
+    std::cout << "<< D: " << *data << std::endl;
     m_face.put(*data);
   }
 
-
   void
   onRegisterFailed(const Name& prefix, const std::string& reason)
   {
-    std::cerr << "ERROR: Failed to register prefix \""
-              << prefix << "\" in local hub's daemon (" << reason << ")"
-              << std::endl;
+    std::cerr << "ERROR: Failed to register prefix '" << prefix
+              << "' with the local forwarder (" << reason << ")" << std::endl;
     m_face.shutdown();
   }
 
@@ -95,12 +89,13 @@
 int
 main(int argc, char** argv)
 {
-  ndn::examples::Producer producer;
   try {
+    ndn::examples::Producer producer;
     producer.run();
+    return 0;
   }
   catch (const std::exception& e) {
     std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
   }
-  return 0;
 }