chunks: switch to v2::ValidatorNull

refs #4089

Change-Id: If23ca4d202ad1b61ee1e56ba0e15a8750c184e03
diff --git a/tests/chunks/consumer.t.cpp b/tests/chunks/consumer.t.cpp
index 158148a..8f7097e 100644
--- a/tests/chunks/consumer.t.cpp
+++ b/tests/chunks/consumer.t.cpp
@@ -1,8 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2016,  Regents of the University of California,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University.
+ * Copyright (c) 2016-2017, Regents of the University of California,
+ *                          Colorado State University,
+ *                          University Pierre & Marie Curie, Sorbonne University.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -28,10 +28,10 @@
 #include "tools/chunks/catchunks/pipeline-interests.hpp"
 
 #include "tests/test-common.hpp"
-#include <ndn-cxx/util/dummy-client-face.hpp>
-#include <ndn-cxx/security/validator-null.hpp>
 
 #include <boost/test/output_test_stream.hpp>
+#include <ndn-cxx/security/validator-null.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
 
 namespace ndn {
 namespace chunks {
@@ -65,9 +65,8 @@
   };
 
   util::DummyClientFace face;
-  ValidatorNull validator;
   output_test_stream output("");
-  Consumer cons(validator, false, output);
+  Consumer cons(security::v2::getAcceptAllValidator(), false, output);
 
   auto interest = makeInterest(name);
 
@@ -105,9 +104,8 @@
   };;
 
   util::DummyClientFace face;
-  ValidatorNull validator;
   output_test_stream output("");
-  Consumer cons(validator, false, output);
+  Consumer cons(security::v2::getAcceptAllValidator(), false, output);
 
   auto interest = makeInterest(name);
   std::vector<shared_ptr<Data>> dataStore;
@@ -198,8 +196,7 @@
 {
   boost::asio::io_service io;
   util::DummyClientFace face(io);
-  ValidatorNull validator;
-  Consumer consumer(validator, false);
+  Consumer consumer(security::v2::getAcceptAllValidator(), false);
 
   Name prefix("/ndn/chunks/test");
   auto discover = make_unique<DiscoverVersionDummy>(prefix, face, Options());
diff --git a/tools/chunks/catchunks/consumer.cpp b/tools/chunks/catchunks/consumer.cpp
index 306cc16..431f058 100644
--- a/tools/chunks/catchunks/consumer.cpp
+++ b/tools/chunks/catchunks/consumer.cpp
@@ -30,7 +30,7 @@
 namespace ndn {
 namespace chunks {
 
-Consumer::Consumer(Validator& validator, bool isVerbose, std::ostream& os)
+Consumer::Consumer(security::v2::Validator& validator, bool isVerbose, std::ostream& os)
   : m_validator(validator)
   , m_outputStream(os)
   , m_nextToPrint(0)
@@ -47,49 +47,44 @@
   m_bufferedData.clear();
 
   m_discover->onDiscoverySuccess.connect(bind(&Consumer::startPipeline, this, _1));
-  m_discover->onDiscoveryFailure.connect(bind(&Consumer::onFailure, this, _1));
+  m_discover->onDiscoveryFailure.connect([] (const std::string& msg) {
+    BOOST_THROW_EXCEPTION(std::runtime_error(msg));
+  });
   m_discover->run();
 }
 
 void
 Consumer::startPipeline(const Data& data)
 {
-  m_validator.validate(data,
-                       bind(&Consumer::onDataValidated, this, _1),
-                       bind(&Consumer::onFailure, this, _2));
+  this->handleData(data);
 
   m_pipeline->run(data,
-                  bind(&Consumer::onData, this, _1, _2),
-                  bind(&Consumer::onFailure, this, _1));
+    [this] (const Interest&, const Data& data) { this->handleData(data); },
+    [] (const std::string& msg) { BOOST_THROW_EXCEPTION(std::runtime_error(msg)); });
 }
 
 void
-Consumer::onData(const Interest& interest, const Data& data)
+Consumer::handleData(const Data& data)
 {
+  auto dataPtr = data.shared_from_this();
+
   m_validator.validate(data,
-                       bind(&Consumer::onDataValidated, this, _1),
-                       bind(&Consumer::onFailure, this, _2));
-}
+    [this, dataPtr] (const Data& data) {
+      if (data.getContentType() == ndn::tlv::ContentType_Nack) {
+        if (m_isVerbose) {
+          std::cerr << "Application level NACK: " << data << std::endl;
+        }
+        m_pipeline->cancel();
+        BOOST_THROW_EXCEPTION(ApplicationNackError(data));
+      }
 
-void
-Consumer::onDataValidated(shared_ptr<const Data> data)
-{
-  if (data->getContentType() == ndn::tlv::ContentType_Nack) {
-    if (m_isVerbose)
-      std::cerr << "Application level NACK: " << *data << std::endl;
-
-    m_pipeline->cancel();
-    throw ApplicationNackError(*data);
-  }
-
-  m_bufferedData[getSegmentFromPacket(*data)] = data;
-  writeInOrderData();
-}
-
-void
-Consumer::onFailure(const std::string& reason)
-{
-  throw std::runtime_error(reason);
+      // 'data' passed to callback comes from DataValidationState and was not created with make_shared
+      m_bufferedData[getSegmentFromPacket(data)] = dataPtr;
+      writeInOrderData();
+    },
+    [this] (const Data& data, const security::v2::ValidationError& error) {
+      BOOST_THROW_EXCEPTION(DataValidationError(error));
+    });
 }
 
 void
