Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 24 | // correct way to include ndn-cxx headers |
| 25 | // #include <ndn-cxx/face.hpp> |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 26 | #include "face.hpp" |
| 27 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 28 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 29 | namespace ndn { |
| 30 | // Additional nested namespace could be used to prevent/limit name contentions |
| 31 | namespace examples { |
| 32 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 33 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 34 | onData(Face& face, |
| 35 | const Interest& interest, Data& data) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 37 | std::cout << "I: " << interest.toUri() << std::endl; |
| 38 | std::cout << "D: " << data.getName().toUri() << std::endl; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 42 | onTimeout(Face& face, |
| 43 | const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 44 | { |
| 45 | std::cout << "Timeout" << std::endl; |
| 46 | } |
| 47 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 48 | int |
| 49 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 50 | { |
| 51 | try { |
Alexander Afanasyev | b678306 | 2014-06-13 13:03:25 -0700 | [diff] [blame] | 52 | Interest i(Name("/example/testApp/randomData")); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 53 | i.setScope(1); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 54 | i.setInterestLifetime(time::milliseconds(1000)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 55 | i.setMustBeFresh(true); |
| 56 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 57 | Face face; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 58 | face.expressInterest(i, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 59 | bind(onData, ref(face), _1, _2), |
| 60 | bind(onTimeout, ref(face), _1)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 61 | |
| 62 | // processEvents will block until the requested data received or timeout occurs |
| 63 | face.processEvents(); |
| 64 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 65 | catch(std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 66 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 67 | return 1; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 68 | } |
| 69 | return 0; |
| 70 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 71 | |
| 72 | } // namespace examples |
| 73 | } // namespace ndn |
| 74 | |
| 75 | int |
| 76 | main(int argc, char** argv) |
| 77 | { |
| 78 | return ndn::examples::main(argc, argv); |
| 79 | } |