blob: 6a76169e14969bcabc4525e49eee59fde7c32063 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080033void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070034onData(Face& face,
35 const Interest& interest, Data& data)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080036{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080037 std::cout << "I: " << interest.toUri() << std::endl;
38 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080039}
40
41void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070042onTimeout(Face& face,
43 const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080044{
45 std::cout << "Timeout" << std::endl;
46}
47
Alexander Afanasyev151a8552014-04-11 00:54:43 -070048int
49main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080050{
51 try {
Alexander Afanasyevb6783062014-06-13 13:03:25 -070052 Interest i(Name("/example/testApp/randomData"));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080053 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070054 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080055 i.setMustBeFresh(true);
56
Alexander Afanasyev151a8552014-04-11 00:54:43 -070057 Face face;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080058 face.expressInterest(i,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070059 bind(onData, ref(face), _1, _2),
60 bind(onTimeout, ref(face), _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080061
62 // processEvents will block until the requested data received or timeout occurs
63 face.processEvents();
64 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065 catch(std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080066 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070067 return 1;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080068 }
69 return 0;
70}
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071
72} // namespace examples
73} // namespace ndn
74
75int
76main(int argc, char** argv)
77{
78 return ndn::examples::main(argc, argv);
79}