docs: improve example applications
refs: #1976
Change-Id: I82e719ac497d3cb4eab74c1c6d02bcaa050388eb
diff --git a/examples/consumer-with-timer.cpp b/examples/consumer-with-timer.cpp
index 7897b41..c08a312 100644
--- a/examples/consumer-with-timer.cpp
+++ b/examples/consumer-with-timer.cpp
@@ -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 @@
// 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 @@
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;
}