docs: improve example applications

refs: #1976

Change-Id: I82e719ac497d3cb4eab74c1c6d02bcaa050388eb
This commit is contained in:
Steve DiBenedetto authored and Alexander Afanasyev committed 2015-01-16 14:59:49 -08:00
1 parent 0711380b4c
commit 9fcc24fd75
4 files changed
+138 -125

No files matched your search

+4 -8
View File
@@ -15,15 +15,10 @@ transport (:ndn-cxx:`UnixTransport`) and sends an Interest for
``/localhost/testApp/randomData``. While expressing Interest, the app specifies two
callbacks to be called when Data is retrieved or Interest times out.
``ndn::bind`` is an alias for either `boost::bind
<http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html>`_ or `std::bind
<http://en.cppreference.com/w/cpp/utility/functional/bind>`_ when the library is compiled
in C++11 mode.
.. literalinclude:: ../examples/consumer.cpp
:language: c++
:linenos:
:emphasize-lines: 24-26,32-34,48-52,54
:emphasize-lines: 24-27,39,43-46,50,57,67
Trivial producer
@@ -47,7 +42,7 @@ using :ndn-cxx:`Face::put` method.
.. literalinclude:: ../examples/producer.cpp
:language: c++
:linenos:
:emphasize-lines: 44-47,50,56,71-74
:emphasize-lines: 42,55-58,61,64,67,70,76
Consumer that uses ndn::Scheduler
@@ -71,4 +66,5 @@ Interest approximately 2 seconds after the first one.
.. literalinclude:: ../examples/consumer-with-timer.cpp
:language: c++
:linenos:
:emphasize-lines: 19,61,76,79-80,83
:emphasize-lines: 39-40,51-54,58-59,61-62,99-100
+70 -60
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
* Copyright (c) 2013-2015 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -32,74 +32,77 @@ namespace ndn {
// Additional nested namespace could be used to prevent/limit name contentions
namespace examples {
void
onData(Face& face,
const Interest& interest, Data& data)
class ConsumerWithTimer : noncopyable
{
std::cout << "I: " << interest.toUri() << std::endl;
std::cout << "D: " << data.getName().toUri() << std::endl;
}
public:
ConsumerWithTimer()
: m_face(m_ioService) // Create face with io_service object
, m_scheduler(m_ioService)
{
}
void
onTimeout(Face& face,
const Interest& interest)
{
std::cout << "Timeout" << std::endl;
}
void
run()
{
Interest interest(Name("/example/testApp/randomData"));
interest.setInterestLifetime(time::seconds(1));
interest.setMustBeFresh(true);
void
delayedInterest(Face& face)
{
std::cout << "One more Interest, delayed by the scheduler" << std::endl;
m_face.expressInterest(interest,
bind(&ConsumerWithTimer::onData, this, _1, _2),
bind(&ConsumerWithTimer::onTimeout, this, _1));
Interest i(Name("/example/testApp/randomData"));
i.setScope(1);
i.setInterestLifetime(time::milliseconds(1000));
i.setMustBeFresh(true);
face.expressInterest(i,
bind(&onData, ref(face), _1, _2),
bind(&onTimeout, ref(face), _1));
}
int
main(int argc, char** argv)
{
try {
// Explicitly create io_service object, which can be shared between Face and Scheduler
boost::asio::io_service ioService;
Interest i(Name("/example/testApp/randomData"));
i.setScope(1);
i.setInterestLifetime(time::seconds(1));
i.setMustBeFresh(true);
// Create face with io_service object
Face face(ioService);
face.expressInterest(i,
bind(&onData, ref(face), _1, _2),
bind(&onTimeout, ref(face), _1));
// Create scheduler object
Scheduler scheduler(ioService);
std::cout << "Sending " << interest << std::endl;
// Schedule a new event
scheduler.scheduleEvent(time::seconds(2),
bind(&delayedInterest, ref(face)));
m_scheduler.scheduleEvent(time::seconds(2),
bind(&ConsumerWithTimer::delayedInterest, this));
// ioService.run() will block until all events finished or ioService.stop() is called
ioService.run();
// m_ioService.run() will block until all events finished or m_ioService.stop() is called
m_ioService.run();
// Alternatively, a helper face.processEvents() also can be called
// processEvents will block until the requested data received or timeout occurs
// face.processEvents();
// Alternatively, m_face.processEvents() can also be called.
// processEvents will block until the requested data received or timeout occurs.
// m_face.processEvents();
}
catch(std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
private:
void
onData(const Interest& interest, const Data& data)
{
std::cout << data << std::endl;
}
return 0;
}
void
onTimeout(const Interest& interest)
{
std::cout << "Timeout " << interest << std::endl;
}
void
delayedInterest()
{
std::cout << "One more Interest, delayed by the scheduler" << std::endl;
Interest interest(Name("/example/testApp/randomData"));
interest.setInterestLifetime(time::milliseconds(1000));
interest.setMustBeFresh(true);
m_face.expressInterest(interest,
bind(&ConsumerWithTimer::onData, this, _1, _2),
bind(&ConsumerWithTimer::onTimeout, this, _1));
std::cout << "Sending " << interest << std::endl;
}
private:
// Explicitly create io_service object, which can be shared between Face and Scheduler
boost::asio::io_service m_ioService;
Face m_face;
Scheduler m_scheduler;
};
} // namespace examples
} // namespace ndn
@@ -107,5 +110,12 @@ main(int argc, char** argv)
int
main(int argc, char** argv)
{
return ndn::examples::main(argc, argv);
ndn::examples::ConsumerWithTimer consumer;
try {
consumer.run();
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
return 0;
}
+38 -33
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
* Copyright (c) 2013-2015 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -30,44 +30,42 @@ namespace ndn {
// Additional nested namespace could be used to prevent/limit name contentions
namespace examples {
void
onData(Face& face,
const Interest& interest, Data& data)
class Consumer : noncopyable
{
std::cout << "I: " << interest.toUri() << std::endl;
std::cout << "D: " << data.getName().toUri() << std::endl;
}
public:
void
run()
{
Interest interest(Name("/example/testApp/randomData"));
interest.setInterestLifetime(time::milliseconds(1000));
interest.setMustBeFresh(true);
void
onTimeout(Face& face,
const Interest& interest)
{
std::cout << "Timeout" << std::endl;
}
m_face.expressInterest(interest,
bind(&Consumer::onData, this, _1, _2),
bind(&Consumer::onTimeout, this, _1));
int
main(int argc, char** argv)
{
try {
Interest i(Name("/example/testApp/randomData"));
i.setScope(1);
i.setInterestLifetime(time::milliseconds(1000));
i.setMustBeFresh(true);
Face face;
face.expressInterest(i,
bind(onData, ref(face), _1, _2),
bind(onTimeout, ref(face), _1));
std::cout << "Sending " << interest << std::endl;
// processEvents will block until the requested data received or timeout occurs
face.processEvents();
m_face.processEvents();
}
catch(std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
private:
void
onData(const Interest& interest, const Data& data)
{
std::cout << data << std::endl;
}
return 0;
}
void
onTimeout(const Interest& interest)
{
std::cout << "Timeout " << interest << std::endl;
}
private:
Face m_face;
};
} // namespace examples
} // namespace ndn
@@ -75,5 +73,12 @@ main(int argc, char** argv)
int
main(int argc, char** argv)
{
return ndn::examples::main(argc, argv);
ndn::examples::Consumer consumer;
try {
consumer.run();
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
return 0;
}
+26 -24
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
* Copyright (c) 2013-2015 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -33,11 +33,22 @@ namespace ndn {
// Additional nested namespace could be used to prevent/limit name contentions
namespace examples {
class Producer
class Producer : noncopyable
{
public:
void
onInterest(const Name& name, const Interest& interest)
run()
{
m_face.setInterestFilter("/example/testApp",
bind(&Producer::onInterest, this, _1, _2),
RegisterPrefixSuccessCallback(),
bind(&Producer::onRegisterFailed, this, _1, _2));
m_face.processEvents();
}
private:
void
onInterest(const InterestFilter& filter, const Interest& interest)
{
std::cout << "<< I: " << interest << std::endl;
@@ -50,40 +61,31 @@ public:
static const std::string content = "HELLO KITTY";
// 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());
shared_ptr<Data> data = make_shared<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);
m_keyChain.sign(*data);
// m_keyChain.sign(data, <identityName>);
// m_keyChain.sign(data, <certificate>);
// Return Data packet to the requester
std::cout << ">> D: " << data << std::endl;
m_face.put(data);
std::cout << ">> D: " << *data << std::endl;
m_face.put(*data);
}
void
onRegisterFailed(const std::string& reason)
onRegisterFailed(const Name& prefix, const std::string& reason)
{
std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")"
std::cerr << "ERROR: Failed to register prefix \""
<< 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;
@@ -95,11 +97,11 @@ private:
int
main(int argc, char** argv)
{
ndn::examples::Producer producer;
try {
ndn::examples::Producer producer;
producer.run();
}
catch (std::exception& e) {
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
return 0;