diff --git a/tools/chunks/catchunks/consumer.hpp b/tools/chunks/catchunks/consumer.hpp
index 9996c14..3dcefe6 100644
--- a/tools/chunks/catchunks/consumer.hpp
+++ b/tools/chunks/catchunks/consumer.hpp
@@ -1,8 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2016,  Regents of the University of California,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University.
+ * Copyright (c) 2016-2017, Regents of the University of California,
+ *                          Colorado State University,
+ *                          University Pierre & Marie Curie, Sorbonne University.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -31,7 +31,8 @@
 #include "discover-version.hpp"
 #include "pipeline-interests.hpp"
 
-#include <ndn-cxx/security/validator.hpp>
+#include <ndn-cxx/security/v2/validation-error.hpp>
+#include <ndn-cxx/security/v2/validator.hpp>
 
 namespace ndn {
 namespace chunks {
@@ -56,10 +57,20 @@
     }
   };
 
+  class DataValidationError : public std::runtime_error
+  {
+  public:
+    explicit
+    DataValidationError(const security::v2::ValidationError& error)
+      : std::runtime_error(boost::lexical_cast<std::string>(error))
+    {
+    }
+  };
+
   /**
    * @brief Create the consumer
    */
-  Consumer(Validator& validator, bool isVerbose, std::ostream& os = std::cout);
+  Consumer(security::v2::Validator& validator, bool isVerbose, std::ostream& os = std::cout);
 
   /**
    * @brief Run the consumer
@@ -72,20 +83,14 @@
   startPipeline(const Data& data);
 
   void
-  onData(const Interest& interest, const Data& data);
-
-  void
-  onDataValidated(shared_ptr<const Data> data);
-
-  void
-  onFailure(const std::string& reason);
+  handleData(const Data& data);
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   void
   writeInOrderData();
 
 private:
-  Validator& m_validator;
+  security::v2::Validator& m_validator;
   std::ostream& m_outputStream;
   unique_ptr<DiscoverVersion> m_discover;
   unique_ptr<PipelineInterests> m_pipeline;
diff --git a/tools/chunks/catchunks/ndncatchunks.cpp b/tools/chunks/catchunks/ndncatchunks.cpp
index 38cb23b..9d9b8a8 100644
--- a/tools/chunks/catchunks/ndncatchunks.cpp
+++ b/tools/chunks/catchunks/ndncatchunks.cpp
@@ -27,18 +27,18 @@
  * @author Weiwei Liu
  */
 
-#include "core/version.hpp"
-#include "options.hpp"
+#include "aimd-statistics-collector.hpp"
+#include "aimd-rtt-estimator.hpp"
 #include "consumer.hpp"
 #include "discover-version-fixed.hpp"
 #include "discover-version-iterative.hpp"
-#include "pipeline-interests-fixed-window.hpp"
 #include "pipeline-interests-aimd.hpp"
-#include "aimd-rtt-estimator.hpp"
-#include "aimd-statistics-collector.hpp"
+#include "pipeline-interests-fixed-window.hpp"
+#include "options.hpp"
+#include "core/version.hpp"
 
-#include <ndn-cxx/security/validator-null.hpp>
 #include <fstream>
+#include <ndn-cxx/security/validator-null.hpp>
 
 namespace ndn {
 namespace chunks {
@@ -267,8 +267,7 @@
       return 2;
     }
 
-    ValidatorNull validator;
-    Consumer consumer(validator, options.isVerbose);
+    Consumer consumer(security::v2::getAcceptAllValidator(), options.isVerbose);
 
     BOOST_ASSERT(discover != nullptr);
     BOOST_ASSERT(pipeline != nullptr);