/*
 * Producer for CongestionMark test (test_congestionmark)
 *
 * Receives Interests for a specified prefix and returns a Data packet with the same congestion mark
 * as the corresponding Interest.
 *
 * Author: Eric Newberry <enewberry@cs.arizona.edu>
 *
 * Based on ndn-cxx example producer
 */

#include <ndn-cxx/face.hpp>
#include <ndn-cxx/interest.hpp>
#include <ndn-cxx/interest-filter.hpp>
#include <ndn-cxx/lp/tags.hpp>
#include <ndn-cxx/security/key-chain.hpp>

using namespace ndn;

class TestCongestionMarkProducer
{
public:
  void
  run(std::string prefix)
  {
    m_face.setInterestFilter(prefix,
                             bind(&TestCongestionMarkProducer::onInterest, this, _1, _2),
                             RegisterPrefixSuccessCallback(),
                             bind(&TestCongestionMarkProducer::onRegisterFailed, this, _1, _2));
    m_face.processEvents();
  }

private:
  void
  onInterest(const InterestFilter& filter, const Interest& i)
  {
    static const std::string content = "0123456789";
    shared_ptr<Data> data = make_shared<Data>();
    data->setName(i.getName());
    data->setFreshnessPeriod(time::seconds(10));
    data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
    data->setCongestionMark(i.getCongestionMark());
    m_keyChain.sign(*data);
    m_face.put(*data);
  }

  void
  onRegisterFailed(const Name& prefix, const std::string& reason)
  {
    std::cerr << "Failed to register prefix " << prefix << " (reason: " + reason + ")" << std::endl;
    m_face.shutdown();
  }

private:
  Face m_face;
  KeyChain m_keyChain;
};

int
main(int argc, char** argv)
{
  if (argc != 2) {
    return 2;
  }

  try {
    TestCongestionMarkProducer producer;
    producer.run(argv[1]);
  }
  catch (const std::exception& e) {
    std::cerr << "ERROR: " << e.what() << std::endl;
    return 1;
  }

  return 0;
}
