ping+peek: change namespace and class names

refs #2525

Change-Id: I772ad90cdbaee8569be3ed96a5951f3cb89d018c
diff --git a/tools/peek/ndn-peek.cpp b/tools/peek/ndn-peek.cpp
index 5460096..4caba0a 100644
--- a/tools/peek/ndn-peek.cpp
+++ b/tools/peek/ndn-peek.cpp
@@ -31,13 +31,14 @@
 
 #include <ndn-cxx/face.hpp>
 
-namespace ndntlvpeek {
+namespace ndn {
+namespace peek {
 
-class NdnTlvPeek : boost::noncopyable
+class NdnPeek : boost::noncopyable
 {
 public:
   explicit
-  NdnTlvPeek(char* programName)
+  NdnPeek(char* programName)
     : m_programName(programName)
     , m_mustBeFresh(false)
     , m_isChildSelectorRightmost(false)
@@ -87,6 +88,7 @@
   {
     if (minSuffixComponents < 0)
       usage();
+
     m_minSuffixComponents = minSuffixComponents;
   }
 
@@ -95,6 +97,7 @@
   {
     if (maxSuffixComponents < 0)
       usage();
+
     m_maxSuffixComponents = maxSuffixComponents;
   }
 
@@ -103,7 +106,8 @@
   {
     if (interestLifetime < 0)
       usage();
-    m_interestLifetime = ndn::time::milliseconds(interestLifetime);
+
+    m_interestLifetime = time::milliseconds(interestLifetime);
   }
 
   void
@@ -117,87 +121,90 @@
   {
     if (timeout < 0)
       usage();
-    m_timeout = ndn::time::milliseconds(timeout);
+
+    m_timeout = time::milliseconds(timeout);
   }
 
   void
   setPrefixName(char* prefixName)
   {
     m_prefixName = prefixName;
+
     if (m_prefixName.length() == 0)
       usage();
   }
 
-  ndn::time::milliseconds
+  time::milliseconds
   getDefaultInterestLifetime()
   {
-    return ndn::time::seconds(4);
+    return time::seconds(4);
   }
 
-  ndn::Interest
+  Interest
   createInterestPacket()
   {
-    ndn::Name interestName(m_prefixName);
-    ndn::Interest interestPacket(interestName);
+    Name interestName(m_prefixName);
+    Interest interestPacket(interestName);
+
     if (m_mustBeFresh)
       interestPacket.setMustBeFresh(true);
+
     if (m_isChildSelectorRightmost)
       interestPacket.setChildSelector(1);
+
     if (m_minSuffixComponents >= 0)
       interestPacket.setMinSuffixComponents(m_minSuffixComponents);
+
     if (m_maxSuffixComponents >= 0)
       interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
-    if (m_interestLifetime < ndn::time::milliseconds::zero())
+
+    if (m_interestLifetime < time::milliseconds::zero())
       interestPacket.setInterestLifetime(getDefaultInterestLifetime());
     else
       interestPacket.setInterestLifetime(m_interestLifetime);
+
     return interestPacket;
   }
 
   void
-  onData(const ndn::Interest& interest, ndn::Data& data)
+  onData(const Interest& interest, Data& data)
   {
     m_isDataReceived = true;
-    if (m_isPayloadOnlySet)
-      {
-        const ndn::Block& block = data.getContent();
-        std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
-      }
-    else
-      {
-        const ndn::Block& block = data.wireEncode();
-        std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
-      }
+    if (m_isPayloadOnlySet) {
+      const Block& block = data.getContent();
+      std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
+    }
+    else {
+      const Block& block = data.wireEncode();
+      std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
+    }
   }
 
   void
-  onTimeout(const ndn::Interest& interest)
+  onTimeout(const Interest& interest)
   {
   }
 
   void
   run()
   {
-    try
-      {
-        m_face.expressInterest(createInterestPacket(),
-                               bind(&NdnTlvPeek::onData, this, _1, _2),
-                               bind(&NdnTlvPeek::onTimeout, this, _1));
-        if (m_timeout < ndn::time::milliseconds::zero())
-          {
-            if (m_interestLifetime < ndn::time::milliseconds::zero())
-              m_face.processEvents(getDefaultInterestLifetime());
-            else
-              m_face.processEvents(m_interestLifetime);
-          }
+    try {
+      m_face.expressInterest(createInterestPacket(),
+                             bind(&NdnPeek::onData, this, _1, _2),
+                             bind(&NdnPeek::onTimeout, this, _1));
+      if (m_timeout < time::milliseconds::zero()) {
+        if (m_interestLifetime < time::milliseconds::zero())
+          m_face.processEvents(getDefaultInterestLifetime());
         else
-          m_face.processEvents(m_timeout);
+          m_face.processEvents(m_interestLifetime);
       }
-    catch (std::exception& e)
-      {
-        std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
-        exit(1);
-      }
+      else
+        m_face.processEvents(m_timeout);
+    }
+    catch (std::exception& e) {
+      std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
+      exit(1);
+    }
   }
 
   bool
@@ -207,58 +214,55 @@
   }
 
 private:
-
   std::string m_programName;
   bool m_mustBeFresh;
   bool m_isChildSelectorRightmost;
   int m_minSuffixComponents;
   int m_maxSuffixComponents;
-  ndn::time::milliseconds m_interestLifetime;
+  time::milliseconds m_interestLifetime;
   bool m_isPayloadOnlySet;
-  ndn::time::milliseconds m_timeout;
+  time::milliseconds m_timeout;
   std::string m_prefixName;
   bool m_isDataReceived;
-  ndn::Face m_face;
+  Face m_face;
 };
 
-}
-
 int
 main(int argc, char* argv[])
 {
-  ndntlvpeek::NdnTlvPeek ndnTlvPeek(argv[0]);
+  NdnPeek program(argv[0]);
   int option;
   while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
     switch (option) {
     case 'h':
-      ndnTlvPeek.usage();
+      program.usage();
       break;
     case 'f':
-      ndnTlvPeek.setMustBeFresh();
+      program.setMustBeFresh();
       break;
     case 'r':
-      ndnTlvPeek.setRightmostChildSelector();
+      program.setRightmostChildSelector();
       break;
     case 'm':
-      ndnTlvPeek.setMinSuffixComponents(atoi(optarg));
+      program.setMinSuffixComponents(atoi(optarg));
       break;
     case 'M':
-      ndnTlvPeek.setMaxSuffixComponents(atoi(optarg));
+      program.setMaxSuffixComponents(atoi(optarg));
       break;
     case 'l':
-      ndnTlvPeek.setInterestLifetime(atoi(optarg));
+      program.setInterestLifetime(atoi(optarg));
       break;
     case 'p':
-      ndnTlvPeek.setPayloadOnly();
+      program.setPayloadOnly();
       break;
     case 'w':
-      ndnTlvPeek.setTimeout(atoi(optarg));
+      program.setTimeout(atoi(optarg));
       break;
     case 'V':
       std::cout << NFD_VERSION_BUILD_STRING << std::endl;
       return 0;
     default:
-      ndnTlvPeek.usage();
+      program.usage();
       break;
     }
   }
@@ -267,13 +271,22 @@
   argv += optind;
 
   if (argv[0] == 0)
-    ndnTlvPeek.usage();
+    program.usage();
 
-  ndnTlvPeek.setPrefixName(argv[0]);
-  ndnTlvPeek.run();
+  program.setPrefixName(argv[0]);
+  program.run();
 
-  if (ndnTlvPeek.isDataReceived())
+  if (program.isDataReceived())
     return 0;
   else
     return 1;
 }
+
+} // namespace peek
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+  return ndn::peek::main(argc, argv);
+}
diff --git a/tools/peek/ndn-poke.cpp b/tools/peek/ndn-poke.cpp
index 45db702..a4a635d 100644
--- a/tools/peek/ndn-poke.cpp
+++ b/tools/peek/ndn-poke.cpp
@@ -32,13 +32,14 @@
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 
-namespace ndntlvpoke {
+namespace ndn {
+namespace peek {
 
-class NdnTlvPoke : boost::noncopyable
+class NdnPoke : boost::noncopyable
 {
 public:
   explicit
-  NdnTlvPoke(char* programName)
+  NdnPoke(char* programName)
     : m_programName(programName)
     , m_isForceDataSet(false)
     , m_isUseDigestSha256Set(false)
@@ -84,7 +85,7 @@
   void
   setIdentityName(char* identityName)
   {
-    m_identityName = ndn::make_shared<ndn::Name>(identityName);
+    m_identityName = make_shared<Name>(identityName);
   }
 
   void
@@ -98,7 +99,8 @@
   {
     if (freshnessPeriod < 0)
       usage();
-    m_freshnessPeriod = ndn::time::milliseconds(freshnessPeriod);
+
+    m_freshnessPeriod = time::milliseconds(freshnessPeriod);
   }
 
   void
@@ -106,57 +108,60 @@
   {
     if (timeout < 0)
       usage();
-    m_timeout = ndn::time::milliseconds(timeout);
+
+    m_timeout = time::milliseconds(timeout);
   }
 
   void
   setPrefixName(char* prefixName)
   {
-    m_prefixName = ndn::Name(prefixName);
+    m_prefixName = Name(prefixName);
   }
 
-  ndn::time::milliseconds
+  time::milliseconds
   getDefaultTimeout()
   {
-    return ndn::time::seconds(10);
+    return time::seconds(10);
   }
 
-  ndn::Data
+  Data
   createDataPacket()
   {
-    ndn::Data dataPacket(m_prefixName);
+    Data dataPacket(m_prefixName);
+
     std::stringstream payloadStream;
     payloadStream << std::cin.rdbuf();
     std::string payload = payloadStream.str();
     dataPacket.setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length());
-    if (m_freshnessPeriod >= ndn::time::milliseconds::zero())
+
+    if (m_freshnessPeriod >= time::milliseconds::zero())
       dataPacket.setFreshnessPeriod(m_freshnessPeriod);
-    if (m_isLastAsFinalBlockIdSet)
-      {
-        if (!m_prefixName.empty())
-          dataPacket.setFinalBlockId(m_prefixName.get(-1));
-        else
-          {
-            std::cerr << "Name Provided Has 0 Components" << std::endl;
-            exit(1);
-          }
+
+    if (m_isLastAsFinalBlockIdSet) {
+      if (!m_prefixName.empty())
+        dataPacket.setFinalBlockId(m_prefixName.get(-1));
+      else {
+        std::cerr << "Name Provided Has 0 Components" << std::endl;
+        exit(1);
       }
+    }
+
     if (m_isUseDigestSha256Set)
       m_keyChain.signWithSha256(dataPacket);
-    else
-      {
-        if (!static_cast<bool>(m_identityName))
-          m_keyChain.sign(dataPacket);
-        else
-          m_keyChain.signByIdentity(dataPacket, *m_identityName);
-      }
+    else {
+      if (m_identityName == nullptr)
+        m_keyChain.sign(dataPacket);
+      else
+        m_keyChain.signByIdentity(dataPacket, *m_identityName);
+    }
+
     return dataPacket;
   }
 
   void
-  onInterest(const ndn::Name& name,
-             const ndn::Interest& interest,
-             const ndn::Data& dataPacket)
+  onInterest(const Name& name,
+             const Interest& interest,
+             const Data& dataPacket)
   {
     m_face.put(dataPacket);
     m_isDataSent = true;
@@ -164,7 +169,7 @@
   }
 
   void
-  onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
+  onRegisterFailed(const Name& prefix, const std::string& reason)
   {
     std::cerr << "Prefix Registration Failure." << std::endl;
     std::cerr << "Reason = " << reason << std::endl;
@@ -173,31 +178,28 @@
   void
   run()
   {
-    try
-      {
-        ndn::Data dataPacket = createDataPacket();
-        if (m_isForceDataSet)
-          {
-            m_face.put(dataPacket);
-            m_isDataSent = true;
-          }
-        else
-          {
-            m_face.setInterestFilter(m_prefixName,
-                                     bind(&NdnTlvPoke::onInterest, this, _1, _2, dataPacket),
-                                     ndn::RegisterPrefixSuccessCallback(),
-                                     bind(&NdnTlvPoke::onRegisterFailed, this, _1, _2));
-          }
-        if (m_timeout < ndn::time::milliseconds::zero())
-          m_face.processEvents(getDefaultTimeout());
-        else
-          m_face.processEvents(m_timeout);
+    try {
+      Data dataPacket = createDataPacket();
+      if (m_isForceDataSet) {
+        m_face.put(dataPacket);
+        m_isDataSent = true;
       }
-    catch (std::exception& e)
-      {
-        std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
-        exit(1);
+      else {
+        m_face.setInterestFilter(m_prefixName,
+                                 bind(&NdnPoke::onInterest, this, _1, _2, dataPacket),
+                                 RegisterPrefixSuccessCallback(),
+                                 bind(&NdnPoke::onRegisterFailed, this, _1, _2));
       }
+
+      if (m_timeout < time::milliseconds::zero())
+        m_face.processEvents(getDefaultTimeout());
+      else
+        m_face.processEvents(m_timeout);
+    }
+    catch (std::exception& e) {
+      std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
+      exit(1);
+    }
   }
 
   bool
@@ -207,56 +209,52 @@
   }
 
 private:
-
-  ndn::KeyChain m_keyChain;
+  KeyChain m_keyChain;
   std::string m_programName;
   bool m_isForceDataSet;
   bool m_isUseDigestSha256Set;
-  ndn::shared_ptr<ndn::Name> m_identityName;
+  shared_ptr<Name> m_identityName;
   bool m_isLastAsFinalBlockIdSet;
-  ndn::time::milliseconds m_freshnessPeriod;
-  ndn::time::milliseconds m_timeout;
-  ndn::Name m_prefixName;
+  time::milliseconds m_freshnessPeriod;
+  time::milliseconds m_timeout;
+  Name m_prefixName;
   bool m_isDataSent;
-  ndn::Face m_face;
-
+  Face m_face;
 };
 
-}
-
 int
 main(int argc, char* argv[])
 {
   int option;
-  ndntlvpoke::NdnTlvPoke ndnTlvPoke(argv[0]);
+  NdnPoke program(argv[0]);
   while ((option = getopt(argc, argv, "hfDi:Fx:w:V")) != -1) {
     switch (option) {
     case 'h':
-      ndnTlvPoke.usage();
+      program.usage();
       break;
     case 'f':
-      ndnTlvPoke.setForceData();
+      program.setForceData();
       break;
     case 'D':
-      ndnTlvPoke.setUseDigestSha256();
+      program.setUseDigestSha256();
       break;
     case 'i':
-      ndnTlvPoke.setIdentityName(optarg);
+      program.setIdentityName(optarg);
       break;
     case 'F':
-      ndnTlvPoke.setLastAsFinalBlockId();
+      program.setLastAsFinalBlockId();
       break;
     case 'x':
-      ndnTlvPoke.setFreshnessPeriod(atoi(optarg));
+      program.setFreshnessPeriod(atoi(optarg));
       break;
     case 'w':
-      ndnTlvPoke.setTimeout(atoi(optarg));
+      program.setTimeout(atoi(optarg));
       break;
     case 'V':
       std::cout << NFD_VERSION_BUILD_STRING << std::endl;
       return 0;
     default:
-      ndnTlvPoke.usage();
+      program.usage();
       break;
     }
   }
@@ -265,13 +263,22 @@
   argv += optind;
 
   if (argv[0] == 0)
-    ndnTlvPoke.usage();
+    program.usage();
 
-  ndnTlvPoke.setPrefixName(argv[0]);
-  ndnTlvPoke.run();
+  program.setPrefixName(argv[0]);
+  program.run();
 
-  if (ndnTlvPoke.isDataSent())
+  if (program.isDataSent())
     return 0;
   else
     return 1;
 }
+
+} // namespace peek
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+  return ndn::peek::main(argc, argv);
+}