Project

General

Profile

Bug #2643 » producer-exclude.cpp

Ilya Moiseenko, 03/13/2015 05:10 PM

 
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*
* @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
*/

// correct way to include ndn-cxx headers
// #include <ndn-cxx/face.hpp>
// #include <ndn-cxx/security/key-chain.hpp>

#include "face.hpp"
#include "security/key-chain.hpp"
#include <sstream>

// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
namespace ndn {
// Additional nested namespace could be used to prevent/limit name contentions
namespace examples {

class Producer
{
public:
Producer()
: m_counter(0)
{}

void
onInterest(const Name& name, const Interest& interest)
{
std::cout << "<< I: " << interest.toUri() << std::endl;

// Create new name, based on Interest's name
Name dataName(interest.getName());

std::stringstream ss;
ss << m_counter;
std::string content = ss.str();

// Create Data packet
Data data;
data.setName(dataName);
data.setFreshnessPeriod(time::seconds(10));
data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());

// Sign Data packet with default identity
m_keyChain.sign(data);

// Return Data packet to the requester
std::cout << ">> D: " << data << std::endl;
m_face.put(data);
}


void
onRegisterFailed(const std::string& reason)
{
std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
<< std::endl;
m_face.shutdown();
}

void
run()
{
m_face.setInterestFilter("/example/testApp",
bind(&Producer::onInterest, this, _1, _2),
RegisterPrefixSuccessCallback(),
bind(&Producer::onRegisterFailed, this, _2));
m_face.processEvents();
}

private:
Face m_face;
KeyChain m_keyChain;
int m_counter;
};

} // namespace examples
} // namespace ndn

int
main(int argc, char** argv)
{
try {
ndn::examples::Producer producer;
producer.run();
}
catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
return 0;
}

(2-2/3)