catchunks: remove modular version discovery

refs: #4832, #4556
Change-Id: I99b4f42d1457c74321c9b6ae84f793a93c4c1436
diff --git a/tools/chunks/catchunks/discover-version.cpp b/tools/chunks/catchunks/discover-version.cpp
index 3a4309c..7ac5195 100644
--- a/tools/chunks/catchunks/discover-version.cpp
+++ b/tools/chunks/catchunks/discover-version.cpp
@@ -23,42 +23,73 @@
  * @author Wentao Shang
  * @author Steve DiBenedetto
  * @author Andrea Tosatto
+ * @author Chavoosh Ghasemi
  */
 
 #include "discover-version.hpp"
 #include "data-fetcher.hpp"
 
+#include <ndn-cxx/metadata-object.hpp>
+
 namespace ndn {
 namespace chunks {
 
-DiscoverVersion::DiscoverVersion(const Name& prefix, Face& face)
-  : m_prefix(prefix)
+DiscoverVersion::DiscoverVersion(const Name& prefix, Face& face, const Options& options)
+  : chunks::Options(options)
+  , m_prefix(prefix)
   , m_face(face)
 {
 }
 
-DiscoverVersion::~DiscoverVersion() = default;
-
 void
-DiscoverVersion::expressInterest(const Interest& interest, int maxRetriesNack, int maxRetriesTimeout)
+DiscoverVersion::run()
 {
-  fetcher = DataFetcher::fetch(m_face, interest, maxRetriesNack, maxRetriesTimeout,
-                               bind(&DiscoverVersion::handleData, this, _1, _2),
-                               bind(&DiscoverVersion::handleNack, this, _1, _2),
-                               bind(&DiscoverVersion::handleTimeout, this, _1, _2),
-                               isVerbose);
+  if (!m_prefix.empty() && m_prefix[-1].isVersion()) {
+    onDiscoverySuccess(m_prefix);
+    return;
+  }
+
+  Interest interest = MetadataObject::makeDiscoveryInterest(m_prefix)
+                                      .setInterestLifetime(interestLifetime);
+
+  m_fetcher = DataFetcher::fetch(m_face, interest,
+                                 maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack,
+                                 bind(&DiscoverVersion::handleData, this, _1, _2),
+                                 [this] (const Interest& interest, const std::string& reason) {
+                                   onDiscoveryFailure(reason);
+                                 },
+                                 [this] (const Interest& interest, const std::string& reason) {
+                                   onDiscoveryFailure(reason);
+                                 },
+                                 isVerbose);
 }
 
 void
-DiscoverVersion::handleNack(const Interest& interest, const std::string& reason)
+DiscoverVersion::handleData(const Interest& interest, const Data& data)
 {
-  onDiscoveryFailure(reason);
-}
+  if (isVerbose)
+    std::cerr << "Data: " << data << std::endl;
 
-void
-DiscoverVersion::handleTimeout(const Interest& interest, const std::string& reason)
-{
-  onDiscoveryFailure(reason);
+  // make a metadata object from received metadata packet
+  MetadataObject mobject;
+  try {
+    mobject = MetadataObject(data);
+  }
+  catch (const tlv::Error& e) {
+    onDiscoveryFailure("Invalid metadata packet: "s + e.what());
+    return;
+  }
+
+  if (mobject.getVersionedName().empty() || !mobject.getVersionedName()[-1].isVersion()) {
+    onDiscoveryFailure(mobject.getVersionedName().toUri() + " is not a valid versioned name");
+    return;
+  }
+
+  if (isVerbose) {
+    std::cerr << "Discovered Data version: " << mobject.getVersionedName()[-1] << std::endl;
+  }
+
+  onDiscoverySuccess(mobject.getVersionedName());
 }
 
 } // namespace chunks