blob: 4042565f4e292f3dc5bfded6f778db61041ef7f8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08002/**
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -07003 * Copyright (c) 2013-2015 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
Alexander Afanasyev766cea72014-04-24 19:16:42 -070024// correct way to include ndn-cxx headers
25// #include <ndn-cxx/face.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080026#include "face.hpp"
27
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
29namespace ndn {
30// Additional nested namespace could be used to prevent/limit name contentions
31namespace examples {
32
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070033class Consumer : noncopyable
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080034{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070035public:
36 void
37 run()
38 {
39 Interest interest(Name("/example/testApp/randomData"));
40 interest.setInterestLifetime(time::milliseconds(1000));
41 interest.setMustBeFresh(true);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080042
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070043 m_face.expressInterest(interest,
44 bind(&Consumer::onData, this, _1, _2),
45 bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080046
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070047 std::cout << "Sending " << interest << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080048
49 // processEvents will block until the requested data received or timeout occurs
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070050 m_face.processEvents();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080051 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070052
53private:
54 void
55 onData(const Interest& interest, const Data& data)
56 {
57 std::cout << data << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070059
60 void
61 onTimeout(const Interest& interest)
62 {
63 std::cout << "Timeout " << interest << std::endl;
64 }
65
66private:
67 Face m_face;
68};
Alexander Afanasyev151a8552014-04-11 00:54:43 -070069
70} // namespace examples
71} // namespace ndn
72
73int
74main(int argc, char** argv)
75{
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070076 ndn::examples::Consumer consumer;
77 try {
78 consumer.run();
79 }
80 catch (const std::exception& e) {
81 std::cerr << "ERROR: " << e.what() << std::endl;
82 }
83 return 0;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084}