blob: ef6cd76832ebd13d5b5e3677ad1e610f446da5e7 [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/*
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -07003 * Copyright (c) 2013-2019 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080022 */
23
Davide Pesavento19442812018-11-23 14:00:04 -050024#include <ndn-cxx/face.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080025
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
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070033class Consumer
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080034{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070035public:
36 void
37 run()
38 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070039 Name interestName("/example/testApp/randomData");
40 interestName.appendVersion();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080041
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070042 Interest interest(interestName);
43 interest.setCanBePrefix(false);
44 interest.setMustBeFresh(true);
45 interest.setInterestLifetime(6_s); // The default is 4 seconds
46
47 std::cout << "Sending Interest " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070048 m_face.expressInterest(interest,
49 bind(&Consumer::onData, this, _1, _2),
Weiwei Liuab9aad02016-10-09 13:56:04 -070050 bind(&Consumer::onNack, this, _1, _2),
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070051 bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080052
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070053 // processEvents will block until the requested data is received or a timeout occurs
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070054 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070056
57private:
58 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070059 onData(const Interest&, const Data& data) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070060 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070061 std::cout << "Received Data " << data << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080062 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070063
64 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070065 onNack(const Interest&, const lp::Nack& nack) const
Weiwei Liuab9aad02016-10-09 13:56:04 -070066 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070067 std::cout << "Received Nack with reason " << nack.getReason() << std::endl;
Weiwei Liuab9aad02016-10-09 13:56:04 -070068 }
69
70 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070071 onTimeout(const Interest& interest) const
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070072 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070073 std::cout << "Timeout for " << interest << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070074 }
75
76private:
77 Face m_face;
78};
Alexander Afanasyev151a8552014-04-11 00:54:43 -070079
80} // namespace examples
81} // namespace ndn
82
83int
84main(int argc, char** argv)
85{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070086 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070087 ndn::examples::Consumer consumer;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070088 consumer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070089 return 0;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070090 }
91 catch (const std::exception& e) {
92 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070093 return 1;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070094 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070095}