blob: 32a14eb301dcee642bdab4952fbb15151ff552ba [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Davide Pesavento2f46d652023-11-09 23:40:01 -05003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
Davide Pesavento19442812018-11-23 14:00:04 -050022#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024
Davide Pesavento2f46d652023-11-09 23:40:01 -050025#include <boost/asio/io_context.hpp>
Davide Pesaventocdcde902017-08-23 15:40:22 -040026#include <iostream>
27
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
29namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070030// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070031namespace examples {
32
Davide Pesavento4d0d0962017-12-19 22:23:14 -050033class ConsumerWithTimer
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070035public:
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070036 void
37 run()
38 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070039 Name interestName("/example/testApp/randomData");
40 interestName.appendVersion();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070042 Interest interest(interestName);
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070043 interest.setMustBeFresh(true);
44 interest.setInterestLifetime(2_s);
45
46 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070047 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040048 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
49 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
50 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070051
Alexander Afanasyev151a8552014-04-11 00:54:43 -070052 // Schedule a new event
Junxiao Shia5f233e2019-03-18 09:39:22 -060053 m_scheduler.schedule(3_s, [this] { delayedInterest(); });
Alexander Afanasyev151a8552014-04-11 00:54:43 -070054
Davide Pesavento2f46d652023-11-09 23:40:01 -050055 // m_ioCtx.run() will block until all events finished or m_ioCtx.stop() is called
56 m_ioCtx.run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070057
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070058 // Alternatively, m_face.processEvents() can also be called.
59 // processEvents will block until the requested data received or timeout occurs.
60 // m_face.processEvents();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070062
63private:
64 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070065 onData(const Interest&, const Data& data) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070066 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070067 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070069
70 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070071 onNack(const Interest& interest, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070072 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070073 std::cout << "Received Nack with reason " << nack.getReason()
74 << " for " << interest << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070075 }
76
77 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070078 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070079 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070080 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070081 }
82
83 void
84 delayedInterest()
85 {
86 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
87
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070088 Name interestName("/example/testApp/randomData");
89 interestName.appendVersion();
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070090
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070091 Interest interest(interestName);
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070092 interest.setMustBeFresh(true);
93 interest.setInterestLifetime(2_s);
94
95 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070096 m_face.expressInterest(interest,
Davide Pesavento2e481fc2021-07-02 18:20:03 -040097 std::bind(&ConsumerWithTimer::onData, this, _1, _2),
98 std::bind(&ConsumerWithTimer::onNack, this, _1, _2),
99 std::bind(&ConsumerWithTimer::onTimeout, this, _1));
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700100 }
101
102private:
Davide Pesavento2f46d652023-11-09 23:40:01 -0500103 // Explicitly create io_context object, which will be shared between Face and Scheduler
104 boost::asio::io_context m_ioCtx;
105 Face m_face{m_ioCtx};
106 Scheduler m_scheduler{m_ioCtx};
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700107};
108
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700109} // namespace examples
110} // namespace ndn
111
112int
113main(int argc, char** argv)
114{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700115 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700116 ndn::examples::ConsumerWithTimer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700117 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700118 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700119 }
120 catch (const std::exception& e) {
121 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700122 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700123 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700124}